Skip to content

HMAC Generator - HMAC-SHA256, SHA1, SHA512 Online

HMAC-SHA-256
—

What This HMAC Generator Does

HMAC (hash-based message authentication code, RFC 2104) combines a secret key with a hash function to produce a tag for a message. If the message or the key changes by a single bit, the tag changes completely. This tool calculates HMAC-SHA1, HMAC-SHA256, HMAC-SHA384 and HMAC-SHA512 and shows the result as hex or Base64, which are the two formats APIs normally expect.

How to Generate an HMAC

  1. Paste the exact message into the Message box. Spaces and line breaks count.
  2. Enter the secret key and choose whether it is text or hex.
  3. Pick the algorithm and the output format.
  4. Copy the result. It updates as you type.

Worked Example

RFC 4231 includes a test case you can use to check any HMAC implementation. With the key Jefe (as text) and the message what do ya want for nothing?, HMAC-SHA256 in hex is:

5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843

Switch the output to Base64 and the same 32 bytes are shown as W9zBRr9gdU5qBCQmCJV1x1oAPwidJzmDnexYuWTsOEM=. Both strings hold identical data, so make sure you compare like with like.

How HMAC Works

HMAC runs the hash function twice. First the key is padded to the hash's block size (64 bytes for SHA-1 and SHA-256, 128 bytes for SHA-384 and SHA-512) and XORed with a repeated 0x36 byte. That is prepended to the message and hashed. The key is then XORed with 0x5c, prepended to the first hash, and hashed again. Keys longer than the block size are hashed first. This two-step design is what protects HMAC from length-extension attacks that affect a naive hash(key + message), and it is why HMAC-SHA1 remains safe even though plain SHA-1 has known collisions.

Common Uses for HMAC

  • Webhook signatures. The sender signs each request with a shared secret and your server recomputes the HMAC to check it. The details differ: GitHub sends X-Hub-Signature-256: sha256=<hex> over the raw body, Shopify sends Base64 in X-Shopify-Hmac-Sha256, and Stripe signs the timestamp, a dot and the body, then sends the hex result as v1= in Stripe-Signature. Always hash the raw bytes you received, not JSON you have parsed and re-serialised.
  • API request signing. Some APIs ask clients to sign a canonical string made of the method, path, timestamp and body.
  • JWT HS256. The signature of an HS256 token is the HMAC-SHA256 of header.payload, Base64URL-encoded. You can check one with the JWT decoder.
  • Signed cookies and links. An HMAC on a cookie value or expiring download link stops users from editing it.

Security Notes

An HMAC is only as strong as its key. Use a long random key (32 bytes or more for SHA-256) rather than a word or phrase. When you check a tag in code, use a constant-time comparison function so attackers cannot learn the correct value from response timing. An HMAC proves integrity and origin, but it does not hide the message; encrypt the data as well if it is secret. To hash data without a key, use the hash generator.

Related Tools

Frequently Asked Questions

What is the difference between a hash and an HMAC?
A plain hash such as SHA-256 can be computed by anyone, so it only detects accidental changes. An HMAC mixes a secret key into the hash, so only someone who knows the key can produce or check the value. That makes it suitable for proving a message came from a trusted sender and was not modified.
Should I enter the key as text or hex?
Use whatever form your system stores the key in. Most webhook secrets and API secrets are used as plain UTF-8 text, so choose Text. Choose Hex if the key is raw random bytes written as hex digits, such as a 64-character key from a key generator. The same characters give different results in the two modes.
Why does my HMAC not match the one from my server?
The usual causes are whitespace or a trailing newline in the message, the key being interpreted as text when it should be hex or Base64-decoded, a different algorithm, or comparing hex output with Base64 output. Webhook providers also often sign a specific string, such as a timestamp joined to the raw request body, rather than the body alone.
Which HMAC algorithm should I use?
HMAC-SHA256 is the standard choice for new systems and is what most APIs, webhooks and JWT HS256 use. HMAC-SHA512 is also fine and gives a longer tag. HMAC-SHA1 is still secure as an HMAC despite SHA-1's collision weaknesses, but use it only when an existing protocol requires it.
Is my secret key sent anywhere?
No. The HMAC is computed with your browser's Web Crypto API. The message and key never leave the page.