line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Digest::HMAC; |
2
|
|
|
|
|
|
|
our $VERSION = '1.04'; # VERSION |
3
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:ARODLAND'; # AUTHORITY |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
341
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# OO interface |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new |
10
|
|
|
|
|
|
|
{ |
11
|
14
|
|
|
14
|
0
|
26
|
my($class, $key, $hasher, $block_size) = @_; |
12
|
14
|
|
50
|
|
|
23
|
$block_size ||= 64; |
13
|
14
|
100
|
|
|
|
31
|
$key = $hasher->new->add($key)->digest if length($key) > $block_size; |
14
|
|
|
|
|
|
|
|
15
|
14
|
|
|
|
|
44
|
my $self = bless {}, $class; |
16
|
14
|
|
|
|
|
38
|
$self->{k_ipad} = $key ^ (chr(0x36) x $block_size); |
17
|
14
|
|
|
|
|
23
|
$self->{k_opad} = $key ^ (chr(0x5c) x $block_size); |
18
|
14
|
|
|
|
|
38
|
$self->{hasher} = $hasher->new->add($self->{k_ipad}); |
19
|
14
|
|
|
|
|
82
|
$self; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub reset |
23
|
|
|
|
|
|
|
{ |
24
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
25
|
0
|
|
|
|
|
0
|
$self->{hasher}->reset->add($self->{k_ipad}); |
26
|
0
|
|
|
|
|
0
|
$self; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
14
|
|
|
14
|
0
|
39
|
sub add { my $self = shift; $self->{hasher}->add(@_); $self; } |
|
14
|
|
|
|
|
32
|
|
|
14
|
|
|
|
|
26
|
|
30
|
0
|
|
|
0
|
0
|
0
|
sub addfile { my $self = shift; $self->{hasher}->addfile(@_); $self; } |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _digest |
33
|
|
|
|
|
|
|
{ |
34
|
14
|
|
|
14
|
|
15
|
my $self = shift; |
35
|
14
|
|
|
|
|
38
|
my $inner_digest = $self->{hasher}->digest; |
36
|
14
|
|
|
|
|
38
|
$self->{hasher}->reset->add($self->{k_opad}, $inner_digest); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
14
|
|
|
14
|
0
|
51
|
sub digest { shift->_digest->digest; } |
40
|
0
|
|
|
0
|
0
|
0
|
sub hexdigest { shift->_digest->hexdigest; } |
41
|
0
|
|
|
0
|
0
|
0
|
sub b64digest { shift->_digest->b64digest; } |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Functional interface |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
require Exporter; |
47
|
|
|
|
|
|
|
*import = \&Exporter::import; |
48
|
1
|
|
|
1
|
|
6
|
use vars qw(@EXPORT_OK); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
160
|
|
49
|
|
|
|
|
|
|
@EXPORT_OK = qw(hmac hmac_hex); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub hmac |
52
|
|
|
|
|
|
|
{ |
53
|
14
|
|
|
14
|
0
|
21
|
my($data, $key, $hash_func, $block_size) = @_; |
54
|
14
|
|
50
|
|
|
23
|
$block_size ||= 64; |
55
|
14
|
100
|
|
|
|
27
|
$key = &$hash_func($key) if length($key) > $block_size; |
56
|
|
|
|
|
|
|
|
57
|
14
|
|
|
|
|
23
|
my $k_ipad = $key ^ (chr(0x36) x $block_size); |
58
|
14
|
|
|
|
|
19
|
my $k_opad = $key ^ (chr(0x5c) x $block_size); |
59
|
|
|
|
|
|
|
|
60
|
14
|
|
|
|
|
84
|
&$hash_func($k_opad, &$hash_func($k_ipad, $data)); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
0
|
0
|
|
sub hmac_hex { unpack("H*", &hmac); } |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
1; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
__END__ |