What is HMAC? Keyed Hashing for Message Authentication Explained
HMAC (Hash-based Message Authentication Code) proves both the integrity and authenticity of a message using a shared secret key and a hash function like SHA-256. It's what signs webhooks, AWS requests, and API payloads. This guide explains how HMAC works, why it's not encryption, and how it differs from a plain hash.
Last updated:
Core Concepts
- What HMAC proves
- HMAC answers two questions at once: was this message tampered with (integrity), and does it come from someone who holds the shared secret (authenticity)? The sender computes HMAC(secret, message) and attaches the result; the receiver, who knows the same secret, recomputes it and compares. If they match, the message is intact and from a trusted source. It does NOT hide the message — HMAC is authentication, not encryption.
- HMAC vs a plain hash
- A plain SHA-256 of a message proves integrity only: anyone can recompute it, so anyone can forge a matching hash for a modified message. HMAC mixes in a secret key, so only parties holding the key can produce a valid code. That's the crucial difference — a bare hash says 'this is the hash of some data', while an HMAC says 'someone with the secret vouches for this exact data'.
- How it's built
- HMAC (RFC 2104) wraps a normal hash function: roughly HMAC(K, m) = H((K ⊕ opad) ‖ H((K ⊕ ipad) ‖ m)). The nested construction with two key-derived pads protects against length-extension attacks that would affect a naive hash(key ‖ message). You name it by the inner hash: HMAC-SHA256 (HS256 in JWT), HMAC-SHA1, HMAC-SHA512. SHA-256 is the modern default.
- Where HMAC is used
- HMAC is everywhere secrets meet messages: webhook signatures (GitHub, Stripe sign payloads with HMAC-SHA256 so you can verify they're genuine), AWS Signature V4 request signing, JWT HS256 tokens, and API request signing in general. Any time two parties share a secret and need to verify a message wasn't forged or altered, HMAC is the standard tool.
Frequently Asked Questions
Is HMAC encryption?
No. HMAC is a message authentication code — it proves a message is intact and came from someone with the shared secret, but it does not hide the message. The original data is sent in the clear alongside the HMAC. If you need confidentiality, encrypt the message (e.g. AES-GCM, which also provides authentication) in addition to or instead of HMAC.
What's the difference between HMAC and a hash like SHA-256?
A plain SHA-256 hash can be computed by anyone, so it only detects accidental changes — an attacker can forge a matching hash for altered data. HMAC mixes in a secret key, so only someone with the key can produce a valid code. That makes HMAC suitable for authentication, not just integrity.
Which hash should I use with HMAC?
HMAC-SHA256 (HS256) is the modern default and what most webhooks, AWS SigV4, and JWTs use. Use SHA-384 or SHA-512 only when a specification requires a longer digest. Avoid HMAC-MD5 and HMAC-SHA1 for new systems.
Try these related tools
HMAC Generator →
Compute HMAC signatures with SHA-1, SHA-256, SHA-384, or SHA-512 online. Verify API requests, sign webhooks, and authenticate messages. Free, no signup — signing runs locally via the Web Crypto API, secrets never leave your browser.
HMAC-SHA256 →
Compute HMAC-SHA256 signatures with any secret key. Outputs hex, base64, and base64url encodings. Verify a signature against an expected value in one click. Used by AWS SigV4, JWT HS256, Stripe / GitHub / Slack webhooks. 100% local via Web Crypto API.
JWT Verifier →
Paste a JWT and its signing key (HMAC secret, RSA/EC public key in PEM or JWK) to verify the signature and inspect claims. Supports HS256/384/512, RS256/384/512, PS256/384/512, ES256/384/512. Also flags exp / nbf / iat time-based claims. 100% local — nothing is sent to any server.
SHA-256 Generator →
Compute SHA-256 (256-bit) hashes of text or files instantly online. The gold-standard cryptographic hash for signatures, blockchain, and integrity. Drag-and-drop file support; verify against a known SHA-256 in one click. 100% local.
Hash Generator →
Generate MD5, SHA-1, SHA-256, and SHA-512 hashes of any text online. Free, no signup — all hashing runs entirely in your browser via the Web Crypto API, so nothing is uploaded.