line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Braintree::Digest; |
2
|
|
|
|
|
|
|
$WebService::Braintree::Digest::VERSION = '0.93'; |
3
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
263
|
use Digest::HMAC_SHA1 qw(hmac_sha1 hmac_sha1_hex); |
|
1
|
|
|
|
|
5289
|
|
|
1
|
|
|
|
|
92
|
|
6
|
1
|
|
|
1
|
|
424
|
use Digest::SHA1; |
|
1
|
|
|
|
|
755
|
|
|
1
|
|
|
|
|
60
|
|
7
|
1
|
|
|
1
|
|
364
|
use Digest::SHA256; |
|
1
|
|
|
|
|
1278
|
|
|
1
|
|
|
|
|
64
|
|
8
|
1
|
|
|
1
|
|
12
|
use Digest::SHA qw(hmac_sha256_hex); |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
66
|
|
9
|
1
|
|
|
1
|
|
464
|
use WebService::Braintree::DigestSHA256; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
77
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
9
|
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS ); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
81
|
|
12
|
1
|
|
|
1
|
|
12
|
use Exporter; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
648
|
|
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; |