Every signature fails. You have checked the algorithm twice, copied the key character by character, and the receiving system still rejects each request. It is tempting to blame the API, or to rewrite the signing code. Resist both. The most common cause of a total HMAC failure is not the code. It is the encoding of the key: the same secret, read two ways, becomes two different keys.
This guide explains why a key is bytes rather than text, how the mix-up happens, and the order to diagnose a mismatch in, testing each step with the free HMAC Generator on Ovatly.
Contents
- Quick answer
- A key is bytes, not characters
- The two ways the encoding goes wrong
- The same trap waits in the output
- A diagnostic order that finds the fault fast
- How to check an HMAC, step by step
- Worked example: the published fox test value
- Limitations and honest tradeoffs
- Common mistakes
- FAQs
- Conclusion
- References
Quick answer
If every signature fails, suspect the key encoding first. A secret stored as hex characters must be decoded from hex into bytes before signing; typed in as plain text, those characters are themselves the bytes, and the signature comes out completely different. After the key, check for a trailing newline in the message, then whether a JSON payload was re-serialised instead of signed raw. The HMAC Generator lets you test each reading in seconds: paste the message, enter the key, pick SHA-256, SHA-384 or SHA-512, compute, and compare.
A key is bytes, not characters
An HMAC signs with a sequence of bytes, not a word. Your secret key exists as bytes first; the characters you see in a dashboard or a config file are only a way of writing those bytes down.
Plain text is one writing system: each character stands for itself, one byte each. Hex is another: pairs of characters from 0 to 9 and a to f stand for a single byte. The three bytes of the word key can be written as the text key, or in hex as 6b6579. Both describe the same secret, if the reader knows which notation it is reading.
Hand the wrong notation to a signing routine and it will not complain. It will quietly sign with the characters themselves as bytes: six bytes where three were meant. The output is a perfectly valid HMAC of the wrong key, and nothing errors.
The two ways the encoding goes wrong
The mix-up runs in both directions. The first is a hex key read as text. A provider displays your signing secret as a long hex string, because hex is a safe way to show arbitrary bytes. Your code expects a text key, so the hex string goes in as-is, and the signer uses the characters themselves as the key bytes, doubling the length and changing every value.
The second is a text key read as hex. Your secret is an ordinary word, but a library setting tells the signer to decode the key from hex first. The letters of your key are reinterpreted as hex digits, or silently mangled, and again the bytes that reach the hash are not your secret. Both failures share a symptom: total failure, every request, from the first test.
The same trap waits in the output
Fixing the key does not end the encoding story, because the signature itself also comes in two writings. The generator shows each result in hex and in Base64 for this reason: they are the same bytes, encoded two ways, and the strings look nothing alike. Compare your hex output against a system expecting Base64 and a correct signature reports as a mismatch: the signing is right and the comparison is wrong. Produce the format the receiver specifies, and compare like with like.
A diagnostic order that finds the fault fast
When a signature fails, work through the suspects in order of how often they are guilty. First, the key encoding. Second, the message bytes: whitespace and line endings are part of the message, so a trailing newline changes the signature completely. Third, the payload's history: a JSON body parsed and written back out before signing may have shifted key order and spacing, and the signature covers those new bytes. Fourth, the algorithm: confirm both sides mean SHA-256, and that a random key of 32 bytes or more is in use. Changing one variable at a time against a known example tells you which layer was lying.
How to check an HMAC, step by step
- Open the HMAC Generator and paste the exact message, byte for byte. Type it by hand if your copy may carry an extra newline.
- Enter the secret key as plain text, and pick the algorithm the system specifies.
- Click Compute HMAC and note the signature in the format the receiver expects, hex or Base64.
- Change only the key reading, using a test key you control in both notations, and compare which reading reproduces the expected value.
- If neither matches, strip any trailing newline and compute again. Verification means signing again and comparing, because an HMAC cannot be decrypted.
Worked example: the published fox test value
The standard published example uses the key key and this message:
The quick brown fox jumps over the lazy dog
Signed with HMAC-SHA-256, the result in hex is:
f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8
Now watch the encoding mistake happen. The key key written in hex is 6b6579. Type 6b6579 into the key box as plain text and the tool signs with six bytes, the characters 6, b, 6, 5, 7 and 9, instead of the three bytes of the word key. The signature bears no resemblance to the published value, even though the message and algorithm are identical. Add one trailing newline to the message and the value changes completely again: the second trap, in the same example.
Limitations and honest tradeoffs
An HMAC authenticates a message; it does not encrypt it. Anyone holding the key can sign and verify, so HMAC fits shared-secret setups and cannot prove which of two key holders sent a message. The generator offers SHA-256 and stronger only, by design. It processes everything on your device and sends nothing anywhere, but never sign production secrets on a machine you do not trust. A matching signature proves the message and key agree; it says nothing about whether the key itself was a good one.
Common mistakes
- Pasting a hex key as plain text. The characters become the key bytes. Decode hex first, or test with a key you control in both forms.
- Reading a text key as hex. A library default quietly reinterprets your secret. Check the setting before the code.
- Comparing hex against Base64. The same signature in two encodings looks like a total mismatch. Generate the format the receiver specifies.
- Signing a re-serialised payload. Parse and re-emit a JSON body and its bytes change. Sign the raw bytes that were sent.
- Ignoring the trailing newline. Editors add them silently, and the signature covers them. When in doubt, type the message fresh.
FAQs
Why does my HMAC fail even though the key looks identical on screen? Because the screen shows characters, and the HMAC signs bytes. A key stored as hex characters, such as 6b6579, is three bytes when decoded from hex. Typed in as plain text, those six characters are six different bytes, so the signature is completely different.
How do I know whether my key is hex or plain text? Check where the key came from. A long string of only 0 to 9 and a to f characters in documentation or a dashboard is usually hex. A secret or signing key with no mention of hex is usually plain text. When in doubt, test both readings against a published example value.
Can I compare a hex signature against a Base64 one? Not directly. Hex and Base64 are two encodings of the same bytes, so the strings look nothing alike even when the signature is correct. Generate the output in the format the receiving system expects, and then compare.
What else causes an HMAC mismatch when the key is right? The next suspects are byte-level differences in the message: a trailing newline added by an editor, a different text encoding, or a JSON payload that was parsed and re-serialised instead of signed as the raw bytes that arrived. A changed message means a changed signature.
Is it safe to test my real production key in an online tool? The Ovatly HMAC Generator runs on your device and sends nothing anywhere, but the safe habit is still to test with example keys first. Never sign production secrets on a machine you do not trust, or in a tool whose behaviour you have not verified.
Conclusion
Most total HMAC failures are not cryptography failures. They are reading failures: the right secret, read the wrong way. Treat the key as bytes, decide whether it is written as text or hex, compare signatures in one encoding, and the mystery ends. Before wiring a real integration, prove your setup against a known answer first, the subject of our companion guide, How to Test an HMAC Example Before Wiring Up an API. For more guides in this series, see our Security and Privacy guides.
References
- RFC 2104, HMAC: Keyed-Hashing for Message Authentication: https://www.rfc-editor.org/rfc/rfc2104
- NIST FIPS 198-1, The Keyed-Hash Message Authentication Code (HMAC): https://csrc.nist.gov/pubs/fips/198-1/final