Cryptographic Hash Functions
One-way fingerprints of data — properties (preimage, second-preimage and collision resistance), the avalanche effect, the birthday bound, MD5 and SHA-1 breaks, SHA-2, SHA-3 and BLAKE, Merkle–Damgård and length-extension attacks, and uses from integrity checks to Merkle trees and blockchains.
A cryptographic hash function maps input of any size to a fixed-size digest (fingerprint), such that the digest is easy to compute but practically impossible to reverse or forge.
SHA-256("hello") = 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
SHA-256("hello.") = a completely different, unrelated-looking 64-hex-digit value
SHA-256(an entire 4 GB ISO) = still 64 hex charactersimport hashlib
print(hashlib.sha256(b"hello").hexdigest())
print(hashlib.blake2b(b"hello", digest_size=32).hexdigest())
with open("ubuntu.iso", "rb") as f: # hash a large file in chunks
print(hashlib.file_digest(f, "sha256").hexdigest())Don't confuse these with the non-cryptographic hashes used by hash tables (Hash tables) — MurmurHash or xxHash are fast but offer no security against adversaries.
Security properties
| Property | Meaning | Generic attack cost (n-bit hash) |
|---|---|---|
| Preimage resistance (one-way) | Given a digest h, can't find any m with H(m) = h | 2ⁿ |
| Second-preimage resistance | Given m₁, can't find a different m₂ with the same hash | 2ⁿ |
| Collision resistance | Can't find any two different messages with the same hash | 2ⁿ⁄² (birthday bound) |
Plus the avalanche effect: flipping one input bit changes about half the output bits, unpredictably. The output should look like random noise.
The birthday paradox
In a room of just 23 people, there's a 50% chance two share a birthday — because there are 253 pairs. Likewise, hashing about random inputs finds a collision with good probability. A 128-bit hash gives only ~64-bit collision resistance — too weak. That's why modern hashes are at least 256 bits.
The hash function zoo
| Function | Output | Status |
|---|---|---|
| MD5 | 128 bits | Broken — collisions in seconds on a laptop; used to forge a CA certificate (2008) and by the Flame malware |
| SHA-1 | 160 bits | Broken — practical collision (SHAttered, 2017: two different PDFs, same hash); chosen-prefix collisions since 2020 |
| SHA-256 / SHA-512 (SHA-2) | 256 / 512 bits | Secure; the workhorse (TLS, Bitcoin, code signing) |
| SHA-3 (Keccak) | 224–512 bits | Secure; a different design (sponge) as a hedge against SHA-2 weaknesses |
| BLAKE2 / BLAKE3 | Variable | Secure and very fast; BLAKE3 is parallelisable |
Rules: never use MD5 or SHA-1 for security. Non-security uses — checksums against accidental corruption, cache keys — are fine, though even there faster non-cryptographic hashes may be better.
How they're built
Merkle–Damgård (MD5, SHA-1, SHA-2): pad the message, split it into blocks, and run a compression function over each block, chaining an internal state; the final state is the digest.
Length-extension attack: because the digest is the final internal state, an attacker who knows H(secret ∥ message) and the length of the secret can compute H(secret ∥ message ∥ padding ∥ extra) without knowing the secret. So SHA256(key + message) is not a secure MAC — use HMAC (MACs). SHA-3 (a sponge construction that keeps part of its state hidden), BLAKE2/3 and truncated SHA-512/256 are immune.
Uses
| Use | How |
|---|---|
| File integrity / downloads | Publish the SHA-256 of a release; users compare (needs the hash from a trusted source — or better, a signature) |
| Digital signatures | Sign the hash of a document, not the whole document (Digital signatures) |
| MACs | HMAC-SHA256 for message authentication |
| Password storage | Only via slow, salted password hashes (Argon2, bcrypt) — never a bare fast hash (Password storage) |
| Content addressing | Git object IDs (Git), IPFS, Docker image digests, deduplicated backups |
| Merkle trees | Verify any piece of a large dataset with a short proof |
| Commitments | Publish H(choice ∥ random nonce) now, reveal later — proves you didn't change your answer |
| Key derivation | HKDF derives multiple keys from a shared secret |
| Proof of work | Bitcoin miners search for inputs whose SHA-256 hash starts with many zeros |
Merkle trees
Hash data blocks, then hash pairs of hashes up to a single root. To prove block 3 belongs to the set, provide only the sibling hashes along its path — O(log n) hashes — and the verifier recomputes the root.
Used by Git, blockchains (transactions in a block), Certificate Transparency logs, BitTorrent, ZFS and distributed databases (anti-entropy repair in Cassandra and DynamoDB).
Pitfalls
- Using MD5/SHA-1 for anything security-related.
H(key ∥ message)as a MAC → length extension. Use HMAC.- Hashing passwords with SHA-256 → GPU brute force at billions of guesses per second.
- Ambiguous concatenation: H("ab" ∥ "c") = H("a" ∥ "bc"). Encode lengths or use structured, unambiguous encodings before hashing multiple fields.
- Comparing digests with
==in timing-sensitive contexts → use constant-time comparison.
- Hash functions produce fixed-size fingerprints that are one-way, collision-resistant and avalanche-y.
- The birthday bound halves collision resistance: use ≥ 256-bit hashes.
- MD5 and SHA-1 are broken; use SHA-256/512, SHA-3 or BLAKE2/3.
- Merkle–Damgård hashes allow length extension — never use H(key ∥ message) as a MAC; use HMAC.
- Uses: integrity, signatures, MACs, content addressing, Merkle trees, commitments, key derivation, proof of work.
- Passwords need slow, salted password hashing, not plain hashes.