CalcSnippets
Security 3 min read

When to Use HMAC and SHA-256 for Message Integrity

Understand the difference between hashing and HMAC, how shared secrets work, and how to verify signed webhooks safely.

Hashing and HMAC are often mentioned together, but they answer different security questions. A hash such as SHA-256 produces a fixed-length digest from data. Anyone who has the same data can calculate the same digest, so a plain hash does not prove who created a message. HMAC combines a cryptographic hash with a shared secret. A receiver who knows the secret can verify that a message was produced by someone who also knew it and that the signed bytes have not changed.

Use HMAC for authenticated integrity

A common use case is a webhook. The sender serializes a payload, computes an HMAC over the exact request bytes using a shared secret, and includes the signature in a header. The receiver reads the raw body, computes the same HMAC, and compares the result using a timing-safe comparison where the platform provides one. If the values match, the receiver has evidence that the payload was not modified and came from a party with the secret.

The phrase “exact request bytes” matters. Parsing JSON and serializing it again can change whitespace or key order, producing a different value even when the data appears equivalent. Verify before transforming the body. Check the provider’s documentation for the exact algorithm, header format, encoding, timestamp requirement, and versioning rules. A correct cryptographic primitive used over different input is still a failed verification.

Protect secrets and resist replay

An HMAC secret is sensitive. Store it in a secret manager or protected environment variable, not in client-side code, a public repository, or an error log. Rotate it according to the provider’s process and support an overlap period when necessary. Do not reveal the expected signature in a response. A local HMAC Generator is useful for test fixtures and debugging, but production verification must happen in a trusted server environment.

Integrity alone does not stop an attacker from replaying a previously valid request. Many webhook systems include a timestamp or event ID. Reject old timestamps within a reasonable window and record processed event IDs to make handling idempotent. Rate limits, HTTPS, and authorization checks remain important even when signature verification is correct.

Avoid legacy choices for new designs

Prefer modern documented algorithms such as HMAC-SHA-256 unless an interoperating system requires something else. Do not use a bare concatenation like hash(secret + message) as a homegrown substitute for HMAC. It has different security properties and is easy to implement inconsistently. Use the cryptographic library supplied by your platform rather than writing the algorithm yourself.

  • Use HMAC when the receiver must verify a shared-secret signature.
  • Sign and verify the original raw bytes.
  • Store secrets outside source code and logs.
  • Use timestamps or event IDs to limit replay attacks.

HMAC is straightforward when its boundary is clear: trusted server code, exact input, protected secret, documented algorithm, and a replay policy. Those operational details are what make the cryptography useful. Add a focused integration test with a known payload and signature so an innocent framework upgrade, body-parser change, or header normalization change cannot quietly disable verification.

Keep reading

Related guides