atmathExploreไทย

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)

LetterCodeOld hh × 31 + codemod 100
a9709797
t116973,12323
m1092382222
a972277979
t116792,56565
h104652,11919

⇒ the toy hash of “atmath” = 19 · it has 2 weaknesses

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)

  1. 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
  2. Rotate bits: shift right by 3 places; bits falling off the end wrap around to the front x10110001ROTR 300110110
  3. XOR (adding bits without carrying): same bits ⇒ 0 · different ⇒ 1 x11001010y10100110x XOR y01101100
  4. 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

Letteratmath
Code9711610997116104
Hex61746d617468
Bits011000010111010001101101011000010111010001101000

⇒ 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

61746d61746880000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030

■ 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

W061746d61W174688000W200000000W300000000W400000000W500000000W600000000W700000000W800000000W900000000W1000000000W1100000000W1200000000W1300000000W1400000000W1500000030

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

a · √26a09e667b · √3bb67ae85c · √53c6ef372d · √7a54ff53ae · √11510e527ff · √139b05688cg · √171f83d9abh · √195be0cd19

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:

  1. Compute the mix T1 = h + (e rotated 3 ways, then XORed) + choose(e, f, g) + K + W · with wrap-around addition
  2. Compute the mix T2 = (a rotated 3 ways, then XORed) + majority(a, b, c)
  3. Shift down: every value moves down 1 place (a ⇒ b ⇒ … ⇒ h) · new a = T1 + T2 · new e = d + T1
abcdefgh
Start6a09e667bb67ae853c6ef372a54ff53a510e527f9b05688c1f83d9ab5be0cd19
After round 15d7cf5ae6a09e667bb67ae853c6ef372fa3c5003510e527f9b05688c1f83d9ab
After round 2ad5a24a25d7cf5ae6a09e667bb67ae85a45fc174fa3c5003510e527f9b05688c
After round 643bd7b95e2f6c08ad06cefbf6bd34c9d9cad11236f138454216400b45bdcf42d7

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

H0a5e19fc5H1ead3b732H2433def68H36284bf13H41bdf64b5H58c3dadceH635c3e4f0H719b00ff0

⇒ 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

0641281922561816324864round · vertical axis = differing bits (of 256)

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

Watch the video

Long video: Encrypt · Check · Repair: The Math That Protects Your Data Every Day (Thai / English audio)
Watch on YouTube

The video loads from YouTube (youtube-nocookie.com) only after you press play. · YouTube channel: atmath (@atmathapp)

The math behind it

Good to know

Cryptography series, 3 parts: Part 1 hashes and passwords · Part 2 the RSA padlock · Part 3 hash step by step (this page)

References

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”)

← More from Explore