HMAC generator
Runs in your browserCreate HMAC-SHA256, HMAC-SHA512, HMAC-SHA1 or HMAC-MD5 from a message and key (text, hex or Base64). Output as hex, Base64 and Base64url, compare with an expected value.
- Hex
- Base64
- Base64url
- HEX (upper case)
Example: HMAC-SHA256 of “The quick brown fox jumps over the lazy dog” with key “key” is f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8.
A hash with a secret
An HMAC mixes a secret key into a hash function, so only someone with the key can produce the right value. Webhooks, JWTs with HS256, AWS request signing and many APIs use it to prove that a message comes from the right sender and wasn’t changed on the way. Unlike a plain hash, a sha256(secret + message) construction is not safe: SHA-256 allows length extension, HMAC doesn’t.
The same HMAC everywhere
Each line gives the example result above for its message and key.
printf '%s' "message" | openssl dgst -sha256 -hmac "key" # Base64 instead of hex printf '%s' "message" | openssl dgst -sha256 -hmac "key" -binary | base64 # key as hex printf '%s' "message" | openssl dgst -sha256 -mac HMAC -macopt hexkey:6b6579
import crypto from "node:crypto";
crypto.createHmac("sha256", "key").update("message").digest("hex");import hmac, hashlib hmac.new(b"key", b"message", hashlib.sha256).hexdigest()
echo hash_hmac('sha256', 'message', 'key');
echo base64_encode(hash_hmac('sha256', 'message', 'key', true));const k = await crypto.subtle.importKey("raw", new TextEncoder().encode("key"),
{ name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
const sig = await crypto.subtle.sign("HMAC", k, new TextEncoder().encode("message"));
[...new Uint8Array(sig)].map((b) => b.toString(16).padStart(2, "0")).join("");mac := hmac.New(sha256.New, []byte("key"))
mac.Write([]byte("message"))
fmt.Println(hex.EncodeToString(mac.Sum(nil)))Why two HMACs differ
echo "message" adds a line break. Use printf '%s' or echo -n.
A key shown as hex or Base64 usually has to be decoded first. Switch the key format above and compare.
The same 32 bytes look completely different as hex and as Base64. Compare with the format the other side sends; the compare field accepts any.
Hex is case-insensitive. Lower-case both before comparing, or compare bytes.
Questions about the HMAC generator
Can an HMAC be decrypted?
No. Like any hash it is one-way. You can only recompute it with the same key and message and compare.
How long should the key be?
At least as long as the hash output: 32 random bytes for HMAC-SHA256, 64 for HMAC-SHA512. Longer keys than the block size (64 bytes for SHA-256) are hashed first.
Is HMAC-SHA1 still safe?
For HMAC, yes: collisions in SHA-1 don’t break HMAC-SHA1. Prefer HMAC-SHA256 for anything new, but HMAC-SHA1 in Twilio or old APIs is not a vulnerability.