DevKits
Concept

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