File Coverage

lib/WebService/Braintree/Digest.pm
Criterion Covered Total %
statement 26 56 46.4
branch 0 6 0.0
condition 0 3 0.0
subroutine 9 16 56.2
pod 0 6 0.0
total 35 87 40.2


line stmt bran cond sub pod time code
1             package WebService::Braintree::Digest;
2             $WebService::Braintree::Digest::VERSION = '0.94';
3 20     20   438 use 5.010_001;
  20         64  
4 20     20   105 use strictures 1;
  20         158  
  20         810  
5              
6 20     20   7151 use Digest::HMAC_SHA1 qw(hmac_sha1 hmac_sha1_hex);
  20         63102  
  20         987  
7 20     20   4838 use Digest::SHA1;
  20         8097  
  20         867  
8 20     20   4738 use Digest::SHA256;
  20         14650  
  20         813  
9 20     20   126 use Digest::SHA qw(hmac_sha256_hex);
  20         37  
  20         722  
10 20     20   4853 use WebService::Braintree::DigestSHA256;
  20         47  
  20         881  
11              
12 20     20   112 use vars qw(@ISA @EXPORT @EXPORT_OK);
  20         40  
  20         801  
13 20     20   96 use Exporter;
  20         34  
  20         7067  
14             our @ISA = qw(Exporter);
15             our @EXPORT = qw(hexdigest hexdigest_256);
16             our @EXPORT_OK = qw();
17              
18             sub hexdigest {
19 0     0 0   my($key, $data) = @_;
20 0           return _hexdigest($key, $data, "SHA-1");
21             }
22              
23             sub hexdigest_256 {
24 0     0 0   my($key, $data) = @_;
25 0           return _hexdigest($key, $data, "SHA-256");
26             }
27              
28             # Methods below here are only used within this class.
29              
30             sub algo_class {
31 0     0 0   my ($algo) = @_;
32 0 0         if ($algo eq "SHA-1") {
33 0           return "Digest::SHA1";
34             } else {
35 0           return "WebService::Braintree::DigestSHA256";
36             }
37             }
38              
39             sub hmac {
40 0     0 0   my($key, $algo) = @_;
41 0           return Digest::HMAC->new($key, algo_class($algo));
42             }
43              
44             sub _hexdigest {
45 0     0     my ($key, $data, $algo) = @_;
46 0           my $digested_key = key_digest($algo, $key);
47 0           my $hmac = hmac($digested_key, $algo);
48 0           $hmac->add($data);
49 0           return $hmac->hexdigest;
50             }
51              
52             sub key_digest {
53 0     0 0   my ($alg, $key) = @_;
54 0           my $sha = Digest->new($alg);
55 0           $sha->add($key);
56 0           return $sha->digest;
57             }
58              
59             # UNUSED?
60             sub secure_compare {
61 0     0 0   my ($left, $right) = @_;
62              
63 0 0 0       if ((not defined($left)) || (not defined($right))) {
64 0           return 0;
65             }
66              
67 0           my @left_bytes = unpack("C*", $left);
68 0           my @right_bytes = unpack("C*", $right);
69              
70 0 0         if (scalar(@left_bytes) != scalar(@right_bytes)) {
71 0           return 0;
72             }
73              
74 0           my $result = 0;
75 0           for (my $i = 0; $i < scalar(@left_bytes); $i++) {
76 0           $result |= $left_bytes[$i] ^ $right_bytes[$i];
77             }
78 0           return $result == 0;
79             }
80              
81             1;
82             __END__