--- url: "https://webhooks.fyi/security/hmac" titulek: "Hash-based Message Authentication Code (HMAC) - Docs" h1: "Hash-based Message Authentication Code (HMAC)" popis: "HMAC is, by far, the most popular authentication and message security method used on webhook requests, including 65% of the 100 webhooks we studied. In this method, the webhook provider and listener use a secret key to sign and validate webhook requests." jazyk: "en" kanonicka: "https://webhooks.fyi/security/hmac" drobecky: [] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 05:20:08" znaku: 3813 slov: 472 kodovani: "utf-8" sha256: "fe1be033b4b78ee0a540144f184b756f02234c2c03c3098f24be2564bdba850a" --- | **Complexity** | Low | | --- | --- | | **Pros** | Authentication and Message Integrity with Low ComplexitySecret keys are not sent in the webhook notificationHash signature enables timestamp validation | | **Caveats** | No confidentiality controlsIntroduces additional complexity with complex signature payloads | | **Examples** | [GitHub](https://docs.github.com/en/developers/webhooks-and-events/webhooks/securing-your-webhooks)[Shopify](https://shopify.dev/apps/webhooks/configuration/https#step-5-verify-the-webhook)[Slack](https://api.slack.com/authentication/verifying-requests-from-slack)[Square](https://developer.squareup.com/docs/webhooks/step3validate)[Twilio](https://www.twilio.com/docs/usage/security#validating-requests) | --- Hash-based Message Authentication Code (HMAC) is, by far, the most popular authentication and message security method used on webhook requests, including 65% of the webhooks we studied. In this method, the webhook provider and listener use a secret key to sign and validate webhook requests. 1. On webhook requests, the provider signs the webhook message using the secret key plus a hashing algorithm — typically `HMAC-SHA256`, encodes the resulting signature in `base64` or `hex`, and then includes the signature in the webhook request as a header. 2. The webhook listener receives the request and repeats the same steps — signs and encodes the webhook message using the secret key — and compares the resulting signature with the value sent in the request header. If the result matches, the request is considered legitimate. `const signatureHeader = 'Signature-Header' const signatureAlgorithm = 'sha256' const encodeFormat = 'hex' const hmacSecret = process.env.WEBHOOK_SECRET app.post('/webhook', (req, res) => { const hashPayload = req.rawBody const hmac = crypto.createHmac(signatureAlgorithm, hmacSecret) const digest = Buffer.from(signatureAlgorithm + '=' + hmac.update(hashPayload).digest(encodeFormat), 'utf8') const providerSig = Buffer.from(req.get(signatureHeader) || '', 'utf8') if (providerSig.length !== digest.length || !crypto.timingSafeEqual(digest, providerSig)) { res.status(401).send('Unauthorized') }else{ res.json({ message: "Success" }) } })` *Request Signature Validation* ## HMAC vs. Shared Secrets HMAC offers the following advantages over Basic Authentication: 1. **Authentication + message integrity** : Assuming the secret key is known only by the webhook provider and the listener, the HMAC process verifies that the message comes from the webhook provider (authenticity) and its contents are the same as they were at the time of sending (integrity). 2. **The secret key remains secret:** In HMAC, secret keys are not sent with the webhook request — only the signatures created with it — reducing the risk of stolen keys. ## HMAC is only as good as its implementation Like any other security control, HMAC is only as good as its implementation. In our research, we saw many examples of webhook providers with unnecessary complexity, lack of features, and complex documentation that made their solutions tough to implement and keep safe. Good webhook implementations will typically: 1. Use strong hash algorithms such as `sha256` and `sha512` 2. Add sensitive headers to the hash digest: `... const clientIdHeader = 'clientid' ... app.post('/webhook', (req, res) => { const hashPayload = req.rawBody+'.'+req.get(clientIdHeader) const hmac = crypto.createHmac(signatureAlgorithm, hmacSecret) const digest = Buffer.from(signatureAlgorithm + '=' + hmac.update(hashPayload).digest(encodeFormat), 'utf8') ... })` 3. Leverage HMAC signatures to implement [replay prevention](/security/replay-prevention) , [versioning](/ops-experience/versioning) , and [key rotation](/ops-experience/key-rotation) 4. Provide great [documentation](/ops-experience/documentation) and features for better [operations](/ops-experience/intro)