About hmac generator
An HMAC combines a message with a secret key to produce a signature. Anyone who has the key can check that the message is genuine and unchanged. Webhooks often use HMAC to prove who sent them.
How it is used
- The sender computes HMAC(secret, body) and sends the result in a header.
- The receiver repeats the calculation with the same secret and compares.
- Compare using a constant-time function to avoid leaking information.
Algorithm choice
Prefer SHA-256 or stronger. HMAC with SHA-1 still works for older systems but should not be used in new designs.
A worked example
A payment provider sends a webhook with the header X-Signature. You compute HMAC-SHA256 of the raw request body with your shared secret, as 64 hex characters, and compare it with the header using a constant-time comparison. If they match, the message is genuine.
Common mistakes
- Computing the HMAC over a re-formatted version of the body instead of the raw bytes.
- Comparing with a plain equality check, which can leak timing information.
- Forgetting to check the timestamp, which allows replay of old messages.
