Webhook signature verification basics: HMAC and the raw body
A webhook is a direct conversation between servers. A payment succeeded, code was deployed, an order arrived: each is a single HTTP request. The problem is that nothing in the request proves it came from that service. Anyone who learns the URL can send a request with the same shape.
What a signature proves
Sender and receiver share a secret. The sender computes a value from the body and the secret and puts it in a header; the receiver repeats the computation and compares. Anyone without the secret cannot produce a matching value for a changed body. That computation is HMAC.
HMAC, defined in RFC 2104, takes a hash function and a secret key and returns a fixed-length value. HMAC-SHA256 returns 32 bytes, SHA-384 returns 48 and SHA-512 returns 64. The output is usually written as hexadecimal or Base64.
The signed subject is the body's bytes
The most common failure is signing something different from what was sent. HMAC computes over bytes, not over a parsed structure. Parse JSON and serialise it again and whitespace, key order and unicode escapes can change, so the bytes change while the content looks identical.
- Keep the original bytes and parse only after verification.
- Line endings are bytes. LF (
\n) and CRLF (\r\n) are different inputs. - A proxy or framework that decompresses or rewrites the body can break the signature, so use middleware that preserves the raw body.
- Fix the character encoding to UTF-8. The same characters in another encoding are different bytes.
How secrets are stored
Services display secrets differently. GitHub and Slack give a readable string; Stripe gives a string starting with whsec_. Some services show Base64 or Hex. What matters is the key bytes, not the characters on screen. Reading a Base64 string as UTF-8 text yields an entirely different key.
| Display | Actual key bytes | Watch out for |
|---|---|---|
| UTF-8 text | The characters' UTF-8 bytes | Most common. Strip leading and trailing whitespace |
| Base64 | Bytes after decoding | Padding characters and URL-safe variants |
| Hex | One byte per two characters | Remove 0x prefixes, colons and spaces |
Order of verification
- Read the raw body. Do not parse it yet.
- Take the algorithm and signature from the header.
- Compute HMAC with the algorithm, secret and body.
- Compare the computed and received values in constant time.
- If a timestamp is present, check it against the tolerance.
- Only then parse the body and process the event.
Always compare with a constant-time function. An ordinary string comparison stops at the first differing byte, so response time can reveal how many bytes were correct.
Where it usually breaks
- Verifying against a string rebuilt after parsing the body
- Pretty-printing JSON in development and compressing it in production
- Storing the secret as Base64 but reading it as text
- Comparing signatures with
== - Skipping the timestamp tolerance so old requests still pass
- Treating a retry as a new event and processing it twice
Check these six in order and most webhook signature problems are found within minutes. hmaclab reproduces the same steps on screen.
Questions
How is HMAC different from a digital signature?
HMAC is symmetric: both sides share one secret key. A public-key signature signs with a private key and verifies with a public key, so no secret is shared. Webhooks already share a secret between sender and receiver, which is why HMAC is common.
Can an empty body be signed?
Yes. An empty byte string is a valid HMAC input. Whether to accept an empty body is a separate decision in your event handling.
How long should a secret be?
HMAC-SHA256 prefers at least 32 bytes, the same length as its output. Use random bytes rather than a short memorable phrase, and store exactly what the service generated.
Enter a secret and body and check the signature right away.
Open the toolThis guide follows the HMAC definition in RFC 2104 and the public webhook documentation of GitHub, Slack and Stripe. Every example value is synthetic.