One extra key-mixing step over a plain hash
Hashing message plus key is unsafe because length extension lets attackers forge. HMAC splits the key: an inner pad XORs into the key and hashes with the message, then an outer pad XORs into the key and hashes with that inner result. The key participates twice and length extension no longer applies. Implementation uses native browser crypto across SHA-1, SHA-256 and SHA-512, with an option to compute all three.
Steps
- Enter the shared secret as text or hex and set the matching format.
- Choose the hash and an output of hex, Base64 or Base64url.
- Provide the message and compute; enable all-three mode to compare.
- Paste an expected signature to check equality, and toggle hex case.
- Load the example, clear, or copy the result.
Key length versus block size
Block size follows the hash: sixty-four bytes for SHA-1 and SHA-256, one hundred twenty-eight for SHA-512. Shorter keys are zero-padded to the block, longer keys are hashed first. So a key need not match the block, but too short weakens security; at least the hash output length is sensible, thirty-two bytes for SHA-256. Treating a password as a key is the commonest failure.
Why verification is constant-time
Ordinary string comparison returns at the first mismatch, and attackers time responses to guess the signature byte by byte — a timing attack. Constant-time comparison examines every byte regardless of content. The expected-signature field here is a convenience for humans; production verification must use a constant-time routine. This is not theoretical: multiple payment and API signing systems have failed on it.
Versus signatures and encryption
HMAC proves integrity and that the sender holds the key, which means it cannot prove authorship to a third party — any key holder can sign. Asymmetric signatures with private and public halves address non-repudiation. Encryption handles confidentiality but not integrity unless the mode authenticates. API schemes typically combine sorted parameter concatenation with HMAC to authenticate the caller and detect tampering.
Common problems:Mismatched concatenation rules — separators, ordering, inclusion of empty values — break every signature. Encoding differences between UTF-8 and GBK change bytes entirely. Keys carrying invisible line breaks from copy-paste fail silently. Base64 and Base64url differ in plus and slash handling. HMAC is one-way, so it cannot be mistaken for encryption. And SHA-1 persists despite demonstrated collisions; new systems start at SHA-256.