File Coverage

blib/lib/App/OATH/Crypt/Rijndael.pm
Criterion Covered Total %
statement 40 40 100.0
branch n/a
condition n/a
subroutine 10 10 100.0
pod 3 3 100.0
total 53 53 100.0


line stmt bran cond sub pod time code
1             package App::OATH::Crypt::Rijndael;
2             our $VERSION = '1.20151002'; # VERSION
3              
4 1     1   5 use strict;
  1         2  
  1         26  
5 1     1   5 use warnings;
  1         1  
  1         25  
6 1     1   6 use Convert::Base32;
  1         2  
  1         66  
7 1     1   831 use Crypt::Rijndael;
  1         581  
  1         32  
8 1     1   6 use Digest::MD5;
  1         2  
  1         42  
9 1     1   814 use String::Random qw{ random_string };
  1         3726  
  1         342  
10              
11             sub new {
12 13     13 1 24     my ( $class, $args ) = @_;
13                 my $self = {
14 13         81         'password' => $args->{'password'},
15                 };
16 13         25     bless $self, $class;
17 13         124     return $self;
18             }
19              
20             sub _get_crypt_object {
21 6     6   8     my ( $self ) = @_;
22 6         17     my $password = $self->{'password'};
23              
24 6         27     my $md5 = Digest::MD5->new();
25 6         23     $md5->add( $password );
26 6         22     my $crypt_key = $md5->digest();
27              
28 6         52     my $crypt = Crypt::Rijndael->new( $crypt_key, Crypt::Rijndael::MODE_CBC() );
29 6         26     return $crypt;
30             }
31              
32             sub encrypt {
33 2     2 1 6     my ( $self, $data ) = @_;
34 2         7     my $worker = $self->_get_crypt_object();
35 2         11     my $pad = random_string( '.' x ( 16 - ( length( $data ) % 16 ) ) );
36              
37 2         122     my $e = $worker->encrypt( $pad . $data );
38 2         8     $e = encode_base32( $e );
39 2         117     return $e;
40             }
41              
42             sub decrypt {
43 4     4 1 6     my ( $self, $data ) = @_;
44 4         11     my $worker = $self->_get_crypt_object();
45 4         16     my $e = decode_base32( $data );
46 3         132     my $u = $worker->decrypt($e);
47 3         12     return $u;
48             }
49              
50             1;
51              
52             __END__
53            
54             =head1 NAME
55            
56             App::OATH::Crypt::Rijndael - Crypto modules for Simple OATH authenticator
57            
58             =head1 DESCRIPTION
59            
60             Crypto modules for basic Rijndael
61            
62             =head1 SYNOPSIS
63            
64             Handles encryption and decryption for the basic Rijndael (not CBC) ciphers
65            
66             =head1 METHODS
67            
68             =over
69            
70             =item I<new()>
71            
72             Instantiate a new object
73            
74             =item I<encrypt($data)>
75            
76             Encrypt the given data
77            
78             =item I<decrypt($data)>
79            
80             Decrypt the given data
81            
82             =back
83            
84             =head1 DEPENDENCIES
85            
86             Convert::Base32
87             Crypt::Rijndael
88             Digest::MD5
89             String::Random
90            
91             =head1 AUTHORS
92            
93             Marc Bradshaw E<lt>marc@marcbradshaw.netE<gt>
94            
95             =head1 COPYRIGHT
96            
97             Copyright 2015
98            
99             This library is free software; you may redistribute it and/or
100             modify it under the same terms as Perl itself.
101            
102