What an HMAC is
An HMAC (hash-based message authentication code) is a short tag computed from a message and a secret key. Anyone holding the key can compute the same tag and compare. If the tags match, the message came from someone with the key and was not changed on the way.
A plain hash cannot do this, because anyone can compute a hash. An HMAC mixes the secret into the computation, so without the key a valid tag cannot be made.
HMAC is defined in RFC 2104 and FIPS 198-1. It works with any hash function; HMAC-SHA256 is the common choice today.
How HMAC is computed
HMAC hashes the message twice, with the key mixed in each time through two fixed padding constants. This construction stays secure even with hash functions that would be weak if you simply hashed the key and the message together.
HMAC(K, m) = H( (K' xor opad) || H( (K' xor ipad) || m ) )
K' the key, padded with zeros to the hash's block size
(or hashed first if it is longer than the block)
ipad the byte 0x36 repeated, opad the byte 0x5c repeatedkey: sample-secret
message: {"event":"LOGIN"}
HMAC-SHA256 (hex):
418b27555f8a69cddcf7f6f8838c44c607e02e87d8ce3edad54fd6e8485cc915An empty key is allowed by the RFC but proves nothing, since anyone can use an empty key. This tool computes it, matching the standard, and warns you.
Where HMACs are used
HMACs are everywhere two parties share a secret and need to trust messages between them.
Webhook signatures
Most webhook senders sign each request body with HMAC-SHA256 and a shared secret, and put the tag in a header, often as hex with a prefix such as sha256=. The receiver recomputes the HMAC over the raw body and rejects the request if the tags differ. Many also sign a timestamp to stop replays.
HS256 JSON Web Tokens
A JWT with alg HS256 is signed with HMAC-SHA256 over the encoded header and payload. Everyone who can verify such a token can also create one, because they share the key. That is why HS256 suits tokens a single service issues and checks itself, and asymmetric algorithms such as RS256 suit tokens that many services verify.
RFC 7518 requires an HMAC key at least as long as the hash output: 256 bits for HS256, 512 for HS512.
Keycloak’s internal tokens
Each Keycloak realm has an HMAC key, from the hmac-generated key provider, used for tokens that only Keycloak itself issues and checks. Your applications never need it. They verify Keycloak’s access and ID tokens with the realm’s public keys from its JWKS endpoint, /realms/<realm>/protocol/openid-connect/certs.
One-time passwords
HOTP and TOTP codes are HMACs of a counter, truncated to six or eight digits. See the TOTP generator for the details.
Signed cookies and state values
Frameworks often sign session cookies or OAuth state values with an HMAC so the server can tell whether the browser changed them, without keeping them in a store.
What this tool does and does not do
- It computes HMAC-SHA256, HMAC-SHA384, HMAC-SHA512 and HMAC-SHA1 with Web Crypto, in your browser.
- It reads the key as UTF-8 text, hex or Base64, and the message as UTF-8 text.
- It shows the result in hex, Base64 or Base64url.
- It compares the result with a signature you paste, given in hex or Base64, with or without a
sha256=style prefix. - It does not take binary messages or files, and it does not know any particular service’s signing scheme. Some services sign a timestamp and body joined together; build that exact string first.
How to check a webhook signature
- Find the signing secret in the sender’s settings, and note whether it is shown as text, hex or Base64.
- Enter the secret and choose the matching key format.
- Paste the raw request body exactly as received, before any JSON parsing.
- Choose the hash the sender documents, usually HMAC-SHA256.
- Paste the signature header’s value into the compare field and read the result.
Use test secrets and sample bodies where you can. A real webhook secret lets anyone forge requests to your endpoint.
Why signatures fail to match
An HMAC changes completely if a single byte changes, so mismatches almost always come from feeding slightly different bytes.
| Cause | What happens | Fix |
|---|---|---|
| Body re-serialized | A framework parses the JSON and you hash JSON.stringify of it; spacing or key order differs | Hash the raw body bytes as received |
| Key format | The secret is hex or Base64 but was used as text, or the reverse | Decode the key the way the sender does |
| Wrong prefix handling | The header holds sha256= plus hex, compared with bare hex | Strip the prefix before comparing |
| Encoding | Hex compared with Base64 | Compare in one encoding |
| Extra data signed | The sender signs a timestamp and body together | Rebuild the exact signed string |
| Line endings | A body copied through a terminal gained CRLF | Use the bytes from the request, not a copy |
Choosing an HMAC key
Make the key random and at least as long as the hash output: 32 random bytes for HMAC-SHA256, 64 for HMAC-SHA512. Longer keys do not hurt but add nothing past the hash’s block size, because longer keys are hashed down first.
Do not use a password or a word as an HMAC key. A guessable key can be found offline from any message and its tag. The client secret generator makes suitable keys in hex or Base64url.
Security notes
- Compare tags in constant time on the server, for example with
crypto.timingSafeEqualin Node.js orhmac.compare_digestin Python. A normal string comparison can leak through timing how many leading bytes matched. - Verify before you parse. Check the HMAC on the raw body, then parse the JSON.
- Reject old requests. Include a timestamp in what is signed and refuse requests outside a short window, so captured requests cannot be replayed later.
- HMAC-SHA1 has no known practical attack as a MAC, but use HMAC-SHA256 for anything new.
- An HMAC is not encryption. The message is still readable by anyone who sees it.
Best practices
- Use a separate key per integration and per environment.
- Keep keys in a secrets manager and rotate them when people with access leave.
- Support two active keys during rotation, so senders can switch without downtime.
- Document the exact signed bytes, the hash, the key format and the header format.
- Prefer asymmetric signatures when many parties must verify but only one should sign.
Verifying an HMAC in your own code
The checks this page does by hand are a few lines on a server. The important parts are the raw body, the decoded key, and a constant-time comparison.
import { createHmac, timingSafeEqual } from 'node:crypto';
function verify(rawBody: Buffer, header: string, secret: string) {
const expected = createHmac('sha256', secret).update(rawBody).digest();
const given = Buffer.from(header.replace(/^sha256=/, ''), 'hex');
return given.length === expected.length && timingSafeEqual(given, expected);
}The length check comes first because timingSafeEqual throws on buffers of different lengths. Read the body as raw bytes; most web frameworks have an option to keep the raw body alongside the parsed JSON for exactly this reason.