Skip to content

HMAC generator

Compute an HMAC of a message with a secret key, and check a signature you received, such as a webhook’s.

Runs in your browser. Nothing you paste is sent to us.

Key and message

A sample key and message are filled in. Use your own; they stay in your browser.

For a webhook, paste the raw request body exactly as received.

HMAC

HMAC-SHA256
 

How to use it

  1. Enter the secret key, as text, hex or Base64.
  2. Paste the message exactly as it was signed, such as the raw request body.
  3. Choose the hash: HMAC-SHA256 is the common choice.
  4. Paste the signature you received to see whether it matches.

What to know

An HMAC proves that a message came from someone holding the key and was not changed. Webhooks, API request signing and HS256 JSON Web Tokens all use it.

Signatures fail most often because the bytes differ: a body re-serialized as JSON, a different line ending, or a key read as text when it was hex.

Keycloak keeps an HMAC key in each realm, from the hmac-generated key provider, for tokens only Keycloak itself checks. Your applications should verify Keycloak’s access tokens with the realm’s public keys instead.

What an HMAC is

An HMAC (hash-based message authentication code) is a short tag computed from a message and a secret key. Anyone holding the key can compute the same tag and compare. If the tags match, the message came from someone with the key and was not changed on the way.

A plain hash cannot do this, because anyone can compute a hash. An HMAC mixes the secret into the computation, so without the key a valid tag cannot be made.

HMAC is defined in RFC 2104 and FIPS 198-1. It works with any hash function; HMAC-SHA256 is the common choice today.

How HMAC is computed

HMAC hashes the message twice, with the key mixed in each time through two fixed padding constants. This construction stays secure even with hash functions that would be weak if you simply hashed the key and the message together.

The HMAC construction (RFC 2104)
HMAC(K, m) = H( (K' xor opad) || H( (K' xor ipad) || m ) )

K'    the key, padded with zeros to the hash's block size
      (or hashed first if it is longer than the block)
ipad  the byte 0x36 repeated, opad  the byte 0x5c repeated
A worked example with sample values
key:     sample-secret
message: {"event":"LOGIN"}
HMAC-SHA256 (hex):
418b27555f8a69cddcf7f6f8838c44c607e02e87d8ce3edad54fd6e8485cc915

An empty key is allowed by the RFC but proves nothing, since anyone can use an empty key. This tool computes it, matching the standard, and warns you.

Where HMACs are used

HMACs are everywhere two parties share a secret and need to trust messages between them.

Webhook signatures

Most webhook senders sign each request body with HMAC-SHA256 and a shared secret, and put the tag in a header, often as hex with a prefix such as sha256=. The receiver recomputes the HMAC over the raw body and rejects the request if the tags differ. Many also sign a timestamp to stop replays.

HS256 JSON Web Tokens

A JWT with alg HS256 is signed with HMAC-SHA256 over the encoded header and payload. Everyone who can verify such a token can also create one, because they share the key. That is why HS256 suits tokens a single service issues and checks itself, and asymmetric algorithms such as RS256 suit tokens that many services verify.

RFC 7518 requires an HMAC key at least as long as the hash output: 256 bits for HS256, 512 for HS512.

Keycloak’s internal tokens

Each Keycloak realm has an HMAC key, from the hmac-generated key provider, used for tokens that only Keycloak itself issues and checks. Your applications never need it. They verify Keycloak’s access and ID tokens with the realm’s public keys from its JWKS endpoint, /realms/<realm>/protocol/openid-connect/certs.

One-time passwords

HOTP and TOTP codes are HMACs of a counter, truncated to six or eight digits. See the TOTP generator for the details.

Signed cookies and state values

Frameworks often sign session cookies or OAuth state values with an HMAC so the server can tell whether the browser changed them, without keeping them in a store.

What this tool does and does not do

  • It computes HMAC-SHA256, HMAC-SHA384, HMAC-SHA512 and HMAC-SHA1 with Web Crypto, in your browser.
  • It reads the key as UTF-8 text, hex or Base64, and the message as UTF-8 text.
  • It shows the result in hex, Base64 or Base64url.
  • It compares the result with a signature you paste, given in hex or Base64, with or without a sha256= style prefix.
  • It does not take binary messages or files, and it does not know any particular service’s signing scheme. Some services sign a timestamp and body joined together; build that exact string first.

How to check a webhook signature

  1. Find the signing secret in the sender’s settings, and note whether it is shown as text, hex or Base64.
  2. Enter the secret and choose the matching key format.
  3. Paste the raw request body exactly as received, before any JSON parsing.
  4. Choose the hash the sender documents, usually HMAC-SHA256.
  5. Paste the signature header’s value into the compare field and read the result.

Use test secrets and sample bodies where you can. A real webhook secret lets anyone forge requests to your endpoint.

Why signatures fail to match

An HMAC changes completely if a single byte changes, so mismatches almost always come from feeding slightly different bytes.

CauseWhat happensFix
Body re-serializedA framework parses the JSON and you hash JSON.stringify of it; spacing or key order differsHash the raw body bytes as received
Key formatThe secret is hex or Base64 but was used as text, or the reverseDecode the key the way the sender does
Wrong prefix handlingThe header holds sha256= plus hex, compared with bare hexStrip the prefix before comparing
EncodingHex compared with Base64Compare in one encoding
Extra data signedThe sender signs a timestamp and body togetherRebuild the exact signed string
Line endingsA body copied through a terminal gained CRLFUse the bytes from the request, not a copy

Choosing an HMAC key

Make the key random and at least as long as the hash output: 32 random bytes for HMAC-SHA256, 64 for HMAC-SHA512. Longer keys do not hurt but add nothing past the hash’s block size, because longer keys are hashed down first.

Do not use a password or a word as an HMAC key. A guessable key can be found offline from any message and its tag. The client secret generator makes suitable keys in hex or Base64url.

Security notes

  • Compare tags in constant time on the server, for example with crypto.timingSafeEqual in Node.js or hmac.compare_digest in Python. A normal string comparison can leak through timing how many leading bytes matched.
  • Verify before you parse. Check the HMAC on the raw body, then parse the JSON.
  • Reject old requests. Include a timestamp in what is signed and refuse requests outside a short window, so captured requests cannot be replayed later.
  • HMAC-SHA1 has no known practical attack as a MAC, but use HMAC-SHA256 for anything new.
  • An HMAC is not encryption. The message is still readable by anyone who sees it.

Best practices

  • Use a separate key per integration and per environment.
  • Keep keys in a secrets manager and rotate them when people with access leave.
  • Support two active keys during rotation, so senders can switch without downtime.
  • Document the exact signed bytes, the hash, the key format and the header format.
  • Prefer asymmetric signatures when many parties must verify but only one should sign.

Verifying an HMAC in your own code

The checks this page does by hand are a few lines on a server. The important parts are the raw body, the decoded key, and a constant-time comparison.

Node.js: verify a hex HMAC-SHA256 signature
import { createHmac, timingSafeEqual } from 'node:crypto';

function verify(rawBody: Buffer, header: string, secret: string) {
  const expected = createHmac('sha256', secret).update(rawBody).digest();
  const given = Buffer.from(header.replace(/^sha256=/, ''), 'hex');
  return given.length === expected.length && timingSafeEqual(given, expected);
}

The length check comes first because timingSafeEqual throws on buffers of different lengths. Read the body as raw bytes; most web frameworks have an option to keep the raw body alongside the parsed JSON for exactly this reason.

Questions

Frequently asked questions

What is the difference between a hash and an HMAC?

A hash depends only on the message, so anyone can compute it. An HMAC also depends on a secret key, so only key holders can compute or check it. That makes an HMAC proof of origin and integrity.

How do I verify a webhook HMAC signature?

Recompute HMAC-SHA256 over the raw request body with the shared secret, in the encoding the sender uses, and compare it with the signature header in constant time. Reject the request if they differ or if its timestamp is too old.

How long should an HMAC secret be?

At least as long as the hash output: 32 random bytes for HMAC-SHA256 and 64 for HMAC-SHA512. RFC 7518 requires this minimum for HS256 and HS512 JWT keys.

Is HMAC-SHA1 still secure?

Yes, as a MAC. SHA-1’s collision weakness does not give a practical attack on HMAC-SHA1. Still, choose HMAC-SHA256 for new systems.

Why does my HMAC not match?

The bytes differ somewhere: a re-serialized JSON body, a key decoded differently, a prefix such as sha256=, or hex compared with Base64. Hash the exact raw body with the exact key bytes.

Can an HMAC be reversed to get the message or key?

No. But if the key is short or guessable, an attacker who has a message and its tag can find the key by trying candidates. Use random keys.

Should I use HS256 or RS256 for JWTs?

Use HS256 only when the same party issues and verifies the token, since the verifier can also sign. Use RS256 or ES256 when other services verify, so they need only a public key. Keycloak signs access tokens with RS256 by default.

What is the difference between HMAC and a digital signature?

An HMAC uses one shared key for making and checking tags, so every verifier could also forge one. A digital signature uses a private key to sign and a public key to verify. Use signatures when many parties verify but only one should sign.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.