RunTheTests
/

HMAC Generator

Compute an HMAC, and see why it is not a hash of the key and the message.

Message

Secret key

How this works

The key and message are passed to WebCrypto's HMAC implementation. The construction is worth understanding rather than treating as a black box: HMAC is not a hash of the key concatenated with the message, and that difference is the reason it exists. Hashing key-then-message is vulnerable to length extension, where an attacker who has never seen the key can take a valid tag, append data, and produce a valid tag for the longer message. HMAC's nested construction — hashing twice with two derived keys — closes that. A useful consequence is that HMAC does not rely on the hash being collision-resistant, which is why HMAC-SHA1 is still considered sound while SHA-1 itself is not.

What the results mean

HMAC
Message authentication code. Proves the message is unchanged and came from someone holding the key.
Versus a plain hash
A hash proves only that the data has not changed. Anyone can compute one; only a key holder can compute an HMAC.
Key length
Shorter than the hash output weakens the MAC. At least as long as the digest is the usual advice.
Length extension
The attack HMAC's nested construction prevents, and the reason not to hash key-then-message.

Common problems and fixes

Which algorithm should I use?
HMAC-SHA256 unless something you are integrating with requires otherwise. SHA-1 is present for legacy compatibility and remains sound in this construction.
My HMAC does not match the other system's
Check encoding first — text encoding, and whether the other side expects hex or Base64. Those account for most mismatches before the algorithm does.
Can I use this to verify a webhook signature?
For checking by hand, yes. In code you must compare using a constant-time function, since a naive comparison leaks the correct value one byte at a time.

Frequently asked questions

What is the difference between a hash and an HMAC?

A hash proves data has not changed and anyone can compute one. An HMAC additionally proves it came from someone holding the key, which is what makes it useful for authenticating messages rather than merely checking them.

Why not just hash the key and message together?

Length extension. With most hash constructions an attacker who has the tag for one message can compute a valid tag for that message plus their own data, without ever knowing the key. HMAC's nested design prevents it.

Is HMAC-SHA1 still safe?

In this construction, yes. HMAC's security does not depend on collision resistance, which is the property SHA-1 lost. It is legacy rather than broken, though SHA-256 is the better default for new work.

Put this on your own site

Free to embed, no attribution required beyond the source link the frame carries itself. It runs entirely in your visitor's browser, sets no cookies and loads no third-party script.

Embed code
<iframe src="https://runthetests.com/embed/hmac-generator/" width="100%" height="560" style="border:1px solid #e5e5e5;border-radius:8px" title="HMAC Generator" loading="lazy"></iframe>

Preview it at https://runthetests.com/embed/hmac-generator/. Embedded pages are marked noindex, so yours stays the canonical copy — not this one.

More in Security