Verify a webhook's HMAC signature
A webhook signature is an HMAC the provider computes over the raw request body with a shared secret. Your server computes the same HMAC and compares: if the two values differ, the request didn't come from the provider or was altered on the way.
The principle in one sentence
HMAC-SHA256(secret, message) produces a digest only a holder of the secret can compute. The message is the raw body at GitHub, and the string "timestamp.body" at Stripe. Any different byte (a space, a newline, JSON key order) changes the digest completely.
GitHub: X-Hub-Signature-256
GitHub sends sha256= followed by the hex HMAC of the body, computed with the secret entered in the webhook settings. Compare in constant time (timingSafeEqual) so the signature can't be guessed byte by byte.
import { createHmac, timingSafeEqual } from 'node:crypto'
// rawBody: the request body exactly as received (Buffer or string), never JSON.parse + JSON.stringify.
export function verifyGithub(rawBody, signatureHeader, secret) {
const expected = 'sha256=' + createHmac('sha256', secret).update(rawBody).digest('hex')
const a = Buffer.from(expected)
const b = Buffer.from(signatureHeader ?? '')
return a.length === b.length && timingSafeEqual(a, b)
}Stripe: Stripe-Signature
Stripe signs the string timestamp + "." + raw body with the endpoint secret (whsec_…). The header holds t= (the timestamp) and one or more v1= values: during a secret rotation, several signatures can be present. Also reject timestamps that are too old to block replays; the official library uses a 5-minute tolerance.
In production, prefer the official library function (stripe.webhooks.constructEvent): the code below is for understanding and debugging.
import { createHmac, timingSafeEqual } from 'node:crypto'
// Header: Stripe-Signature: t=<unix timestamp>,v1=<hex signature>[,v1=<...>]
export function verifyStripe(rawBody, signatureHeader, secret, toleranceSec = 300) {
const pairs = (signatureHeader ?? '').split(',').map(p => p.split('='))
const t = pairs.find(([k]) => k === 't')?.[1]
const signatures = pairs.filter(([k]) => k === 'v1').map(([, v]) => v)
if (!t || Math.abs(Date.now() / 1000 - Number(t)) > toleranceSec) return false
const expected = createHmac('sha256', secret).update(`${t}.${rawBody}`).digest('hex')
return signatures.some(s => s.length === expected.length
&& timingSafeEqual(Buffer.from(s), Buffer.from(expected)))
}Why verification fails
In order of frequency:
- The body is no longer raw: a middleware (express.json(), a framework body parser) parsed the JSON, and your code signs JSON.stringify(req.body). Key order, whitespace and unicode escaping differ. Read the body as a Buffer on the webhook route.
- The wrong secret: at Stripe, every endpoint has its own secret, different between test and live mode, and different from the one printed by the Stripe CLI.
- Encoding: comparing a hex signature with a base64 digest, or forgetting the sha256= prefix.
- The timestamp: a request replayed several minutes later exceeds Stripe's tolerance, even with a correct HMAC.
- A proxy that alters the body: decompression, re-encoding or an added trailing newline.
Debug with the real request
Temporarily point the webhook to a Tracehook capture URL: you get the exact body and signature header as the provider sent them. Recompute the HMAC locally over that body. If your result matches the received signature, the problem is how your server reads the body; otherwise, it's the secret.
# Body saved byte for byte from Tracehook (JSON export or "Copy as cURL")
openssl dgst -sha256 -hmac "$WEBHOOK_SECRET" < body.json
# Compare the hex output with the signature header received.