HMAC generator

Computes a hash-based message authentication code (HMAC) using a secret key and your favorite hashing function.

loading HMAC generator
About this tool

What is an HMAC?

HMAC stands for hash-based message authentication code, defined in RFC 2104. It combines a hash function such as SHA-256 with a secret key to produce a short tag that proves two things at once: the message was not changed, and whoever made the tag knows the key. A plain hash proves only the first, because anyone can recompute it. Without the key, nobody can forge an HMAC.

You meet HMACs all the time without noticing. A webhook from GitHub or a payment provider carries a signature header that is an HMAC of the request body, so your server can check the call really came from them. A JWT signed with HS256 is an HMAC-SHA256 of its header and payload. Cookies and URLs are signed this way to stop tampering, and one-time codes such as TOTP are built on HMAC as well.

This HMAC generator computes the tag for a message and a secret key with the hash function you pick: MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, RIPEMD-160, the four SHA-3 sizes and the four Keccak sizes. The key can be written as text, hexadecimal, Base64 or Base64url, and the result can come out as hexadecimal, binary, Base64 or Base64url. It all runs in your browser, so the secret key you paste for debugging a signature is never uploaded. For a plain digest without a key, use the hash generator.

How to generate an HMAC online

  1. Type or paste the text in Message. The page opens with Hello, toolbox as an example. Paste the exact request body when you debug a webhook, with no trailing spaces.
  2. Enter the key in Secret key and choose Key format to say how it is written: text (UTF-8), hexadecimal, Base64 or Base64url. A key that is already in hex or Base64 must be flagged as such, or it will be read as plain text.
  3. Pick the Hash function. SHA-256 is the default and the most widely used.
  4. Choose the Output encoding: hexadecimal for most APIs, Base64 or Base64url for headers and tokens, binary for teaching.
  5. Read HMAC of your text and press Copy HMAC. It updates as you type.

Examples

The classic test vector

Message The quick brown fox jumps over the lazy dog, key key (text), SHA-256, hexadecimal:

f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8

Same HMAC, other encodings

The same input with the output set to Base64, then Base64url (the difference is the + that becomes -):

97yD9DBThCSxMpjmqm+xQ+9NWaFJRhdZl0edvC0aPNg=
97yD9DBThCSxMpjmqm-xQ-9NWaFJRhdZl0edvC0aPNg

A key written in hexadecimal

The text key is the three bytes 6b 65 79. Set Key format to hexadecimal and type 6b6579: you get the same value as above. If you leave the format on text, the key becomes the six characters 6b6579 and the result changes completely.

Other hash functions

Same message and key key, other functions:

SHA-1       de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9
SHA3-256    8c6e0683409427f8931711b10ca92a506eb1fafa48fadd66d76126f47ac2c333

SHA3 here is the NIST standard HMAC-SHA3. It differs from Keccak, the earlier version some blockchain libraries still call SHA3.

An empty key

With an empty key and SHA-256 the same message gives fb011e6154a19b9a4c767373c305275a5a69e8b68b0b4c9200c383dced19a416. The page shows a warning, because anyone can compute that value and it proves nothing.

Common errors and how to fix them

  • My HMAC does not match the one from the provider: check, in order, the exact bytes of the message (a trailing newline, parsed and re-serialized JSON, Windows line endings), the key (a secret with a prefix such as whsec_ may need to be used whole or Base64-decoded, as the provider documents) and the encoding of the result.
  • "The key is not hexadecimal. Use only the digits 0-9 and the letters a-f": the Key format is hexadecimal but the key has other characters. Switch to text or fix the key.
  • "A hexadecimal key needs an even number of digits": add or remove one digit, since each byte takes two.
  • "The key is not valid Base64": if the key has - or _, choose Base64url; if it has + or /, choose Base64.
  • "The key is empty, so anyone can compute this HMAC": type the secret to get the real tag.

Frequently asked questions

What is the difference between a hash and an HMAC?

A hash takes only the message; an HMAC takes the message and a secret key. Anyone can hash a message, but only the key holders can produce the right HMAC. That is why signatures use HMAC and integrity checks of public downloads use a plain hash.

Which HMAC algorithm should I use?

HMAC-SHA256 for almost everything. HMAC-SHA1 and even HMAC-MD5 are not broken in the same way as the bare hashes, but there is no reason to pick them for new work. Use them only when a legacy system requires it.

How long should the secret key be?

At least as long as the hash output: 32 bytes for SHA-256. Make it with a secure random source, for example the token generator, not with a human-chosen phrase.

Can an HMAC be reversed to find the key?

No. Recovering the key means guessing it, so a long random key is what keeps an HMAC safe.

Is HMAC the same as a JWT signature?

For the HS256, HS384 and HS512 algorithms, yes: the signature is an HMAC over header.payload, Base64url encoded. You can inspect those tokens with the JWT tool.

Compute an HMAC in code

JavaScript

import { createHmac } from 'node:crypto';

console.log(createHmac('sha256', 'key').update('The quick brown fox jumps over the lazy dog').digest('hex'));

Python

import hmac, hashlib

print(hmac.new(b'key', b'The quick brown fox jumps over the lazy dog', hashlib.sha256).hexdigest())

Go

// import "crypto/hmac", "crypto/sha256", "encoding/hex"
mac := hmac.New(sha256.New, []byte("key"))
mac.Write([]byte("The quick brown fox jumps over the lazy dog"))
fmt.Println(hex.EncodeToString(mac.Sum(nil)))

Bash

printf 'The quick brown fox jumps over the lazy dog' | openssl dgst -sha256 -hmac key
Report a problem