blib/lib/DBIx/Raw/Crypt.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 99 CC 7 SCAR 22.3
CC
7
Cov
0%
decrypt
20.0
new
12.0
encrypt
6.0
CRAP 9.3
1 # THIS IS BORROWED FROM Gantry::Utils::Crypt
2 # He did not have Crypt::CBC listed as a dependency, and didn't fix it
3 # even though it was listed as a bug several years ago. Thus, I have copied
4 # this into my package so that installation will work. Also, since this was a
5 # part of the Gantry web Framework, it would install a lot of unnecessary modules
6 # not needed for DBIx::Raw. So I have copied this here for my own use. If you are
7 # interested in using this crypt functionality, please see Gantry::Utils::Crypt instead
8 # of using this module
9 package DBIx::Raw::Crypt;
10 use strict;
11  
12 use Crypt::CBC;
13 use MIME::Base64;
14 use Digest::MD5 qw( md5_hex );
15  
16 sub new {
17 my ( $class, $opt ) = @_;
18  
19 my $self = { options => $opt };
20 bless( $self, $class );
21  
22 my @errors;
23 foreach( qw/secret/ ) {
24 push( @errors, "$_ is not set properly" ) if ! $opt->{$_};
25 }
26  
27 if ( scalar( @errors ) ) {
28 die join( "\n", @errors );
29 }
30
31 # populate self with data from site
32 return( $self );
33  
34 } # end new
35  
36 #-------------------------------------------------
37 # decrypt()
38 #-------------------------------------------------
39 sub decrypt {
40 my ( $self, $encrypted ) = @_;
41  
42 $encrypted ||= '';
43 $self->set_error( undef );
44
45 local $^W = 0;
46
47 my $c;
48 eval {
49 $c = new Crypt::CBC ( {
50 'key' => $self->{options}{secret},
51 'cipher' => 'Blowfish',
52 'padding' => 'null',
53 } );
54 };
55 if ( $@ ) {
56 my $error = (
57 "Error building CBC object are your Crypt::CBC and"
58 . " Crypt::Blowfish up to date? Actual error: $@"
59 );
60
61 $self->set_error( $error );
62 die $error;
63 }
64  
65 my $p_text = $c->decrypt( MIME::Base64::decode( $encrypted ) );
66
67 $c->finish();
68
69 my @decrypted_values = split( ':;:', $p_text );
70 my $md5 = pop( @decrypted_values );
71 my $omd5 = md5_hex( join( '', @decrypted_values ) ) || '';
72  
73 if ( $omd5 eq $md5 ) {
74 if ( wantarray ) {
75 return @decrypted_values;
76 }
77 else {
78 return join( ' ', @decrypted_values );
79 }
80 }
81 else {
82 $self->set_error( 'bad encryption' );
83 }
84  
85 } # END decrypt_cookie
86  
87 #-------------------------------------------------
88 # encrypt
89 #-------------------------------------------------
90 sub encrypt {
91 my ( $self, @to_encrypt ) = @_;
92  
93 local $^W = 0;
94 $self->set_error( undef );
95
96 my $c;
97 eval {
98 $c = new Crypt::CBC( {
99 'key' => $self->{options}{secret},
100 'cipher' => 'Blowfish',
101 'padding' => 'null',
102 } );
103 };
104 if ( $@ ) {
105 my $error = (
106 "Error building CBC object are your Crypt::CBC and"
107 . " Crypt::Blowfish up to date? Actual error: $@"
108 );
109  
110 $self->set_error( $error );
111 die $error;
112 }
113  
114 my $md5 = md5_hex( join( '', @to_encrypt ) );
115 push ( @to_encrypt, $md5 );
116
117 my $str = join( ':;:', @to_encrypt );
118 my $encd = $c->encrypt( $str );
119 my $c_text = MIME::Base64::encode( $encd, '' );
120  
121 $c->finish();
122
123 return( $c_text );
124
125 } # END encrypt
126  
127 #-------------------------------------------------
128 # set_error()
129 #-------------------------------------------------
130 sub set_error {
131 my $self = shift;
132 $self->{__error__} = shift;
133  
134 return $self->{__error__};
135 }
136  
137 #-------------------------------------------------
138 # get_error()
139 #-------------------------------------------------
140 sub get_error {
141 my $self = shift;
142 return $self->{__error__};
143 }
144  
145 # EOF
146 1;
147  
148 __END__