SHA-256 vs HMAC — Which Should You Use?
SHA-256 and HMAC both produce a fixed-size digest, but they answer different questions. SHA-256 is an unkeyed checksum. Anyone can compute it, so it detects accidental corruption but says nothing about who made the data. HMAC is the same hash family wrapped in a secret key. Only parties who share the key can produce or verify it, so it proves authenticity as well as integrity.
Use SHA-256 when
- You need a checksum or fingerprint: file integrity, content addressing, or verifying a payload wasn't altered.
- There is no shared secret, e.g. public downloads, cache keys, dedup, or matching an existing spec.
- Anyone should be able to recompute and verify the digest, not just key holders.
- You want the simplest possible hash with no key to manage.
Use HMAC when
- You must prove the data came from someone who knows the secret: API signing, webhooks, auth tokens.
- The message or payload could be tampered with by an attacker, not just corrupted by accident.
- You and the receiver already share a secret key you can keep out of the message itself.
- You need to detect forgery, not just accidental damage.
Side by side
| SHA-256 Hash Generator | HMAC Generator | |
|---|---|---|
| Purpose | Unkeyed checksum | Keyed authentication code |
| Secret key | None | Required, shared between parties |
| Proves | Integrity (not tampered) | Integrity + authenticity (from the key holder) |
| Who can verify | Anyone | Anyone with the key |
| Output (SHA-256) | 64 hex chars | 64 hex chars |
| Where it runs | Browser, no upload | Browser, no upload |
HMAC is not a substitute for password hashing. It's for signing messages. For password storage use a slow, salted function like bcrypt. Neither tool uploads your input; both run entirely in the browser.
Use SHA-256 when there's no secret and you just need to detect corruption: file checksums, cache keys, content addressing. Reach for HMAC when authenticity matters: if an attacker could forge the data, an unkeyed hash proves nothing. When in doubt, integrity-only work → SHA-256, signed or authenticated exchanges → HMAC.