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 →
Related concepts
- secret-suffix MACaddressed by · A secret-suffix MAC is a message authentication construction that appends the secret key to the message before hashing.
- HMACcontrasts with · HMAC is a keyed message authentication code constructed from a cryptographic hash function and a secret key to authenticate data and verify its integrity.
- secret-prefix hashinguses · Secret-prefix hashing is an authentication construction that prepends a secret to a message before hashing the resulting data.
- 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.
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.

