File Coverage

blib/lib/App/OATH/Crypt.pm
Criterion Covered Total %
statement 38 38 100.0
branch 10 10 100.0
condition 3 3 100.0
subroutine 9 9 100.0
pod 5 5 100.0
total 65 65 100.0


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