File Coverage

blib/lib/Business/OnlinePayment/StoredTransaction/Unstore.pm
Criterion Covered Total %
statement 53 54 98.1
branch 7 14 50.0
condition 1 3 33.3
subroutine 10 10 100.0
pod 0 2 0.0
total 71 83 85.5


line stmt bran cond sub pod time code
1             package Business::OnlinePayment::StoredTransaction::Unstore;
2              
3 1     1   4099 use 5.008004;
  1         5  
  1         38  
4 1     1   6 use strict;
  1         1  
  1         29  
5 1     1   5 use warnings;
  1         2  
  1         31  
6 1     1   4 use Carp;
  1         2  
  1         57  
7 1     1   6 use Crypt::OpenSSL::RSA;
  1         2  
  1         26  
8 1     1   5 use Crypt::CBC;
  1         1  
  1         17  
9 1     1   4 use Storable;
  1         13  
  1         41  
10 1     1   4 use MIME::Base64;
  1         2  
  1         500  
11              
12             our @ISA = qw();
13              
14             our @EXPORT_OK = ();
15              
16             our @EXPORT = ();
17              
18             our $VERSION = '0.01';
19              
20             #takes private RSA key and string returned by StoredTransaction->authorization
21             #takes arguments (private_key => $privkey, authorization => $auth)
22             sub new {
23 1     1 0 312 my $invocant = shift;
24 1   33     10 my $class = ref($invocant) || $invocant;
25 1         4 my $self = {
26             @_,
27             };
28 1         4 bless $self, $class;
29              
30 1 50       5 croak "missing 'private_key'" unless $self->{'private_key'};
31 1         4 my $privkey = $self->{'private_key'};
32 1 50       3 croak "missing 'authorization'" unless $self->{'authorization'};
33 1 50       8 croak "bad authorization" unless $self->{'authorization'} =~ /:/;
34 1         4 my ($seckey, $ciphertext) = split /:/, $self->{'authorization'};
35 1         5 $seckey = decode_base64($seckey);
36 1         4 $ciphertext = decode_base64($ciphertext);
37 1         1 my $rsa_priv;
38 1         2 eval {$rsa_priv = Crypt::OpenSSL::RSA->new_private_key($privkey)};
  1         40  
39 1 50       5 croak $@ if $@;
40 1         2 eval {$seckey = $rsa_priv->decrypt($seckey)};
  1         791  
41 1 50       5 croak $@ if $@;
42 1         7 my $cipher = Crypt::CBC->new( {'key' => $seckey,
43             'cipher' => 'Blowfish',
44             });
45 1         77 my $plaintext = $cipher->decrypt($ciphertext);
46 1 50       399 croak "no plaintext" unless $plaintext;
47 1         4 my $data = Storable::thaw($plaintext);
48 1         18 $self->{data} = $data;
49 1         13 return $self;
50             }
51              
52             #returns hash containing content or single value from hash if given a key
53             sub get {
54 1     1 0 2 my $self = shift;
55 1         3 my $key = shift;
56 1         1 my %data = %{$self->{data}};
  1         9  
57 1 50       9 return $data{$key} if defined $key;
58 0           return %data;
59             }
60              
61             1;
62             __END__