Curious math · Cryptography, part 3
How does a hash work? SHA-256 step by step, from letters to a 64-character hash
Short answer: SHA-256 turns the message into bits (0s and 1s) · pads them to a 512-bit block · starts from 8 numbers taken from the square roots of primes · then mixes for 64 rounds using wrap-around addition, bit rotation and bit selection · the result is 8 numbers of 32 bits each, joined into 256 bits = 64 hexadecimal characters · every step is ordinary arithmetic, but it is repeated so many times that a tiny change to the message changes almost half the hash
In part 1 (Why can’t a website tell you your old password?) we saw that SHA-256(“atmath”) = a5e19fc5ead3b732433def686284bf131bdf64b58c3dadce35c3e4f019b00ff0 · now let’s open the machine and see how 6 letters become this hash
Warm-up: a toy hash (made up by us · not a real one)
Rule: start with h = 0 · read one letter at a time and do h = (h × 31 + letter code) mod 100 · “mod 100” = divide by 100 and keep the remainder (like a clock with 100 positions)
| Letter | Code | Old h | h × 31 + code | mod 100 |
|---|---|---|---|---|
| a | 97 | 0 | 97 | 97 |
| t | 116 | 97 | 3,123 | 23 |
| m | 109 | 23 | 822 | 22 |
| a | 97 | 22 | 779 | 79 |
| t | 116 | 79 | 2,565 | 65 |
| h | 104 | 65 | 2,119 | 19 |
⇒ the toy hash of “atmath” = 19 · it has 2 weaknesses
- Too small: only 100 possible results · there are 676 2-letter words (a–z), so collisions are certain, e.g. “ap” also gives 19 (6 words give 19)
- Too little mixing: change the last letter h (104) to i (105), a code 1 higher ⇒ “atmati” gives 20, so the hash also goes up by just 1 · the hash lets you guess nearby messages
SHA-256 fixes these 2 problems: the result is 256 bits long (2256 possible values), and it mixes for 64 rounds with the 4 ingredients below
4 ingredients (examples use 8-bit numbers to keep them readable · the real thing uses 32 bits)
- Wrap-around addition: add, then keep the remainder · 8 bits hold 0–255 ⇒ 200 + 100 = 300 ⇒ mod 256 = 44 · the real thing uses mod 232 = 4,294,967,296
- Rotate bits: shift right by 3 places; bits falling off the end wrap around to the front x10110001ROTR 300110110
- XOR (adding bits without carrying): same bits ⇒ 0 · different ⇒ 1 x11001010y10100110x XOR y01101100
- Choose and majority (bit by bit): “choose” = if x’s bit is 1 take y’s bit, otherwise z’s · “majority” = whichever bit at least 2 of the 3 have x11001010y10100110z01101100choose10100110majority11101110
SHA-256 step by step with “atmath”
Step 1 · letters ⇒ bytes ⇒ bits
Each letter has a code (ASCII) of 1 byte = 8 bits · written as 2 hexadecimal characters
| Letter | a | t | m | a | t | h |
|---|---|---|---|---|---|---|
| Code | 97 | 116 | 109 | 97 | 116 | 104 |
| Hex | 61 | 74 | 6d | 61 | 74 | 68 |
| Bits | 01100001 | 01110100 | 01101101 | 01100001 | 01110100 | 01101000 |
⇒ the message is 48 bits long
Step 2 · pad the bits to 512
Append a single 1 bit · append 0s until the length leaves remainder 448 when divided by 512 (here, 399 of them) · then append the original length (48) as a 64-bit number · 48 + 1 + 399 + 64 = 512
■ message · ■ the appended 1 bit (80 = 1000 0000) · ■ zeros · ■ the length 48 in hexadecimal · a message of up to 55 bytes fits in one block · longer messages take several blocks, and steps 3–6 run block by block
Step 3 · cut into 16 words of 32 bits
Then expand to 64 words: from word 16 on, each word is computed from 4 earlier words (rotation + XOR + wrap-around addition) · so the first word of the message shows up again in many later words
Step 4 · 8 starting numbers (a to h)
The standard picks numbers anyone can recompute, so nothing is hidden: the square roots of the first 8 primes (2, 3, 5, 7, 11, 13, 17, 19), keeping only the part after the decimal point
√2 = 1.4142135623… ⇒ after the point 0.4142135623… × 232 = 1,779,033,703.95… ⇒ round down 1,779,033,703 = hex 6a09e667 = a
The other set of constants, 64 K values (one per round), comes the same way from the cube roots of the first 64 primes (2 to 311), e.g. ∛2 = 1.2599210498… ⇒ 428a2f98
Step 5 · mix for 64 rounds
Each round uses 1 word W for that round + 1 constant K, and does 3 things:
- Compute the mix T1 = h + (e rotated 3 ways, then XORed) + choose(e, f, g) + K + W · with wrap-around addition
- Compute the mix T2 = (a rotated 3 ways, then XORed) + majority(a, b, c)
- Shift down: every value moves down 1 place (a ⇒ b ⇒ … ⇒ h) · new a = T1 + T2 · new e = d + T1
| a | b | c | d | e | f | g | h | |
|---|---|---|---|---|---|---|---|---|
| Start | 6a09e667 | bb67ae85 | 3c6ef372 | a54ff53a | 510e527f | 9b05688c | 1f83d9ab | 5be0cd19 |
| After round 1 | 5d7cf5ae | 6a09e667 | bb67ae85 | 3c6ef372 | fa3c5003 | 510e527f | 9b05688c | 1f83d9ab |
| After round 2 | ad5a24a2 | 5d7cf5ae | 6a09e667 | bb67ae85 | a45fc174 | fa3c5003 | 510e527f | 9b05688c |
| After round 64 | 3bd7b95e | 2f6c08ad | 06cefbf6 | bd34c9d9 | cad11236 | f1384542 | 16400b45 | bdcf42d7 |
Orange = the 2 values computed fresh that round (a and e) · the rest just shift down · step through every round in the Try it box
Step 6 · add the starting values, then join
Add a–h after round 64 to the starting values from step 4, one by one (wrap-around), giving 8 words, then line them up
⇒ SHA-256(“atmath”) = a5e19fc5ead3b732433def686284bf131bdf64b58c3dadce35c3e4f019b00ff0 ✓ the same as in part 1
The program behind this page follows the standard step by step, and was checked against NIST’s example value (“abc” ⇒ ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad) and against a standard implementation on 3,000 more messages: all matched
Watch the avalanche, round by round
“atmath” and “atmatH” differ only in the last letter · count how many of the 256 bits in a–h differ between the 2 messages after each round
- Round 1: 0 bits differ (it uses word W0, which is identical · the last letter sits in word W1)
- Rounds 2–5: 4 ⇒ 26 ⇒ 56 ⇒ 85 bits · by round 6 more than 100 bits differ
- After that it hovers around half (128 bits), as if random · the final hashes differ in 129 bits (the same number as in part 1)
Try it
⚠️ Don’t type a real password · everything runs on your device; nothing you type is sent anywhere
1 · Toy hash
2 · SHA-256 step by step
3 · Avalanche: how many bits differ between 2 messages?
Why it can’t be reversed
- Information is lost: wrap-around addition throws away the overflow · a message of any length is squeezed down to 256 bits ⇒ many messages share the same hash (the pigeonhole principle, as with the toy hash, but collisions are extremely hard to find because there are 2256 slots)
- No known shortcut: every bit of the hash depends on every bit of the message through 64 rounds · FIPS 180-4 calls SHA-256 “secure” because finding a message for a given hash, or 2 messages with the same hash, is computationally infeasible · ⚠️ this means “nobody knows a shortcut”, not something proved mathematically
Watch the video
Watch on YouTube
The video loads from YouTube (youtube-nocookie.com) only after you press play. · YouTube channel: atmath (@atmathapp)
The math behind it
- Directly: remainders (mod 232) · binary and hexadecimal · prime numbers · square and cube roots · functions
- Behind the scenes: logic (choose · majority · XOR bit by bit) · the pigeonhole principle · the multiplication principle (2256 possibilities)
Good to know
- This page only shows single-block messages (up to 55 bytes) · longer messages repeat steps 3–6 block by block, using the previous block’s result as the starting values
- 1 Thai character takes 3 bytes (UTF-8), so Thai messages must be shorter
- Real password storage doesn’t use plain SHA-256 (see why in part 1)
Cryptography series, 3 parts: Part 1 hashes and passwords · Part 2 the RSA padlock · Part 3 hash step by step (this page)
References
- NIST FIPS 180-4 · Secure Hash Standard (SHS) · §4.1.2 §4.2.2 §5.1.1 §5.2.1 §5.3.3 §6.2.2
- NIST · Examples with Intermediate Values (SHA-256 “abc”)
Accessed 19 Nov 2026 · the numbers and hashes on this page were computed with our own script written to the standard (checked against NIST’s “abc”)