How to Verify a Webhook HMAC Signature (and Why It Fails)

Learn what an HMAC is, how webhook providers sign payloads, how to verify signatures safely in Node.js, and the raw-body and encoding mistakes that break them.

Updated September 29, 2026 6 min read

Use the free HMAC GeneratorCompute HMAC-SHA1, SHA-256, SHA-384 and SHA-512 signatures online

You have set up a webhook endpoint. A payment provider, a Git host or a form service will call it whenever something happens. But your endpoint is a public URL: anyone who discovers it can send a fake "invoice paid" event. That is why most providers sign each request with an HMAC, and why their documentation asks you to verify the signature before trusting the payload.

In theory verification is three lines of code. In practice, "signature mismatch" is one of the most common webhook support questions. This guide explains what an HMAC is, how webhook signing works, how to verify it correctly, and the handful of mistakes behind nearly every mismatch.

What an HMAC is

HMAC stands for Hash-based Message Authentication Code, defined in RFC 2104. It combines a secret key with a message and runs them through a hash function such as SHA-256 in a specific two-pass construction. The output is a fixed-length code, commonly shown as hex or Base64.

An HMAC proves two things to anyone who holds the same secret key:

  • Integrity: the message has not been changed, because any change produces a completely different code.
  • Authenticity: the message came from someone who knows the key, because without the key you cannot compute the right code.

Why not just hash the message with the secret?

It is tempting to compute sha256(secret + message) instead. That construction is vulnerable to a length-extension attack with SHA-256 and other hashes of the same family: an attacker who sees one valid code can compute a valid code for the message with extra data appended, without knowing the secret. HMAC's construction prevents this. Always use a real HMAC function rather than a homemade combination.

HMAC versus signatures and encryption

An HMAC is symmetric: the sender and the receiver share the same secret. A digital signature (RSA, ECDSA, Ed25519) uses a private key to sign and a public key to verify, so the verifier cannot create signatures. HMAC is simpler and faster, which is why it dominates webhooks, where each customer gets their own shared secret. An HMAC does not encrypt anything; the payload is still readable, and HTTPS protects it in transit.

How webhook signing works

The pattern is the same across most providers, with small differences you must read in their documentation:

  1. You receive a signing secret when you create the webhook.
  2. For each delivery, the provider computes an HMAC, usually HMAC-SHA256, over the raw request body, sometimes combined with a timestamp.
  3. It sends the result in a header, for example X-Signature: sha256=910afade.... The header name, prefix and encoding (hex or Base64) vary by provider.
  4. Your endpoint computes the same HMAC with the same secret and compares.

Verify a webhook in Node.js

Here is a complete, runnable example using only Node's built-in crypto module:

const crypto = require("node:crypto");

function verifyWebhook(rawBody, signatureHeader, secret) {
  const expected = "sha256=" + crypto.createHmac("sha256", secret).update(rawBody).digest("hex");
  const a = Buffer.from(expected);
  const b = Buffer.from(signatureHeader || "");
  // timingSafeEqual throws if lengths differ, so check first
  return a.length === b.length && crypto.timingSafeEqual(a, b);
}

const secret = "whsec_test_123";
const body = '{"event":"invoice.paid","id":"evt_1001"}';
const header = "sha256=" + crypto.createHmac("sha256", secret).update(body).digest("hex");

console.log(verifyWebhook(body, header, secret));                         // true
console.log(verifyWebhook(body.replace("1001", "1002"), header, secret)); // false

Two details matter for security. First, compare with timingSafeEqual, not ===: an ordinary string comparison can stop at the first differing character, and the timing difference can, in principle, help an attacker guess a valid code byte by byte. Second, reject the request before doing anything with the payload if verification fails.

Why signatures fail to match

You hashed parsed JSON instead of the raw body

This is the most common cause by far. Frameworks often parse the JSON body before your handler runs. If you re-serialise it with JSON.stringify, whitespace, key order and number formatting may differ from what the provider signed, and even one changed byte breaks the HMAC:

JSON.stringify(JSON.parse('{"id": 1,  "event":"x"}')); // '{"id":1,"event":"x"}'

Configure your framework to give you the raw bytes for the webhook route, and compute the HMAC over those.

The key is in the wrong format

Some providers give you a secret as text, others as hex or Base64 that must be decoded to bytes before use. Using the Base64 string as UTF-8 text produces a completely different key. Read the provider's documentation carefully, and look for a prefix like whsec_ that may or may not be part of the key.

The encoding or prefix differs

Hex versus Base64, uppercase versus lowercase hex, a sha256= prefix or a v1, prefix: all of these must match exactly. Strip or add prefixes deliberately.

The provider signs more than the body

Many providers sign a string such as timestamp + "." + body, and send the timestamp in another header. They do this to prevent replay attacks, where someone resends a captured valid request later. If your provider includes a timestamp, sign exactly the documented string and reject requests whose timestamp is too old.

Character encoding and line endings

A body containing non-ASCII text must be hashed as the exact bytes received, normally UTF-8. Copying a payload through an editor or a terminal can change line endings or encoding, which is worth remembering when you debug by hand.

Debug a signature in your browser

When a verification fails, compute the HMAC by hand to see which side is wrong. The HMAC Generator does this locally in your browser, using the Web Crypto API.

  1. Open the HMAC Generator. Click Load sample webhook to see a worked example.
  2. Paste the exact raw body into Message.
  3. Enter the Secret key and choose the Key format: Text (UTF-8), Hex or Base64.
  4. Choose the Output: Hex (lowercase), Hex (uppercase), Base64 or Base64URL. The tool shows HMAC-SHA-1, SHA-256, SHA-384 and SHA-512 at the same time.
  5. Paste the value from the provider's header into Compare with an expected signature (optional). A leading sha256= or sha1= label is ignored automatically; other prefixes, such as v1,, should be removed first. The tool tells you which algorithm matches, or that none does with the current key, message and output format.

If nothing matches, change one thing at a time: the key format first, then the output encoding, then the exact message (for example, adding the timestamp prefix your provider documents).

Use test secrets when debugging on any website. Rotate a production secret if you suspect it has been exposed.

Other places HMACs appear

  • JSON Web Tokens signed with HS256 use HMAC-SHA256 over the header and payload. The JWT Decoder shows what a token contains.
  • API request signing, such as some cloud providers' signature schemes, uses chains of HMACs over canonical request strings.
  • Signed URLs and cookies use HMACs so that a server can detect tampering without storing state.

For plain checksums without a key, use the Hash Generator instead; to convert keys between hex and Base64, the Base64 Converter helps.

Frequently asked questions

What is the difference between a hash and an HMAC?

A hash like SHA-256 takes only a message, so anyone can compute it. An HMAC also requires a secret key, so only key holders can produce or verify it.

Which HMAC algorithm should I use?

HMAC-SHA256 is the common default and is considered secure. HMAC-SHA1 is still secure for HMAC purposes in practice but is being phased out; use it only when a system requires it.

Can an HMAC be reversed to get the message or key?

No. HMACs are one-way. However, a short or guessable secret can be brute-forced if an attacker has a message and its code, so use long random secrets.

Why does my webhook signature not match?

Most often because the code hashes a re-serialised JSON object instead of the raw body, or uses the secret in the wrong encoding. Check also the output format, prefixes and whether a timestamp is part of the signed string.

Should I compare signatures with ===?

Use a constant-time comparison such as Node's crypto.timingSafeEqual. It avoids leaking information through response timing.

Try it now: HMAC Generator

Loading tool…