File Coverage

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