Digest::HMAC - Keyed-Hashing for Message Authentication
| Use Case | Command | Description |
|---|---|---|
| Functional HMAC ๐งช | hmac($data, $key, \&myhash) | Compute HMAC with a given hash function |
| Functional HMAC (hex) ๐งช | hmac_hex($data, $key, \&myhash) | Compute HMAC and return hex string |
| OO style instantiation ๐งฑ | Digest::HMAC->new($key, "Digest::MyHash") | Create HMAC object with key and digest class |
| Add data โ | $hmac->add($data) | Feed data to the HMAC object |
| Add file data ๐ | $hmac->addfile(*FILE) | Feed data from a filehandle |
| Binary digest ๐ | $hmac->digest | Get the raw binary digest |
| Hex digest ๐ข | $hmac->hexdigest | Get the digest as a hex string |
| Base64 digest ๐ฆ | $hmac->b64digest | Get the digest as a Base64 string |
| Optional blocksize ๐งฉ | Digest::HMAC->new($key, "Digest::Foo", $blocksize) | Specify custom compression block size |
# Functional style
use Digest::HMAC qw(hmac hmac_hex);
$digest = hmac($data, $key, \&myhash);
print hmac_hex($data, $key, \&myhash);
# OO style
use Digest::HMAC;
$hmac = Digest::HMAC->new($key, "Digest::MyHash");
$hmac->add($data);
$hmac->addfile(*FILE);
$digest = $hmac->digest;
$digest = $hmac->hexdigest;
$digest = $hmac->b64digest;
HMAC is used for message integrity checks ๐ between two parties that share a secret key ๐, and works in combination with some other Digest algorithm, usually MD5 or SHA-1. The HMAC mechanism is described in RFC 2104.
HMAC follow the common "Digest::" interface, but the constructor takes the secret key and the name of some other simple "Digest::" as argument.
The hmac() and hmac_hex() functions and the Digest::HMAC->new() constructor takes an optional ๐ฆ $blocksize argument as well. The HMAC algorithm assumes the digester to hash by iterating a basic compression function on blocks of data and the $blocksize should match the byte-length of such blocks.
The default $blocksize is 64 which is suitable for the MD5 and SHA-1 digest functions. For stronger algorithms the blocksize probably needs to be increased.
Andrew Rodland <arodland AT cpan.org>
Graham Barr <gbarr AT ti.com>, Gisle Aas <gisle AT aas.no>
Generated by phpman v4.9.25-25-g40dbf62 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-17 04:58 @216.73.216.114
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format