| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Crypt::Mac::HMAC; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! |
|
4
|
|
|
|
|
|
|
|
|
5
|
5
|
|
|
5
|
|
328374
|
use strict; |
|
|
5
|
|
|
|
|
10
|
|
|
|
5
|
|
|
|
|
173
|
|
|
6
|
5
|
|
|
5
|
|
57
|
use warnings; |
|
|
5
|
|
|
|
|
12
|
|
|
|
5
|
|
|
|
|
442
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.089'; |
|
8
|
|
|
|
|
|
|
|
|
9
|
5
|
|
|
5
|
|
33
|
use base qw(Crypt::Mac Exporter); |
|
|
5
|
|
|
|
|
7
|
|
|
|
5
|
|
|
|
|
2048
|
|
|
10
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( all => [qw( hmac hmac_hex hmac_b64 hmac_b64u )] ); |
|
11
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
|
12
|
|
|
|
|
|
|
our @EXPORT = qw(); |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
1; |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=pod |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 NAME |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Crypt::Mac::HMAC - Message authentication code HMAC |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
### Functional interface: |
|
25
|
|
|
|
|
|
|
use Crypt::Mac::HMAC qw( hmac hmac_hex hmac_b64 hmac_b64u ); |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# calculate MAC from string/buffer |
|
28
|
|
|
|
|
|
|
my $hmac_raw = hmac('SHA256', $key, 'data buffer'); |
|
29
|
|
|
|
|
|
|
my $hmac_hex = hmac_hex('SHA256', $key, 'data buffer'); |
|
30
|
|
|
|
|
|
|
my $hmac_b64 = hmac_b64('SHA256', $key, 'data buffer'); |
|
31
|
|
|
|
|
|
|
my $hmac_b64u = hmac_b64u('SHA256', $key, 'data buffer'); |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
### OO interface: |
|
34
|
|
|
|
|
|
|
use Crypt::Mac::HMAC; |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
my $d = Crypt::Mac::HMAC->new('SHA256', $key); |
|
37
|
|
|
|
|
|
|
$d->add('any data'); |
|
38
|
|
|
|
|
|
|
my $result_hex = $d->hexmac; # finalizes the object |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# for another output encoding use a fresh object (or clone before finalizing) |
|
41
|
|
|
|
|
|
|
my $result_b64u = Crypt::Mac::HMAC->new('SHA256', $key)->add('any data')->b64umac; |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# or MAC a file instead |
|
44
|
|
|
|
|
|
|
my $file_result_raw = Crypt::Mac::HMAC->new('SHA256', $key)->addfile('filename.dat')->mac; |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Provides an interface to the HMAC message authentication code (MAC) algorithm. |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 EXPORT |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
Nothing is exported by default. |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
You can export selected functions: |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
use Crypt::Mac::HMAC qw( hmac hmac_hex hmac_b64 hmac_b64u ); |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
Or all of them at once: |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
use Crypt::Mac::HMAC ':all'; |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head2 hmac |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Joins all arguments into a single string and returns its HMAC message authentication code encoded as a binary string. |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Data arguments for the functional helpers are converted to byte strings using |
|
69
|
|
|
|
|
|
|
Perl's usual scalar stringification. Defined scalars, including numbers and |
|
70
|
|
|
|
|
|
|
string-overloaded objects, are accepted. C is treated as an empty |
|
71
|
|
|
|
|
|
|
string and may emit Perl's usual "uninitialized value" warning. The same |
|
72
|
|
|
|
|
|
|
rules apply to C, C, and |
|
73
|
|
|
|
|
|
|
C. |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
my $hmac_raw = hmac($hash_name, $key, 'data buffer'); |
|
76
|
|
|
|
|
|
|
#or |
|
77
|
|
|
|
|
|
|
my $hmac_raw = hmac($hash_name, $key, 'any data', 'more data', 'even more data'); |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# $hash_name ... [string] any for which there is a Crypt::Digest:: module |
|
80
|
|
|
|
|
|
|
# $key ......... [binary string] the key (octets/bytes) |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head2 hmac_hex |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Joins all arguments into a single string and returns its HMAC message authentication code encoded as a hexadecimal string. |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $hmac_hex = hmac_hex($hash_name, $key, 'data buffer'); |
|
87
|
|
|
|
|
|
|
#or |
|
88
|
|
|
|
|
|
|
my $hmac_hex = hmac_hex($hash_name, $key, 'any data', 'more data', 'even more data'); |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# $hash_name ... [string] any for which there is a Crypt::Digest:: module |
|
91
|
|
|
|
|
|
|
# $key ......... [binary string] the key (not hex!) |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 hmac_b64 |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Joins all arguments into a single string and returns its HMAC message authentication code encoded as a Base64 string. |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
my $hmac_b64 = hmac_b64($hash_name, $key, 'data buffer'); |
|
98
|
|
|
|
|
|
|
#or |
|
99
|
|
|
|
|
|
|
my $hmac_b64 = hmac_b64($hash_name, $key, 'any data', 'more data', 'even more data'); |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# $hash_name ... [string] any for which there is a Crypt::Digest:: module |
|
102
|
|
|
|
|
|
|
# $key ......... [binary string] the key (not Base64!) |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 hmac_b64u |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Joins all arguments into a single string and returns its HMAC message authentication code encoded as a Base64 URL-safe string (see RFC 4648 section 5). |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
my $hmac_b64url = hmac_b64u($hash_name, $key, 'data buffer'); |
|
109
|
|
|
|
|
|
|
#or |
|
110
|
|
|
|
|
|
|
my $hmac_b64url = hmac_b64u($hash_name, $key, 'any data', 'more data', 'even more data'); |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# $hash_name ... [string] any for which there is a Crypt::Digest:: module |
|
113
|
|
|
|
|
|
|
# $key ......... [binary string] the key (not Base64url!) |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 METHODS |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Unless noted otherwise, assume C<$d> is an existing MAC object created via |
|
118
|
|
|
|
|
|
|
C, for example: |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
my $d = Crypt::Mac::HMAC->new('SHA256', $key); |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 new |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
my $d = Crypt::Mac::HMAC->new($hash_name, $key); |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# $hash_name ... [string] one of 'SHA256', 'SHA384', 'SHA512', 'SHA1', 'SHA3_256', 'BLAKE2b_256', |
|
127
|
|
|
|
|
|
|
# 'RIPEMD160', etc. - any for which there is a Crypt::Digest:: module |
|
128
|
|
|
|
|
|
|
# $key ......... [binary string] the key (any length - internally padded/hashed as per RFC 2104) |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head2 clone |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
$d->clone(); |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 add |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Appends data to the message. Returns the object itself (for chaining). |
|
137
|
|
|
|
|
|
|
Croaks if the object has already been finalized by C, C, |
|
138
|
|
|
|
|
|
|
C, or C. |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Each argument is converted to bytes using Perl's usual scalar stringification. |
|
141
|
|
|
|
|
|
|
Defined scalars, including numbers and string-overloaded objects, are |
|
142
|
|
|
|
|
|
|
accepted. C is treated as an empty string and may emit Perl's usual |
|
143
|
|
|
|
|
|
|
"uninitialized value" warning. |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
$d->add('any data'); |
|
146
|
|
|
|
|
|
|
#or |
|
147
|
|
|
|
|
|
|
$d->add('any data', 'more data', 'even more data'); |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 addfile |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Reads the file content and appends it to the message. Returns the object itself |
|
152
|
|
|
|
|
|
|
(for chaining). Croaks if the object has already been finalized by C, |
|
153
|
|
|
|
|
|
|
C, C, or C. |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
$d->addfile('filename.dat'); |
|
156
|
|
|
|
|
|
|
#or |
|
157
|
|
|
|
|
|
|
my $filehandle = ...; # existing binary-mode filehandle |
|
158
|
|
|
|
|
|
|
$d->addfile($filehandle); |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head2 mac |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Returns the binary MAC (raw bytes) and finalizes the object. After the first |
|
163
|
|
|
|
|
|
|
call to C, C, C, or C, later calls to C, |
|
164
|
|
|
|
|
|
|
C, or any MAC getter croak. |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
my $result_raw = $d->mac(); |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head2 hexmac |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Returns the MAC encoded as a lowercase hexadecimal string and finalizes the |
|
171
|
|
|
|
|
|
|
object. After the first call to C, C, C, or C, |
|
172
|
|
|
|
|
|
|
later calls to C, C, or any MAC getter croak. |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
my $result_hex = $d->hexmac(); |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head2 b64mac |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Returns the MAC encoded as a Base64 string with trailing C<=> padding and |
|
179
|
|
|
|
|
|
|
finalizes the object. After the first call to C, C, C, or |
|
180
|
|
|
|
|
|
|
C, later calls to C, C, or any MAC getter croak. |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
my $result_b64 = $d->b64mac(); |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head2 b64umac |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Returns the MAC encoded as a Base64 URL-safe string (no trailing C<=>) and |
|
187
|
|
|
|
|
|
|
finalizes the object. After the first call to C, C, C, or |
|
188
|
|
|
|
|
|
|
C, later calls to C, C, or any MAC getter croak. |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
my $result_b64url = $d->b64umac(); |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=over |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=item * L |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=item * L |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=item * L |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=back |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=cut |