File Coverage

blib/lib/WebService/Braintree/Digest.pm
Criterion Covered Total %
statement 24 54 44.4
branch 0 6 0.0
condition 0 3 0.0
subroutine 8 15 53.3
pod 0 6 0.0
total 32 84 38.1


line stmt bran cond sub pod time code
1             package WebService::Braintree::Digest;
2             $WebService::Braintree::Digest::VERSION = '0.92';
3 1     1   7 use strict;
  1         1  
  1         33  
4              
5 1     1   252 use Digest::HMAC_SHA1 qw(hmac_sha1 hmac_sha1_hex);
  1         3103  
  1         44  
6 1     1   233 use Digest::SHA1;
  1         385  
  1         35  
7 1     1   222 use Digest::SHA256;
  1         704  
  1         39  
8 1     1   7 use Digest::SHA qw(hmac_sha256_hex);
  1         2  
  1         34  
9 1     1   249 use WebService::Braintree::DigestSHA256;
  1         3  
  1         42  
10              
11 1     1   5 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS );
  1         2  
  1         41  
12 1     1   4 use Exporter;
  1         2  
  1         333  
13             our @ISA = qw(Exporter);
14             our @EXPORT = qw(hexdigest hexdigest_256);
15             our @EXPORT_OK = qw();
16              
17             sub algo_class {
18 0     0 0   my ($algo) = @_;
19 0 0         if ($algo eq "SHA-1") {
20 0           return "Digest::SHA1";
21             } else {
22 0           return "WebService::Braintree::DigestSHA256";
23             }
24             }
25              
26             sub hmac {
27 0     0 0   my($key, $algo) = @_;
28 0           return Digest::HMAC->new($key, algo_class($algo));
29             }
30              
31             sub hexdigest {
32 0     0 0   my($key, $data) = @_;
33 0           return _hexdigest($key, $data, "SHA-1");
34             }
35              
36             sub hexdigest_256 {
37 0     0 0   my($key, $data) = @_;
38 0           return _hexdigest($key, $data, "SHA-256");
39             }
40              
41             sub _hexdigest {
42 0     0     my ($key, $data, $algo) = @_;
43 0           my $digested_key = key_digest($algo, $key);
44 0           my $hmac = hmac($digested_key, $algo);
45 0           $hmac->add($data);
46 0           return $hmac->hexdigest;
47             }
48              
49             sub key_digest {
50 0     0 0   my ($alg, $key) = @_;
51 0           my $sha = Digest->new($alg);
52 0           $sha->add($key);
53 0           return $sha->digest;
54             }
55              
56             sub secure_compare {
57 0     0 0   my ($left, $right) = @_;
58              
59 0 0 0       if ((not defined($left)) || (not defined($right))) {
60 0           return 0;
61             }
62              
63 0           my @left_bytes = unpack("C*", $left);
64 0           my @right_bytes = unpack("C*", $right);
65              
66 0 0         if (scalar(@left_bytes) != scalar(@right_bytes)) {
67 0           return 0;
68             }
69              
70 0           my $result = 0;
71 0           for (my $i = 0; $i < scalar(@left_bytes); $i++) {
72 0           $result |= $left_bytes[$i] ^ $right_bytes[$i];
73             }
74 0           return $result == 0;
75             }
76              
77             1;