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 5     5   57704 use strict;
  5         15  
  5         175  
4 5     5   27 use warnings;
  5         12  
  5         128  
5              
6 5     5   5445 use Crypt::ECB;
  5         16497  
  5         392  
7 5     5   23184 use Data::Dumper;
  5         96549  
  5         4175  
8              
9             ### constructor ###
10              
11             sub new {
12              
13 3     3 1 21 my $caller = shift;
14              
15 3         8 my $class = ref( $caller );
16 3 50       13 $class = $caller if ( !$class );
17              
18 3         20 my $self = {'encryption_key' => undef,
19             'decryption_key' => undef,
20             @_};
21              
22 3         10 bless( $self, $class );
23              
24 3         26 my $crypt = Crypt::ECB->new();
25              
26 3         76 $crypt->padding( PADDING_AUTO );
27 3         39 $crypt->cipher( 'Blowfish' );
28              
29 3         8992 $self->{'crypt'} = $crypt;
30              
31 3         16 return $self;
32             }
33              
34             ### getters/setters ###
35              
36             sub error {
37              
38 8     8 1 1981 my ( $self, $error ) = @_;
39              
40 8 100       22 $self->{'error'} = $error if ( defined( $error ) );
41              
42 8         34 return $self->{'error'};
43             }
44              
45             ### public methods ###
46              
47             sub encrypt {
48              
49 3     3 1 707 my ( $self, $data ) = @_;
50              
51             # make sure encryption_key defined
52 3 100       14 if ( !defined( $self->{'encryption_key'} ) ) {
53              
54 1         4 $self->error( 'An encryption_key must be provided to the constructor.' );
55 1         3 return;
56             }
57              
58             # make sure data to encrypt was given
59 2 100       11 if ( !defined( $data ) ) {
60              
61 1         6 $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         8 $self->{'crypt'}->key( $self->{'encryption_key'} );
67              
68             # return the hex-encrypted form
69 1         13 return $self->{'crypt'}->encrypt_hex( $data );
70             }
71              
72             sub decrypt {
73              
74 3     3 1 1255 my ( $self, $data ) = @_;
75              
76             # make sure decryption_key defined
77 3 100       12 if ( !defined( $self->{'decryption_key'} ) ) {
78              
79 1         5 $self->error( 'A decryption_key must be provided to the constructor.' );
80 1         3 return;
81             }
82              
83             # make sure data to decrypt was given
84 2 100       7 if ( !defined( $data ) ) {
85              
86 1         5 $self->error( 'A string of data to decrypt must be given.' );
87 1         4 return;
88             }
89              
90             # give the crypt object the decryption key
91 1         7 $self->{'crypt'}->key( $self->{'decryption_key'} );
92              
93             # return the decrypted string
94 1         11 return $self->{'crypt'}->decrypt_hex( $data );
95             }
96              
97             1;