Why Secret-Prefix Hashes Fail: The Merkle-Damgard Root Cause
Archi4 min read
Contents
The Bug Lives in the Padding, Not the Hash#
Take a shared secret, glue a message to its front, run the pair through SHA-256, and treat the digest as a signature. The pattern shows up in API docs, internal service calls, and a handful of payment gateways whose signing code has not changed since 2012. SHA-256 itself is fine here. The construction around it is what breaks.
The failure has a name, length extension, and a mechanical cause that sits in the compression loop rather than in any weakness of the hash function.
One state, carried forward#
SHA-256 does not process a message as a single object. It chops the input into 64-byte blocks, starts from a fixed 256-bit initial value, and feeds each block through a compression function that mixes the block into the current state. After the last block, whatever state remains is the digest.
That design is deliberate. It lets a machine hash a file larger than memory, one block at a time. It also means the digest is not a summary of the message so much as the machine's internal position after reading it.
If you know that position, you can keep going. Load the digest as the new initial value, feed more blocks, and the result is a valid hash of a longer string. No key required.
Padding is the lever#
The attacker cannot simply append bytes. SHA-256 pads before hashing: a single 0x80 byte, then zeros until the length is 56 modulo 64, then an 8-byte big-endian count of the message length in bits.
So a signature over secret || message is really a signature over secret || message || pad. The attacker knows the tag and needs one more number, the byte length of the secret, to rebuild that padding exactly. When the key is a 32-byte random value or a 40-character hex string, the length is fixed and often documented.
With the length in hand, the attacker computes the glue bytes, appends whatever payload they want, and produces a tag that the server will accept for the forged string.
What the forged request looks like#
Suppose a service signs a query string as sha256(api_secret + query). The caller sends amount=100&to=alice plus a tag. An attacker who intercepts one request can append the glue padding and then &amount=9000&to=mallory.
The server recomputes the hash over secret plus the full received string and gets the same tag, because the compression function was restarted from a known state. The parser sees junk bytes from the padding, usually tolerated as an unnamed parameter, and then sees the injected keys. Many query parsers keep the last value when a key repeats, which hands the attacker the win.
In 2009, researchers demonstrated exactly this against a photo-sharing service whose API signature was an MD5 of the secret followed by the request parameters. Nobody had broken MD5's compression function for that attack. The signature scheme simply exposed its internal state.
Which constructions share the flaw#
MD5, SHA-1, SHA-256, SHA-512 and RIPEMD-160 all use the Merkle-Damgard structure, so all of them leak their final state this way. The block size changes the arithmetic but not the attack. SHA-256 uses 64-byte blocks and a 64-bit length field. SHA-512 uses 128-byte blocks.
SHA-3 does not. Its sponge construction absorbs data into a wider state and outputs only part of it, keeping the rest hidden, so an attacker cannot reconstruct the position needed to continue. BLAKE2 and BLAKE3 are also built to avoid this. Swapping hash families is the expensive fix, and usually the wrong first move.
One partial brake deserves mention. If a system truncates the tag, say to 128 bits, the attacker only sees half the chaining state and cannot restart the loop. Truncation is a mitigation, not a design, and it comes with its own collision math.
The standard repair and its cost#
HMAC wraps the hash in two keyed passes: an inner hash over the key mixed with the message, then an outer hash over the key mixed with that inner digest. The tag is no longer a resumable state for the message, so appending data produces nothing useful.
The cost is small. A 1 KB payload goes through the inner compression loop once, then a single extra block for the outer pass. On any hardware shipped in the last decade, that is noise next to the network round trip.
A length-prefixed construction also works: hash the key length, then the key, then the message. It is sound and it is rarely worth inventing when HMAC already exists in every standard library.
Finding it in your own code#
The search is dull and effective. Look for calls where a key is concatenated with a message and passed to a hash: patterns like hash(secret +, md5(key +, sha1(token +, and any helper named sign that takes two arguments and calls a digest directly. Check client libraries too, since a mobile SDK can carry the same bug to thousands of devices.
Then check what the server does with the extra bytes. A parser that drops unknown parameters quietly is the difference between a theoretical flaw and a live forgery. Log the raw signed string and compare it to the parsed parameters. If they disagree in length, the signing scheme is holding the door open.
More articles