Cryptography guides

Find which HMAC input differs when two authentication tags disagree

·

Two tools agree on an HMAC when they use the same algorithm, secret key bytes and message bytes. Start with a published test vector to confirm the settings. Then change one input at a time, including the output encoding, to find the mismatch.

HMAC Generator

Verify a published HMAC-SHA-256 test vector

Choose Load RFC 4231 example. Algorithm is HMAC-SHA-256, the key is Hex and contains twenty bytes of 0b, and the message is the eight UTF-8 bytes for Hi There. Choose Calculate HMAC. Compare its 32-byte tag with the RFC result in the example below.

Paste that result into Expected HMAC and calculate again. The tool reports a match. Change a single digit in the expected value to see a mismatch. Download result saves the generated tag in its selected text encoding. The key and message stay on the page.

RFC 4231 test case 1 HMAC-SHA-256 tag in hex
b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7

RFC 4231, HMAC test case 1

Distinguish key bytes from output encoding

Clear Expected HMAC and choose Base64URL for the result. The printed tag changes to the unpadded form below. Its decoded content is still 32 bytes. Choose your protocol’s expected alphabet and padding when copying a tag.

Change Secret key encoding from Hex to UTF-8 text without editing the key. The key count changes from 20 bytes to 40 bytes, because each printed digit becomes a separate byte. The different key produces a different authentication tag.

The same HMAC-SHA-256 tag in unpadded Base64URL
sDRMYdjbOFNcqK_OrwvxK4gdwgDJgz2nJuk3bC4yz_c

RFC 4648, Base16 and Base64URL encodings

Check invisible bytes at the end of a saved message

Reload the example and add one line break after Hi There. The message count rises to 9 and the hex tag begins 1cb5b866. To test a CRLF ending, switch Message encoding to Hex and type 48692054686572650d0a. The count is 10 bytes and the hex tag starts d6e743fc.

Use encoded bytes when a browser text field would change line endings. For an integration test, use the exact bytes that its documentation says to authenticate. Timestamp prefixes, body encoding and whitespace can all affect those bytes.

W3C Web Cryptography API, HMAC

Common questions

Why does a valid HMAC need the original secret key?

HMAC is a symmetric message authentication code. The verifier computes the same function using the same key bytes and message. A bare hash uses the message alone.

Can I verify a protocol’s truncated tag here?

This workspace compares a full tag. For HMAC-SHA-256, enter all 32 bytes in the selected encoding. Use your protocol’s rules to verify its truncated form.

Sources

Put it into practice.

Related guides