How HMAC Works: HMAC-SHA256, Keys and Webhook Signatures
Last updated: · 6 min read
An HMAC (hash-based message authentication code) is a short tag that proves two things about a message: it has not been altered, and it was produced by someone who holds the shared secret key. The construction is defined in RFC 2104 and wraps an ordinary hash function such as SHA-256 around the key. Webhook signatures, signed API requests and HS256 JSON Web Tokens are all built on it. This guide covers the formula, why a plain hash of key plus message falls short, how long a key should be and the mistakes that most often break webhook verification. The values in the example are real output from the HMAC Generator, which runs on the Web Crypto API in your browser; neither the message nor the key is sent to a server.
Formula
HMAC(K, m) = H( (K' ⊕ opad) ‖ H( (K' ⊕ ipad) ‖ m ) ) H = hash function (SHA-256, SHA-1, SHA-384 or SHA-512) K' = key brought to the block size B: hashed with H if longer than B, padded with zero bytes if shorter ipad = 0x36 repeated B times, opad = 0x5c repeated B times B = 64 bytes (SHA-1, SHA-256), 128 bytes (SHA-384, SHA-512) Output = 20 bytes (SHA-1), 32 bytes (SHA-256), 48 bytes (SHA-384), 64 bytes (SHA-512)
The key is first fitted to the block size of the hash. In the inner pass it is XORed with ipad, placed in front of the message and hashed; in the outer pass it is XORed with opad, placed in front of the inner digest and hashed again. Because of the two nested passes, someone who sees a tag cannot extend the message and produce another valid tag without the key. The tool encodes the message and the key as UTF-8, computes the HMAC with the browser's SubtleCrypto.sign and shows the same bytes in two notations: hex (two characters per byte) and Base64.
What the inputs mean
- Message
- The text to authenticate. Spaces and line breaks are not trimmed, and every character changes the result. When checking a webhook, paste the raw request body exactly as it arrived.
- Secret key
- The key shared by both sides. The characters you type are used as UTF-8 text; a key written in hex or Base64 is not decoded, it is taken literally. It cannot be empty.
- Algorithm
- HMAC-SHA-256 (default), HMAC-SHA-1, HMAC-SHA-384 or HMAC-SHA-512. Pick whatever the other side's documentation specifies; the output length depends on it.
Worked example: HMAC-SHA-256 with the key "key", plus an RFC 4231 test vector
- Input: Message = The quick brown fox jumps over the lazy dog, Secret key = key, Algorithm = HMAC-SHA-256.
- The key "key" is three bytes (6b 65 79). SHA-256 has a 64-byte block, so 61 zero bytes are appended; one copy is then XORed with ipad (0x36) and another with opad (0x5c).
- The inner digest is SHA-256((K' ⊕ ipad) ‖ message) and the outer digest is SHA-256((K' ⊕ opad) ‖ inner digest). The result is 32 bytes.
- The tool prints three lines: HMAC-SHA-256, then Hex: f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8 and Base64: 97yD9DBThCSxMpjmqm+xQ+9NWaFJRhdZl0edvC0aPNg=
- Both lines show the same 32 bytes: 64 characters in hex, 44 in Base64. Which one you compare against depends on the provider you are integrating with.
- Capitalise one letter of the key (Key) and the hex value becomes 6c54a3f1708e3ed2636ed2ff9193f0be7b26889cee1052e11a7c17b6674544de. A single changed key character alters the whole tag.
- Switch the algorithm to HMAC-SHA-1 and you get de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9 (20 bytes, 40 characters).
- A check against the standard: test case 2 of RFC 4231 uses the key Jefe and the message what do ya want for nothing? The tool returns 5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843, the value printed in the RFC.
Result: HMAC-SHA-256("key", "The quick brown fox jumps over the lazy dog") = f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8, or 97yD9DBThCSxMpjmqm+xQ+9NWaFJRhdZl0edvC0aPNg= in Base64.
How to read the result
Webhook verification is where most developers meet HMAC. The sender signs the request body with a shared secret and puts the result in a header; you repeat the calculation and compare. GitHub, for example, sends the tag in X-Hub-Signature-256 as sha256= followed by hex. Three rules keep verification sound: compute the HMAC over the raw bytes of the request, compare with a constant-time function, and if the provider includes a timestamp, reject old requests so a captured one cannot be replayed. An HMAC does not hide the message; it only authenticates it. The tool never transmits your key, but as a general habit do not paste production secrets into any web page. Use a test secret when you experiment.
Common mistakes
- Using SHA-256(key + message) instead of HMAC. With SHA-1, SHA-256 and SHA-512, anyone who knows the digest and the length can append data and compute a valid digest without the key (a length-extension attack). The nested structure of HMAC prevents this.
- Signing re-serialised JSON. Once a framework has parsed the body and written it out again, whitespace, key order or escaping may differ and the signature no longer matches. Always use the raw body as received.
- Comparing signatures with ==. An ordinary comparison stops at the first differing byte and can leak timing information; use crypto.timingSafeEqual in Node.js, hmac.compare_digest in Python or hash_equals in PHP.
- Assuming a hex or Base64 key is decoded. Some providers sign with the decoded bytes of the secret; this tool reads the key as plain text, so the result will differ in that case.
- Missing an invisible trailing newline or space. The input is not trimmed, and a single line break picked up while copying produces a completely different tag.
- Mixing up hex and Base64, or forgetting a prefix such as sha256=. Make sure both values are in the same notation before comparing.
- Choosing a short or guessable key. An attacker who captures one message and its tag can test weak keys offline.
Frequently asked questions
What is the difference between HMAC and a plain SHA-256 hash?
Anyone can compute a SHA-256 hash, so it only shows that data is unchanged. Computing an HMAC requires the secret key, so it also shows that the message came from someone who holds that key.
Does HMAC encrypt the message?
No. It is an authentication tag: the message stays readable and cannot be recovered from the tag. If you need confidentiality, add encryption such as TLS or AES-GCM.
How long should an HMAC key be?
RFC 2104 recommends at least the output length of the hash, which is 32 bytes for SHA-256. Keys longer than the block size are hashed first, so extra length adds nothing. What matters most is that the key comes from a cryptographically secure random generator.
Is HMAC-SHA1 still safe to use?
The known collision attacks on SHA-1 do not directly break the HMAC construction, and HMAC-SHA-1 is not considered broken in practice. New designs should still use HMAC-SHA-256 or stronger; the SHA-1 option is there for compatibility with older systems.
Is an HMAC a digital signature?
No. HMAC is symmetric: whoever can verify a tag can also create one with the same key. If you must prove to a third party who signed something, use a public-key signature such as RSA or ECDSA.
Are my message and key sent to a server?
No. The calculation runs on your device through the Web Crypto API, which browsers only expose on secure (https) pages.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.