# man > Crypt::Mac::HMAC

---
type: CommandReference
command: Crypt::Mac::HMAC
mode: perldoc
section: 3pm
source: perldoc
---

## Quick Reference
- `hmac('SHA256', $key, 'data buffer')` — raw binary HMAC
- `hmac_hex('SHA256', $key, 'data buffer')` — hex string HMAC
- `hmac_b64('SHA256', $key, 'data buffer')` — Base64 HMAC
- `hmac_b64u('SHA256', $key, 'data buffer')` — Base64 URL Safe HMAC
- `$d = Crypt::Mac::HMAC->new('SHA256', $key); $d->add('data'); $d->mac;` — OO interface
- `$d->addfile('filename.dat'); $d->hexmac;` — process file, get hex MAC

## Name
Message authentication code HMAC

## Synopsis
perl
use Crypt::Mac::HMAC qw( hmac hmac_hex );

$raw  = hmac('SHA256', $key, 'data buffer');
$hex  = hmac_hex('SHA256', $key, 'data buffer');
$b64  = hmac_b64('SHA256', $key, 'data buffer');
$b64u = hmac_b64u('SHA256', $key, 'data buffer');

# OO interface
$d = Crypt::Mac::HMAC->new('SHA256', $key);
$d->add('any data');
$d->addfile('filename.dat');
$result_raw  = $d->mac;
$result_hex  = $d->hexmac;
$result_b64  = $d->b64mac;
$result_b64u = $d->b64umac;
## Functions and Methods
- `hmac($hash, $key, $data, ...)` — compute HMAC, return raw bytes
- `hmac_hex($hash, $key, $data, ...)` — same as `hmac`, output hex string
- `hmac_b64($hash, $key, $data, ...)` — same as `hmac`, output Base64 string
- `hmac_b64u($hash, $key, $data, ...)` — same as `hmac`, output Base64 URL Safe string (RFC 4648 sec 5)
- `new($hash_name, $key)` — create a new HMAC object; `$hash_name` corresponds to any [Crypt::Digest](https://perldoc.perl.org/Crypt::Digest) implementation
- `clone()` — return a copy of the object
- `reset()` — reset the state (discard added data)
- `add($data, ...)` — append data to the MAC computation
- `addfile($filename_or_handle)` — append file contents
- `mac()` — finalize and return raw MAC
- `hexmac()` — finalize and return hex‑encoded MAC
- `b64mac()` — finalize and return Base64‑encoded MAC
- `b64umac()` — finalize and return Base64 URL Safe‑encoded MAC

## Examples
Functional style:
perl
use Crypt::Mac::HMAC qw(hmac_hex);
my $key = 'secret';
my $mac = hmac_hex('SHA256', $key, 'message');
print "$mac\n";
Object‑oriented style:
perl
use Crypt::Mac::HMAC;
my $d = Crypt::Mac::HMAC->new('SHA256', 'secret');
$d->add('Hello ');
$d->add('World');
my $mac = $d->hexmac;
print "$mac\n";
## See Also
- [CryptX](https://perldoc.perl.org/CryptX)
- [HMAC on Wikipedia](https://en.wikipedia.org/wiki/Hmac)
- [RFC 2104](https://tools.ietf.org/html/rfc2104)