HMAC Generator · Tutorial
How to verify a webhook signature with HMAC-SHA256
7 min readintermediateUpdated
Your webhook endpoint rejects every request with "invalid signature", or you just want to confirm a payload really came from your provider. This tutorial recalculates the HMAC-SHA256 signature from the raw body and the signing secret, compares it with the header, and covers the usual reasons they differ.
What you need
- The HMAC Generator
- The raw webhook body, the signature header and your signing secret (the Try it button loads a sample payment event)
Steps
Get the raw body
Log or copy the request body exactly as received, before any JSON parsing. The sample body is a single line of JSON for a
payment.succeededevent. Paste it into Message without adding spaces or line breaks.Enter the signing secret
Copy the webhook signing secret from your provider's dashboard into Secret key. The sample uses
whsec_demo_51f2c9as plain text. If your provider documents the secret as Base64, choose that format instead.Choose SHA-256
Keep Hash on SHA-256, the standard for webhooks. The HMAC appears immediately, in hex and Base64.
ea3d3d9e6deb526f4b9809d3ef75223709552d77a00f26e56781fcd2d9bdfdd7Compare with the header
Paste the value of the signature header, here
sha256=ea3d…fdd7, into Compare with a received signature. Thesha256=prefix is ignored automatically. A green badge means the signature is genuine.Implement it in code
In your handler, compute the HMAC over the raw body bytes and compare with a constant-time function.
const expected = crypto.createHmac('sha256', secret).update(rawBody).digest('hex'); const ok = crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(received));
Try it yourself
Opens the HMAC Generator with this tutorial's sample loaded.
Common mistakes
- Signing the parsed JSON
- Frameworks often parse the body before your code sees it. Re-serialising changes spacing and key order. Use the raw body your framework provides, often via a raw body or text option.
- Missing the signed timestamp
- Some providers sign
timestamp.bodyrather than just the body, to stop replays. Build the exact same string in the message box. - Comparing with ===
- Ordinary string comparison can leak timing information. Use a constant-time comparison in production code.
Next steps
Go back to the HMAC Generator, or keep going with one of these:
FAQ
Can I check a signature without the secret?
No. That's the point of an HMAC. Only holders of the secret can create or check it.