1Crypt::Mac::HMAC(3)   User Contributed Perl Documentation  Crypt::Mac::HMAC(3)
2
3
4

NAME

6       Crypt::Mac::HMAC - Message authentication code HMAC
7

SYNOPSIS

9          ### Functional interface:
10          use Crypt::Mac::HMAC qw( hmac hmac_hex );
11
12          # calculate MAC from string/buffer
13          $hmac_raw  = hmac('SHA256', $key, 'data buffer');
14          $hmac_hex  = hmac_hex('SHA256', $key, 'data buffer');
15          $hmac_b64  = hmac_b64('SHA256', $key, 'data buffer');
16          $hmac_b64u = hmac_b64u('SHA256', $key, 'data buffer');
17
18          ### OO interface:
19          use Crypt::Mac::HMAC;
20
21          $d = Crypt::Mac::HMAC->new('SHA256', $key);
22          $d->add('any data');
23          $d->addfile('filename.dat');
24          $d->addfile(*FILEHANDLE);
25          $result_raw  = $d->mac;     # raw bytes
26          $result_hex  = $d->hexmac;  # hexadecimal form
27          $result_b64  = $d->b64mac;  # Base64 form
28          $result_b64u = $d->b64umac; # Base64 URL Safe form
29

DESCRIPTION

31       Provides an interface to the HMAC message authentication code (MAC)
32       algorithm.
33

EXPORT

35       Nothing is exported by default.
36
37       You can export selected functions:
38
39         use Crypt::Mac::HMAC qw(hmac hmac_hex );
40
41       Or all of them at once:
42
43         use Crypt::Mac::HMAC ':all';
44

FUNCTIONS

46   hmac
47       Logically joins all arguments into a single string, and returns its
48       HMAC message authentication code encoded as a binary string.
49
50        $hmac_raw = hmac($hash_name, $key, 'data buffer');
51        #or
52        $hmac_raw = hmac($hash_name, $key, 'any data', 'more data', 'even more data');
53
54        # $hash_name ... any <NAME> for which there exists Crypt::Digest::<NAME>
55        # $key ......... the key (octets/bytes)
56
57   hmac_hex
58       Logically joins all arguments into a single string, and returns its
59       HMAC message authentication code encoded as a hexadecimal string.
60
61        $hmac_hex = hmac_hex($hash_name, $key, 'data buffer');
62        #or
63        $hmac_hex = hmac_hex($hash_name, $key, 'any data', 'more data', 'even more data');
64
65        # $hash_name ... any <NAME> for which there exists Crypt::Digest::<NAME>
66        # $key ......... the key (octets/bytes, not hex!)
67
68   hmac_b64
69       Logically joins all arguments into a single string, and returns its
70       HMAC message authentication code encoded as a Base64 string.
71
72        $hmac_b64 = hmac_b64($hash_name, $key, 'data buffer');
73        #or
74        $hmac_b64 = hmac_b64($hash_name, $key, 'any data', 'more data', 'even more data');
75
76        # $hash_name ... any <NAME> for which there exists Crypt::Digest::<NAME>
77        # $key ......... the key (octets/bytes, not Base64!)
78
79   hmac_b64u
80       Logically joins all arguments into a single string, and returns its
81       HMAC message authentication code encoded as a Base64 URL Safe string
82       (see RFC 4648 section 5).
83
84        $hmac_b64url = hmac_b64u($hash_name, $key, 'data buffer');
85        #or
86        $hmac_b64url = hmac_b64u($hash_name, $key, 'any data', 'more data', 'even more data');
87
88        # $hash_name ... any <NAME> for which there exists Crypt::Digest::<NAME>
89        # $key ......... the key (octets/bytes, not Base64url!)
90

METHODS

92   new
93        $d = Crypt::Mac::HMAC->new($hash_name, $key);
94
95        # $hash_name ... any <NAME> for which there exists Crypt::Digest::<NAME>
96        # $key ......... the key (octets/bytes)
97
98   clone
99        $d->clone();
100
101   reset
102        $d->reset();
103
104   add
105        $d->add('any data');
106        #or
107        $d->add('any data', 'more data', 'even more data');
108
109   addfile
110        $d->addfile('filename.dat');
111        #or
112        $d->addfile(*FILEHANDLE);
113
114   mac
115        $result_raw = $d->mac();
116
117   hexmac
118        $result_hex = $d->hexmac();
119
120   b64mac
121        $result_b64 = $d->b64mac();
122
123   b64umac
124        $result_b64url = $d->b64umac();
125

SEE ALSO

127       •   CryptX
128
129       •   <https://en.wikipedia.org/wiki/Hmac>
130
131       •   <https://tools.ietf.org/html/rfc2104>
132
133
134
135perl v5.34.0                      2022-02-14               Crypt::Mac::HMAC(3)
Impressum