Length Extension Attack
An attack against Merkle-DamgΓ¥rd hash functions (MD5, SHA-1, SHA-256) that allows appending data to a message and computing a valid hash without knowing the secret.
Length Extension Attack exploits the iterative structure of Merkle-DamgΓ¥rd hash functions (MD5, SHA-1, SHA-256). Given H(secret || message), an attacker can compute H(secret || message || padding || extension) without knowing the secret.
Prevention
- Use HMAC \- the only correct solution for message authentication
- Use SHA-3 or BLAKE2/3 (not vulnerable due to sponge/different construction)
- Never use raw hash concatenation (H(secret || message)) for authentication
Why HMAC?
HMAC's nested construction (H(K XOR opad || H(K XOR ipad || message))) prevents length extension by design. The outer hash operation on the inner hash output makes it impossible to continue the hash state. Always use HMAC – not ad-hoc constructions.