How to use the hmac sha256 generator
- Enter your secret key and choose whether it is plain text or hex bytes (for example
4a656665is the text "Jefe"). - Type or paste the message and pick text or hex for it as well.
- Select the algorithm: HMAC-SHA256, SHA-1, SHA-384 or SHA-512.
- Read the hex and base64 results below. The output updates as you type.
How HMAC works
HMAC (Hash-based Message Authentication Code) combines a secret key with a message using a cryptographic hash function. The algorithm defined in RFC 2104 pads the key to the hash block size, then computes two nested hashes:
HMAC(K, m) = H((K' XOR opad) || H((K' XOR ipad) || m))
Here, K' is the key padded or hashed to exactly one block length. ipad is the byte 0x36 repeated for the full block, and opad is 0x5c repeated. For HMAC SHA256, the block size is 64 bytes and the inner hash produces 32 bytes. If the provided key exceeds 64 bytes, it is first hashed with SHA-256 to produce a 32-byte key, which is then zero-padded to 64 bytes.
RFC 4231 test vectors
RFC 4231 publishes HMAC test vectors for the SHA-2 family, and RFC 2202 does the same for SHA-1. You can reproduce any row below by choosing the matching key format, so they double as a sanity check for your own code. Keys are shown in hexadecimal.
| Case | Key (hex) | Message | Algo | Hex output |
|---|---|---|---|---|
| 1 | 0b x 20 | Hi There | SHA-256 | b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7 |
| 2 | 4a656665 | what do ya want for nothing? | SHA-256 | 5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843 |
| 2 | 4a656665 | what do ya want for nothing? | SHA-512 | 164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737 |
| 6 | aa x 131 | Test Using Larger Than Block-Size Key - Hash Key First | SHA-256 | 60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54 |
Case 6 uses a 131-byte key, longer than the 64-byte SHA-256 block. The key is hashed first and the 32-byte digest is then zero-padded, which is the rule this tool follows.
Choosing between SHA1, SHA256, SHA384 and SHA512
The choice of hash algorithm determines both security strength and output length. SHA-1 produces a 160-bit (20-byte) digest and is no longer recommended for new systems due to known collision weaknesses, though it remains common in legacy protocols like OAuth 1.0a. SHA-256 produces a 256-bit (32-byte) digest and is the most widely used variant for HMAC today, including in JSON Web Tokens (JWTs) and webhook signatures. See the SHA256 hash generator if you need a plain hash rather than an HMAC.
SHA-384 and SHA-512 use 128-byte blocks instead of 64-byte blocks and produce 48-byte and 64-byte digests respectively. They offer higher security margins and perform better on 64-bit processors. Use HMAC SHA512 when compliance requirements or threat models demand more than 256 bits of security.
Common uses for HMAC
HMAC is used wherever two parties share a secret key and need to verify that a message has not been altered. Common applications include:
- Webhook verification: services like Stripe, GitHub and Slack sign outgoing payloads with HMAC SHA256 so receivers can confirm authenticity.
- API request signing: AWS Signature Version 4 derives signing keys through a chain of HMAC SHA256 operations.
- JSON Web Tokens: the HS256, HS384 and HS512 algorithms in JWTs are HMAC-based. Tools like the JWT decoder can inspect these tokens.
- TOTP and HOTP: time-based and counter-based one-time passwords rely on HMAC SHA1 by default. See the HOTP generator for details.
Hex versus base64 encoding
The raw output of HMAC SHA256 is a sequence of 32 bytes. To transmit or store this value as text, it must be encoded. Hexadecimal encoding represents each byte as two characters (0-9, a-f), producing a 64-character string. Base64 encoding maps every three bytes into four printable characters, yielding a shorter 44-character string (with padding). Most APIs accept either format, but webhook documentation will specify which one to use. Always compare HMAC values using constant-time comparison to prevent timing attacks.
Worked examples
| Example | Input | Result |
|---|---|---|
| RFC 4231 case 2 (SHA-256) | Secret key: Jefe; Key format: text; Message: what do ya want for nothing?; Message format: text; Algorithm: sha256 | Hex: 5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843; Base64: W9zBRr9gdU5qBCQmCJV1x1oAPwidJzmDnexYuWTsOEM=; Algorithm: HMAC-SHA256; Length: 32 bytes |
| RFC 4231 case 1 (SHA-512) | Secret key: 0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b; Key format: hex; Message: Hi There; Message format: text; Algorithm: sha512 | Hex: 87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854; Base64: h6p83qXvYZ1P8LQkGh1ssCN59OLOTsJ4etCzBUXhfN7aqDO31rinAgOLJ06uo/Tkvp2RTuth8XAuaWwgOhJoVA==; Algorithm: HMAC-SHA512; Length: 64 bytes |
| Webhook style signature | Secret key: whsec_test_secret; Key format: text; Message: {"event":"invoice.paid","id":"evt_123"}; Message format: text; Algorithm: sha256 | Hex: 5721ed99eb82b350024cb09432b13914d395bdbe68127428b97f8b35e4777b52; Base64: VyHtmeuCs1ACTLCUMrE5FNOVvb5oEnQouX+LNeR3e1I=; Algorithm: HMAC-SHA256; Length: 32 bytes |
| Message as hex bytes | Secret key: key; Key format: text; Message: 00ff10; Message format: hex; Algorithm: sha1 | Hex: 67f33d41117a01fc3824072d0b7f31711124d8f2; Base64: Z/M9QRF6Afw4JActC38xcREk2PI=; Algorithm: HMAC-SHA1; Length: 20 bytes |
FAQ
What is HMAC SHA256?
HMAC SHA256 is a message authentication code that uses the SHA-256 hash function combined with a secret key. It verifies both the integrity and authenticity of a message.
Is HMAC the same as a regular hash?
No. A regular hash like SHA-256 only ensures data integrity. HMAC adds a secret key, so only someone who knows the key can generate or verify the correct MAC.
Should I enter the key as text or hex?
Use text when your key is a passphrase or an API secret string, and hex when it is raw bytes such as a random 32-byte key. The same characters give different results in the two modes, so match what your other system does.
Can I use a short key for HMAC?
You can, but short keys reduce security. RFC 2104 recommends a key at least as long as the hash output: 32 bytes for SHA-256, 64 bytes for SHA-512.
Why does my HMAC output differ from another tool?
Check that you are using the same algorithm, the same byte representation for the key, and the same output format (hex or base64). Even a trailing newline in the message changes the result.
Is SHA-1 safe for HMAC?
HMAC-SHA1 is still considered secure for authentication because the HMAC construction mitigates the collision attacks that weaken plain SHA-1. However, SHA-256 is preferred for new projects.
Does this tool send my key to a server?
No. Hashing runs locally in your browser. Your key and message never leave your device.
Can I use HMAC to store passwords?
No. HMAC proves that a message came from someone holding a key. For password storage use a slow, salted function such as bcrypt (see the bcrypt generator), never a bare SHA-1 or MD5.