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

  1. 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.succeeded event. Paste it into Message without adding spaces or line breaks.

  2. Enter the signing secret

    Copy the webhook signing secret from your provider's dashboard into Secret key. The sample uses whsec_demo_51f2c9 as plain text. If your provider documents the secret as Base64, choose that format instead.

  3. Choose SHA-256

    Keep Hash on SHA-256, the standard for webhooks. The HMAC appears immediately, in hex and Base64.

    ea3d3d9e6deb526f4b9809d3ef75223709552d77a00f26e56781fcd2d9bdfdd7
  4. Compare with the header

    Paste the value of the signature header, here sha256=ea3d…fdd7, into Compare with a received signature. The sha256= prefix is ignored automatically. A green badge means the signature is genuine.

  5. 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.

Try it now

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.body rather 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.