Length-extension attack
A length-extension attack exploits an iterative hash construction to compute a hash for an extended message from the original hash and message length.
Hacking Cryptography
A length-extension attack computes a valid hash for an extended message from a hash of the original message and its length when the hash construction exposes a reusable internal state.
- How it is used
- Demonstrates how naive API authentication with secret-prefix hashing can be forged.
- How deep it goes
- Works through padding generation, SHA-256 state replacement, and a forged HTTP request using OpenSSL.
- Examples
- Uses a SHA-256-signed bank transaction, extending amount=10 into a forged amount=424242 request.
- What you can do after
- Diagnose/debug with it by recognizing forgeable secret-prefix MAC authentication and selecting HMAC or sponge-based hashes.
Read how Hacking Cryptography explains length-extension attack →
Real-World Cryptography
A length-extension attack is an attack that exploits iterative hash constructions to compute a hash for an extended message when given a hash of an original message and its length.
- How it is used
- Explains why SHA-2 cannot safely authenticate secret-prefixed messages such as cookies.
- How deep it goes
- Works through digest continuation, padding, and appended data, but stops short of implementation details.
- Examples
- Uses a user=bob cookie extended with &admin=true and illustrates successful digest reuse.
- What you can do after
- Evaluate whether secret-prefixed SHA-2 authentication is vulnerable and avoid using it as a homemade MAC.
Read how Real-World Cryptography explains length-extension attack →
What to know first
- hash functionA hash function is a deterministic function that maps input data to a hash value, typically of fixed size, for uses such as lookup, indexing, comparison, or integrity checking.
- Merkle–Damgård constructionThe Merkle–Damgård construction builds an arbitrary-length hash function by iteratively applying a fixed-size compression function to padded message blocks.
- SHA-2SHA-2 is a standardized family of cryptographic hash algorithms that produces fixed-length digests, including SHA-224, SHA-256, SHA-384, and SHA-512.
- SHA-256SHA-256 is a standardized cryptographic hash function in the SHA-2 family that maps input data to a fixed-length 256-bit digest.
Related concepts
- HMACcontrasts with · HMAC is a message authentication code constructed from a cryptographic hash function and a shared secret key to verify a message’s integrity and authenticity.
- secret-prefix hashinguses · Secret-prefix hashing is an authentication construction that prepends a secret to a message before hashing the resulting data.
- secret-suffix MACaddressed by · A secret-suffix MAC is a message authentication construction that appends the secret key to the message before hashing.
- SHA-2uses · SHA-2 is a standardized family of cryptographic hash algorithms that produces fixed-length digests, including SHA-224, SHA-256, SHA-384, and SHA-512.
- message authentication codeuses · A message authentication code is a keyed cryptographic value generated from a message to verify its integrity and authenticity.
- sponge constructioncontrasts with · A sponge construction absorbs input into a state through repeated permutation applications and squeezes output from that state, supporting outputs of arbitrary length.
Questions about length-extension attack
What is length-extension attack?
A length-extension attack exploits an iterative hash construction to compute a hash for an extended message from the original hash and message length.
Which Manning books explain length-extension attack?
Hacking Cryptography and Real-World Cryptography.
What should I know before learning length-extension attack?
Manning books that teach length-extension attack usually introduce hash function, Merkle–Damgård construction, SHA-2 and SHA-256 first.
What is the difference between length-extension attack and HMAC?
A length-extension attack exploits an iterative hash construction to compute a hash for an extended message from the original hash and message length. HMAC is a message authentication code constructed from a cryptographic hash function and a shared secret key to verify a message’s integrity and authenticity.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

