File Coverage

blib/lib/App/OATH/Crypt.pm
Criterion Covered Total %
statement 47 47 100.0
branch 14 14 100.0
condition 3 3 100.0
subroutine 10 10 100.0
pod 5 5 100.0
total 79 79 100.0


line stmt bran cond sub pod time code
1             package App::OATH::Crypt;
2             our $VERSION = '1.20151002'; # VERSION
3              
4 1     1   5 use strict;
  1         1  
  1         27  
5 1     1   5 use warnings;
  1         3  
  1         34  
6              
7 1     1   727 use App::OATH::Crypt::Rijndael;
  1         4  
  1         91  
8 1     1   763 use App::OATH::Crypt::CBC;
  1         2  
  1         36  
9 1     1   6 use String::Random qw{ random_string };
  1         2  
  1         574  
10              
11             sub new {
12 13     13 1 3963     my ( $class, $password ) = @_;
13                 
14 13         107     my $self = {
15                     'workers' => {
16                         'rijndael' => App::OATH::Crypt::Rijndael->new({ 'password' => $password }),
17                         'cbcrijndael' => App::OATH::Crypt::CBC->new({ 'password' => $password, 'type' => 'Rijndael', }),
18                         'cbcblowfish' => App::OATH::Crypt::CBC->new({ 'password' => $password, 'type' => 'Blowfish', }),
19                     },
20                     'type' => q{},
21                     'check' => 'oath',
22                 };
23 13         46     bless $self, $class;
24 13         30     return $self;
25             }
26              
27             sub get_workers_list {
28 1     1 1 368     my ( $self ) = @_;
29 1         3     my @list = sort keys %{ $self->{'workers'} };
  1         7  
30 1         4     return \@list;
31             }
32              
33             sub set_worker {
34 7     7 1 7797     my ( $self, $type ) = @_;
35 7 100 100     49     if ( $type ne q{} and not exists( $self->{'workers'}->{$type} ) ) {
36 1         13         die "Unknown encryption type $type";
37                 }
38 6         13     $self->{'type'} = $type;
39 6         13     return;
40             }
41              
42             sub encrypt {
43 16     16 1 47     my ( $self, $data ) = @_;
44 16         37     my $type = $self->{'type'};
45 16 100       46     $type = 'cbcrijndael' if $type eq q{};
46 16         34     my $worker = $self->{'workers'}->{$type};
47 16         58     my $u = random_string( '..........' ) . ' ' . $self->{'check'} . ' ' . $data;
48 16         1707     return $type . ':' . $worker->encrypt( $u );
49             }
50              
51             sub decrypt {
52 19     19 1 4508     my ( $self, $data ) = @_;
53 19         39     my $type = $self->{'type'};
54 19 100       61     $type = 'rijndael' if $type eq q{};
55 19 100       64     if ( $data =~ /:/ ) {
56 15         59         ( $type, $data ) = split ':', $data;
57                 }
58 19         48     my $worker = $self->{'workers'}->{$type};
59 19 100       77     die "Unknown encryption type $type" if ! $worker;
60 18         63     my $u = $worker->decrypt( $data );
61 15         55     my ( $salt, $check, $payload ) = split( ' ', $u );
62 15 100       42     $check = q{} if ! $check;
63 15 100       43     if ( $check ne $self->{'check'} ) {
64 5         17         return;
65                 }
66 10         27     return $payload;
67             }
68              
69             1;
70              
71             __END__
72            
73             =head1 NAME
74            
75             App::OATH::Crypt - Crypto modules for Simple OATH authenticator
76            
77             =head1 DESCRIPTION
78            
79             Crypto modules super class
80            
81             =head1 SYNOPSIS
82            
83             Handles all crypto, detection of methods and handing off to sub
84             modules which implement the actual encryption and decryption of data
85            
86             =head1 METHODS
87            
88             =over
89            
90             =item I<new()>
91            
92             Instantiate a new object
93            
94             =item I<get_workers_list()>
95            
96             Return an array ref of possible worker types
97            
98             =item I<set_worker($worker)>
99            
100             Set the default worker type
101            
102             =item I<encrypt($data)>
103            
104             Encrypt the given data
105            
106             =item I<decrypt($data)>
107            
108             Decrypt the given data
109            
110             =back
111            
112             =head1 DEPENDENCIES
113            
114             =head1 AUTHORS
115            
116             Marc Bradshaw E<lt>marc@marcbradshaw.netE<gt>
117            
118             =head1 COPYRIGHT
119            
120             Copyright 2015
121            
122             This library is free software; you may redistribute it and/or
123             modify it under the same terms as Perl itself.
124            
125