HMAC vs Hash: Generate and Verify an HMAC-SHA256
Short answer: an HMAC combines a hash function with a secret key. Anyone holding the same key can recompute it and confirm that a message is unchanged and came from a key holder. Use the HMAC Generator & Verifier to produce an HMAC-SHA256 or to check one you were given. The message and secret are computed locally, and are never sent, stored or put in the URL.
HMAC versus a plain hash
A hash has no key, so anyone can compute it. An HMAC needs the secret, so only holders of the key can create or check it. That is why HMAC is used to sign webhooks and API requests, and why it is the signature behind HS256 JSON Web Tokens (see the JWT guide). Plain digests are made with the Hash Generator; for choosing between them, see MD5 vs SHA-256 vs SHA-3 vs BLAKE2.
HMAC is not encryption
The message stays readable. The HMAC only travels alongside it as proof, and it cannot be turned back into the message. To keep data secret you need real encryption, and HMAC is not a way to store passwords either: use bcrypt, scrypt or Argon2. See also Base64 is not encryption for the same trap.
A test you can check yourself
With the secret key and the message The quick brown fox jumps over the lazy dog:
- HMAC-SHA-256 (hex):
f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8 - HMAC-SHA-1 (hex):
de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9
An empty secret with an empty message gives b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad for HMAC-SHA-256.
Why your result does not match
Almost every mismatch is one of these:
- A trailing newline. It changes the result, so a mismatch with a command-line check is often a line break.
- Different encodings. The message and the secret each have their own setting: UTF-8 text, hex or Base64. A binary key must be entered as hex or Base64, not as text.
- Wrong algorithm. In Verify mode, if the value matches a different algorithm, the tool tells you which one.
How to generate an HMAC SHA256 value with the HMAC tool
- Open the HMAC Generator & Verifier.
- Generate: enter the message and secret, pick HMAC-SHA-256 (or SHA-1, SHA-384, SHA-512) and copy the result as hex, Base64 or Base64url.
- Verify: paste the HMAC you received. Hex and Base64 are detected automatically. You get a valid or invalid result, and a reminder to check the message, the secret and the encodings if it fails.
- Create a strong secret with the Password Generator; see strong random passwords.
Constant-time comparison on your server
The tool compares byte by byte over the full length, but in a browser this is a best-effort measure. In production, use your platform's constant-time comparison function, not a normal string comparison, so that timing does not reveal how many characters were right.
Is HMAC-SHA-1 still safe?
The tool notes that HMAC-SHA-1 is still considered safe as a message authentication code, but recommends HMAC-SHA-256 or stronger for new designs.
Limits to know
- Algorithms depend on your browser's Web Crypto support. Any it does not provide are disabled.
- The browser comparison is not a substitute for server-side constant-time checks.
- The secret field is cleared when you leave the page.
Frequently asked questions
What is HMAC? A keyed hash (RFC 2104) that proves integrity and knowledge of a secret.
What is the difference between HMAC and SHA-256? SHA-256 has no key. HMAC-SHA-256 uses one.
How do I verify a webhook signature? Compute the HMAC of the exact raw message with the shared secret and compare it with the signature in constant time.
Why do I get a different value from the command line? Usually a trailing newline or a different key encoding.
Does my secret leave the browser? No.