File Coverage

blib/lib/WebService/Pandora/Cryptor.pm
Criterion Covered Total %
statement 43 43 100.0
branch 11 12 91.6
condition n/a
subroutine 8 8 100.0
pod 4 4 100.0
total 66 67 98.5


line stmt bran cond sub pod time code
1             package WebService::Pandora::Cryptor;
2              
3 4     4   20640 use strict;
  4         8  
  4         92  
4 4     4   19 use warnings;
  4         7  
  4         93  
5              
6 4     4   3148 use Crypt::ECB;
  4         9151  
  4         235  
7 4     4   3828 use Data::Dumper;
  4         40390  
  4         1595  
8              
9             ### constructor ###
10              
11             sub new {
12              
13 3     3 1 16 my $caller = shift;
14              
15 3         7 my $class = ref( $caller );
16 3 50       11 $class = $caller if ( !$class );
17              
18 3         13 my $self = {'encryption_key' => undef,
19             'decryption_key' => undef,
20             @_};
21              
22 3         7 bless( $self, $class );
23              
24 3         20 my $crypt = Crypt::ECB->new();
25              
26 3         55 $crypt->padding( PADDING_AUTO );
27 3         33 $crypt->cipher( 'Blowfish' );
28              
29 3         3597 $self->{'crypt'} = $crypt;
30              
31 3         12 return $self;
32             }
33              
34             ### getters/setters ###
35              
36             sub error {
37              
38 8     8 1 1107 my ( $self, $error ) = @_;
39              
40 8 100       21 $self->{'error'} = $error if ( defined( $error ) );
41              
42 8         25 return $self->{'error'};
43             }
44              
45             ### public methods ###
46              
47             sub encrypt {
48              
49 3     3 1 464 my ( $self, $data ) = @_;
50              
51             # make sure encryption_key defined
52 3 100       11 if ( !defined( $self->{'encryption_key'} ) ) {
53              
54 1         4 $self->error( 'An encryption_key must be provided to the constructor.' );
55 1         2 return;
56             }
57              
58             # make sure data to encrypt was given
59 2 100       9 if ( !defined( $data ) ) {
60              
61 1         4 $self->error( 'A string of data to encrypt must be given.' );
62 1         2 return;
63             }
64              
65             # give the crypt object the encryption key
66 1         6 $self->{'crypt'}->key( $self->{'encryption_key'} );
67              
68             # return the hex-encrypted form
69 1         11 return $self->{'crypt'}->encrypt_hex( $data );
70             }
71              
72             sub decrypt {
73              
74 3     3 1 764 my ( $self, $data ) = @_;
75              
76             # make sure decryption_key defined
77 3 100       10 if ( !defined( $self->{'decryption_key'} ) ) {
78              
79 1         4 $self->error( 'A decryption_key must be provided to the constructor.' );
80 1         2 return;
81             }
82              
83             # make sure data to decrypt was given
84 2 100       6 if ( !defined( $data ) ) {
85              
86 1         4 $self->error( 'A string of data to decrypt must be given.' );
87 1         3 return;
88             }
89              
90             # give the crypt object the decryption key
91 1         5 $self->{'crypt'}->key( $self->{'decryption_key'} );
92              
93             # return the decrypted string
94 1         8 return $self->{'crypt'}->decrypt_hex( $data );
95             }
96              
97             1;