SHA-256 Hash Function
An interactive walkthrough of SHA-256 — from the Merkle-Damgård construction to the message schedule and compression function — with live hashing, avalanche visualization, and comparison to MD5.
0 The SHA Family
SHA stands for Secure Hash Algorithm. NIST has published several generations. SHA-256 belongs to the SHA-2 family — the current standard for most applications.
1 SHA-256 at a Glance
SHA-256 vs MD5 — why it's stronger
| Property | MD5 | SHA-256 |
|---|---|---|
| Output length | 128 bits | 256 bits |
| Birthday attack cost | 2⁶⁴ ops | 2¹²⁸ ops |
| Collision resistance | ⚠ Broken (seconds) | ✓ No known collisions |
| Rounds | 64 (4×16) | 64 (single structure) |
| Security level | ⚠ None for crypto | ~128-bit security |
| TLS/certificates | Forbidden | Standard |
| Password hashing | Never use | Weak alone (needs bcrypt/scrypt) |
How SHA-256 Security Is Measured
A 256-bit hash provides roughly 128 bits of security against collision attacks (birthday paradox halves the effective security). For comparison:
SHA-1 (160-bit): 2⁸⁰ ops — deprecated, practical attacks demonstrated
SHA-256 (256-bit): 2¹²⁸ ≈ 10³⁸ ops for collision — infeasible for any attacker
2 Merkle-Damgård Construction
SHA-256 is built on the Merkle-Damgård construction: the message is split into fixed-size blocks, and a compression function is applied iteratively to produce a running hash state. The final state is the hash output.
Processing Pipeline
Initial Hash Values (H₀–H₇)
First 32 bits of the fractional parts of the square roots of the first 8 primes. They are "nothing up my sleeve" numbers — no special weakness can be hidden in them.
H₁ = 0xbb67ae85 (√3)
H₂ = 0x3c6ef372 (√5)
H₃ = 0xa54ff53a (√7)
H₅ = 0x9b05688c (√13)
H₆ = 0x1f83d9ab (√17)
H₇ = 0x5be0cd19 (√19)
3 Message Schedule: 16 → 64 Words
Each 512-bit block contains 16 input words (W[0..15]). SHA-256 extends these to 64 words using a recurrence relation. This expansion ensures every input bit eventually influences all 64 rounds.
The σ (sigma) mixing functions
ROTR = rotate right, SHR = shift right. These operations "smear" bits across word boundaries, ensuring that changing a single input word perturbs many schedule words.
Schedule Visualizer
Enter a message to see the first block's message schedule. The first 16 words are the padded message; the next 48 are derived.
4 The Compression Function
Each of the 64 rounds updates 8 working variables (a, b, c, d, e, f, g, h). Two temporary values T1 and T2 are computed from complex bitwise functions, and the state is shifted down like a chain.
One Round Step
Σ₀ and Σ₁ (capital sigma) — for state
Σ₁(e) = ROTR⁶(e) ⊕ ROTR¹¹(e) ⊕ ROTR²⁵(e)
These "big sigma" functions mix the bits of a single word using three different rotation amounts for maximum diffusion.
Ch and Maj (bit-selection functions)
Maj(a,b,c) = (a ∧ b) ⊕ (a ∧ c) ⊕ (b ∧ c)
Ch (choice): if e=1, choose f; else choose g. Maj (majority): output 1 if majority of a,b,c are 1.
64 Round Constants (K[0..63])
The first 32 bits of the fractional parts of the cube roots of the first 64 prime numbers. Like the initial hash values, these are "nothing up my sleeve" numbers.
Show all 64 constants
Davies-Meyer Feed-Forward
After all 64 rounds, the working variables are added back to the state from before the block was processed:
5 Live SHA-256 Demo
Type anything and watch the hash update in real time. SHA-256 outputs exactly 64 hex characters = 256 bits, always.
Real-time Hash
Preset Examples
SHA-256("abc") = ba7816bf8f01cfea...2e3cbbf952
SHA-256("The quick brown fox...") = d7a8fbb307d78...4e62e0c
6 Avalanche Effect
SHA-256's avalanche effect is thorough: even a 1-bit difference in input should flip roughly 50% of the 256 output bits. More rounds and a more complex compression function give SHA-256 better diffusion than MD5.
Bit-level Comparison
One-character Increment Table
Change one character at a time and observe that every position produces ~50% bit change:
7 Real-world Applications
bcrypt, scrypt, or Argon2. You can use PBKDF2 with many iterations as a compromise.
SHA-256 + RSA = Digital Signature
This is how documents, code, and TLS certificates are signed:
Verify: hash' = SHA-256(document) → RSA_verify(signature, hash', public_key)
Hashing the document first (rather than encrypting the whole thing) makes RSA signatures fast regardless of document size. Review RSA →