Crypt::Mac::HMAC(3)
π Crypt::Mac::HMAC(3) β Message authentication code HMAC, bestie edition π
okay so this is the Crypt::Mac::HMAC(3) page from the User Contributed Perl Documentation, and weβre gonna get you comfy with HMAC without any gatekeeping. no cap, itβs actually a vibe. π«Ά
β¨ NAME β¨
Crypt::Mac::HMAC β Message authentication code HMAC. Thatβs the name, thatβs the whole assignment. π
πΈ SYNOPSIS πΈ
okay bestie, hereβs the whole menu. Functional interface first, then the OO interface, then a file example because we love options. π
### Functional interface:
use Crypt::Mac::HMAC qw( hmac hmac_hex hmac_b64 hmac_b64u );
# calculate MAC from string/buffer
my $hmac_raw = hmac('SHA256', $key, 'data buffer');
my $hmac_hex = hmac_hex('SHA256', $key, 'data buffer');
my $hmac_b64 = hmac_b64('SHA256', $key, 'data buffer');
my $hmac_b64u = hmac_b64u('SHA256', $key, 'data buffer');
### OO interface:
use Crypt::Mac::HMAC;
my $d = Crypt::Mac::HMAC->new('SHA256', $key);
$d->add('any data');
my $result_hex = $d->hexmac; # finalizes the object
# for another output encoding use a fresh object (or clone before finalizing)
my $result_b64u = Crypt::Mac::HMAC->new('SHA256', $key)->add('any data')->b64umac;
# or MAC a file instead
my $file_result_raw = Crypt::Mac::HMAC->new('SHA256', $key)->addfile('filename.dat')->mac;
π¦ DESCRIPTION π¦
This module gives you an interface to the HMAC message authentication code (MAC) algorithm. Short, iconic, does exactly what it says on the label. β
π EXPORT π
Default behavior? Nothing exported. Literally nothing. By default you get zilch. π
Want selected functions? Say it with the import list:
use Crypt::Mac::HMAC qw( hmac hmac_hex hmac_b64 hmac_b64u );
Or grab all of them at once, main-character style:
use Crypt::Mac::HMAC ':all';
π FUNCTIONS π
hmac πΈ
It joins all the arguments into a single string and returns its HMAC message authentication code encoded as a binary string. Yes, raw bytes, no encoding glow-up. β¨
Data arguments for the functional helpers are converted to byte strings using Perlβs usual scalar stringification. Defined scalars, including numbers and string-overloaded objects, are accepted. undef is treated as an empty string and may emit Perlβs usual "uninitialized value" warning. The same rules apply to hmac_hex, hmac_b64, and hmac_b64u. π«Ά
my $hmac_raw = hmac($hash_name, $key, 'data buffer');
#or
my $hmac_raw = hmac($hash_name, $key, 'any data', 'more data', 'even more data');
# $hash_name ... [string] any <NAME> for which there is a Crypt::Digest::<NAME> module
# $key ......... [binary string] the key (octets/bytes)
hmac_hex π
It smushes all the arguments into one string, then returns its HMAC message authentication code encoded as a hexadecimal string. cute and readable. π
my $hmac_hex = hmac_hex($hash_name, $key, 'data buffer');
#or
my $hmac_hex = hmac_hex($hash_name, $key, 'any data', 'more data', 'even more data');
# $hash_name ... [string] any <NAME> for which there is a Crypt::Digest::<NAME> module
# $key ......... [binary string] the key (not hex!)
hmac_b64 π¦
It joins all the arguments into a single string and returns its HMAC message authentication code encoded as a Base64 string. Base64 bestie hours. πΈ
my $hmac_b64 = hmac_b64($hash_name, $key, 'data buffer');
#or
my $hmac_b64 = hmac_b64($hash_name, $key, 'any data', 'more data', 'even more data');
# $hash_name ... [string] any <NAME> for which there is a Crypt::Digest::<NAME> module
# $key ......... [binary string] the key (not Base64!)
hmac_b64u π
It joins all the arguments into a single string and returns its HMAC message authentication code encoded as a Base64 URL-safe string (see RFC 4648 section 5). URL-safe, so itβs ready for the linkified chaos. β¨
my $hmac_b64url = hmac_b64u($hash_name, $key, 'data buffer');
#or
my $hmac_b64url = hmac_b64u($hash_name, $key, 'any data', 'more data', 'even more data');
# $hash_name ... [string] any <NAME> for which there is a Crypt::Digest::<NAME> module
# $key ......... [binary string] the key (not Base64url!)
π METHODS π
Unless noted otherwise, assume $d is an existing MAC object created via new, like this:
my $d = Crypt::Mac::HMAC->new('SHA256', $key);
new π
Make a fresh MAC object with a hash name and a key:
my $d = Crypt::Mac::HMAC->new($hash_name, $key);
# $hash_name ... [string] one of 'SHA256', 'SHA384', 'SHA512', 'SHA1', 'SHA3_256', 'BLAKE2b_256',
# 'RIPEMD160', etc. - any <NAME> for which there is a Crypt::Digest::<NAME> module
# $key ......... [binary string] the key (any length - internally padded/hashed as per RFC 2104)
clone π§
Need a little twin? Hereβs the call:
$d->clone();
add πΈ
Appends data to the message. Returns the object itself (for chaining). Croaks if the object has already been finalized by mac, hexmac, b64mac, or b64umac. π¨
Each argument is converted to bytes using Perlβs usual scalar stringification. Defined scalars, including numbers and string-overloaded objects, are accepted. undef is treated as an empty string and may emit Perlβs usual "uninitialized value" warning.
$d->add('any data');
#or
$d->add('any data', 'more data', 'even more data');
addfile π
Reads the file content and appends it to the message. Returns the object itself (for chaining). Croaks if the object has already been finalized by mac, hexmac, b64mac, or b64umac. π¨
$d->addfile('filename.dat');
#or
my $filehandle = ...; # existing binary-mode filehandle
$d->addfile($filehandle);
mac π₯
Returns the binary MAC (raw bytes) and finalizes the object. After the first call to mac, hexmac, b64mac, or b64umac, later calls to add, addfile, or any MAC getter croak. so yeah, get your data in before you get the MAC. π
my $result_raw = $d->mac();
hexmac β
Returns the MAC encoded as a lowercase hexadecimal string and finalizes the object. After the first call to mac, hexmac, b64mac, or b64umac, later calls to add, addfile, or any MAC getter croak.
my $result_hex = $d->hexmac();
b64mac π
Returns the MAC encoded as a Base64 string with trailing = padding and finalizes the object. After the first call to mac, hexmac, b64mac, or b64umac, later calls to add, addfile, or any MAC getter croak.
my $result_b64 = $d->b64mac();
b64umac π
Returns the MAC encoded as a Base64 URL-safe string (no trailing =) and finalizes the object. After the first call to mac, hexmac, b64mac, or b64umac, later calls to add, addfile, or any MAC getter croak.
my $result_b64url = $d->b64umac();
π SEE ALSO π
okay byeee bestie β source/version: perl v5.42.0; date: 2026-05-11; manual: Crypt::Mac::HMAC(3). mwah ππ
π¦ where this page comes from
- opensuse-leap 16.0: perl-CryptX 0.89.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-CryptX 0.89.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-CryptX 0.89.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-CryptX 0.89.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms