App-OATH

 view release on metacpan or  search on metacpan

lib/App/OATH/Crypt.pm  view on Meta::CPAN

package App::OATH::Crypt;
our $VERSION = '1.20260324'; # VERSION

use strict;
use warnings;

use App::OATH::Crypt::Rijndael;
use App::OATH::Crypt::CBC;
use String::Random qw{ random_string };

sub new {
    my ( $class, $password ) = @_;
    
    my $self = {
        'workers' => {
            'rijndael'    => App::OATH::Crypt::Rijndael->new({ 'password' => $password }),
            'cbcrijndael' => App::OATH::Crypt::CBC->new({ 'password' => $password, 'type' => 'Rijndael', }),
            'cbcblowfish' => App::OATH::Crypt::CBC->new({ 'password' => $password, 'type' => 'Blowfish', }),
        },
        'type'  => q{},
        'check' => 'oath',
    };
    bless $self, $class;
    return $self;
}

sub get_workers_list {
    my ( $self ) = @_;
    my @list = sort keys %{ $self->{'workers'} };
    return \@list;
}

sub set_worker {
    my ( $self, $type ) = @_;
    if ( $type ne q{} and not exists( $self->{'workers'}->{$type} ) ) {
        die "Unknown encryption type $type";
    }
    $self->{'type'} = $type;
    return;
}

sub encrypt {
    my ( $self, $data ) = @_;
    my $type = $self->{'type'};
    $type = 'cbcrijndael' if $type eq q{};
    my $worker = $self->{'workers'}->{$type};
    my $u = random_string( '..........' ) . ' ' . $self->{'check'} . ' ' . $data;
    return $type . ':' . $worker->encrypt( $u );
}

sub decrypt {
    my ( $self, $data ) = @_;
    my $type = $self->{'type'};
    $type = 'rijndael' if $type eq q{};
    if ( $data =~ /:/ ) {
        ( $type, $data ) = split ':', $data;
    }
    my $worker = $self->{'workers'}->{$type};
    die "Unknown encryption type $type" if ! $worker;
    my $u = $worker->decrypt( $data );
    my ( $salt, $check, $payload ) = split( ' ', $u );
    $check = q{} if ! $check;



( run in 2.063 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )