opyeb hmaclab

hmaclab · Basics

Webhook signature verification basics: HMAC and the raw body

What HMAC is, what it signs, and where it usually breaks

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.

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.

DisplayActual key bytesWatch out for
UTF-8 textThe characters' UTF-8 bytesMost common. Strip leading and trailing whitespace
Base64Bytes after decodingPadding characters and URL-safe variants
HexOne byte per two charactersRemove 0x prefixes, colons and spaces

Order of verification

  1. Read the raw body. Do not parse it yet.
  2. Take the algorithm and signature from the header.
  3. Compute HMAC with the algorithm, secret and body.
  4. Compare the computed and received values in constant time.
  5. If a timestamp is present, check it against the tolerance.
  6. 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

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 tool

This guide follows the HMAC definition in RFC 2104 and the public webhook documentation of GitHub, Slack and Stripe. Every example value is synthetic.