Skip to content
CryptographyInternals

How Does SHA-256 Work? A Step-by-Step Example

How SHA-256 works, step by step: we hash "Hello, world!" by hand, from padding and message schedule to one real round and the final 256-bit digest.

Published on 9 min read

SHA-256 works by padding your message to a multiple of 512 bits, cutting it into 512-bit blocks, expanding each block into 64 words of 32 bits, and running 64 rounds that stir those words into eight 32-bit registers with additions, bit rotations and two small logic functions. Each block's result is added to a running state, and the final eight words, written in hex, are the 256-bit hash. That is the whole algorithm. The rest of this post does it by hand on one real message, Hello, world!, with every number computed and checked against Node's crypto module.

If you prefer to watch rather than read, the interactive SHA-256 page replays the same 64 rounds for this exact message: you scroll and see the registers shift down one slot per round.

The message we will hash

Our input is the 13-byte ASCII string Hello, world!, with no newline at the end. Its expected result, which we will reach at the end, is:

315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3

Sixty-four hex characters, so 256 bits. Whatever the input size, a single byte or a 4 GB disk image, the output is always exactly that long. SHA-256 is part of the SHA-2 family, designed by the NSA and published by NIST in 2001 (today specified in FIPS 180-4). It has no key: anyone can compute it, and its security rests entirely on how hard it is to run backwards.

Step 1: padding the message into a 512-bit block

SHA-256 only eats 512-bit (64-byte) blocks. Padding works like this:

  1. Write the message bytes.
  2. Append a single 1 bit, which in practice is the byte 0x80.
  3. Append zero bytes until the length is 8 bytes short of a multiple of 64.
  4. Write the original message length in bits as a 64-bit big-endian integer.

Our message is 13 bytes, so 13 x 8 = 104 bits, which is 0x68. Everything fits in one block, because a message of up to 55 bytes leaves room for the 0x80 byte and the 8 length bytes. Here are the 64 bytes:

48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 80 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 68

You can read H (48), e (65), l (6c) and so on up to ! (21), then the 80 marker, a run of zeros, and the length 68 in the very last byte. Writing the length at the end matters: it makes sure that two messages that differ only by trailing zeros never produce the same padded input.

Step 2: sixteen words, then the message schedule

The block is read as sixteen 32-bit big-endian words, W0 to W15:

W0  = 48656c6c   ("Hell")
W1  = 6f2c2077   ("o, w")
W2  = 6f726c64   ("orld")
W3  = 21800000   ("!" + padding marker)
W4  ... W14 = 00000000
W15 = 00000068   (length in bits)

The compression needs 64 words, one per round, so the remaining 48 are generated from the first sixteen. This is the message schedule:

W[t] = σ1(W[t-2]) + W[t-7] + σ0(W[t-15]) + W[t-16]     (mod 2^32)

The two helpers use lowercase sigma. They mix one word with rotated and shifted copies of itself:

σ0(x) = ROTR7(x)  XOR ROTR18(x) XOR SHR3(x)
σ1(x) = ROTR17(x) XOR ROTR19(x) XOR SHR10(x)

ROTR rotates bits around the word, SHR shifts them out and fills with zeros. Let's compute W16 for real. It needs W14, W9, W1 and W0.

  • σ1(W14) = σ1(00000000) = 00000000
  • W9 = 00000000
  • σ0(W1): W1 = 6f2c2077, rotated right by 7 gives eede5840, by 18 gives 081ddbcb, shifted right by 3 gives 0de5840e. XOR of the three: eb260785.
  • W0 = 48656c6c

Adding them modulo 2^32:

00000000 + 00000000 + eb260785 + 48656c6c = 1 338b73f1  ->  W16 = 338b73f1

The leading 1 is the carry that falls off the 32-bit word. The same recipe gives W17 = cd8ed2ff (here σ1(W15) = 00390000 is the first time the length word enters the mix), and so on up to W63 = 1c66535f. Notice that our 13 bytes have now been smeared across all 64 words, including the ones that started as zeros.

Step 3: eight registers seeded with square roots

The compression keeps eight 32-bit working registers named a, b, c, d, e, f, g and h. Before the first block they hold the initial hash value H0, and these constants are not arbitrary. Each one is the first 32 bits of the fractional part of the square root of one of the first eight primes:

RegisterPrimeSquare rootH0 (hex)
a21.41421356...6a09e667
b31.73205080...bb67ae85
c52.23606797...3c6ef372
d72.64575131...a54ff53a
e113.31662479...510e527f
f133.60555127...9b05688c
g174.12310562...1f83d9ab
h194.35889894...5be0cd19

Check the first one: 0.41421356... x 2^32 = 1779033703.95..., and 1779033703 is 6a09e667. The 64 round constants K0 to K63 come from the same idea with cube roots of the first 64 primes (K0 = 428a2f98 comes from the cube root of 2). These are "nothing up my sleeve" numbers: anyone can recompute them, so nobody could have hidden a trapdoor in the choice.

Step 4: one round of compression, with real values

Each of the 64 rounds computes two temporary values from the registers, using four functions. This time the sigmas are uppercase, and they are different from the σ0 and σ1 of the message schedule:

Σ1(e)       = ROTR6(e)  XOR ROTR11(e) XOR ROTR25(e)
Ch(e, f, g) = (e AND f) XOR (NOT e AND g)          "e chooses between f and g"
Σ0(a)       = ROTR2(a)  XOR ROTR13(a) XOR ROTR22(a)
Maj(a, b, c)= (a AND b) XOR (a AND c) XOR (b AND c) "bitwise majority vote"

T1 = h + Σ1(e) + Ch(e, f, g) + K[t] + W[t]
T2 = Σ0(a) + Maj(a, b, c)

Here is round 0 on our message, starting from the H0 values above.

Left half, around e. e = 510e527f. Rotating it by 6, 11 and 25 gives fd443949, 4fea21ca and 87293fa8; their XOR is Σ1(e) = 3587272b. Ch(e, f, g) picks, bit by bit, f where e has a 1 and g where e has a 0: 1f85c98c. Then:

T1 = h        + Σ1(e)    + Ch       + K0       + W0
   = 5be0cd19 + 3587272b + 1f85c98c + 428a2f98 + 48656c6c
   = 3bdd59d4   (mod 2^32)

This is where the message enters the round: W0, the bytes Hell, is added into T1.

Right half, around a. a = 6a09e667. Rotating by 2, 13 and 22 gives da827999, 333b504f and 27999da8, so Σ0(a) = ce20b47e. The majority of a, b and c is 3a6fe667. Then:

T2 = Σ0(a) + Maj = ce20b47e + 3a6fe667 = 08909ae5   (mod 2^32)

The register shift. Now every register moves down one slot, and only two of them get genuinely new values:

new a = T1 + T2 = 3bdd59d4 + 08909ae5 = 446df4b9
new b = old a   = 6a09e667
new c = old b   = bb67ae85
new d = old c   = 3c6ef372
new e = d + T1  = a54ff53a + 3bdd59d4 = e12d4f0e
new f = old e   = 510e527f
new g = old f   = 9b05688c
new h = old g   = 1f83d9ab

Side by side:

before round 0:  6a09e667 bb67ae85 3c6ef372 a54ff53a 510e527f 9b05688c 1f83d9ab 5be0cd19
after  round 0:  446df4b9 6a09e667 bb67ae85 3c6ef372 e12d4f0e 510e527f 9b05688c 1f83d9ab

You can see the old values sliding to the right while a and e receive fresh mixtures. The old h, 5be0cd19, has left the registers, but it is not lost: it went into T1. This is exactly the motion the SHA-256 visualizer linked above animates, round by round, for all 64 rounds.

Step 5: 63 more rounds, then the final addition

Rounds 1 to 63 repeat the same computation with W1 to W63 and K1 to K63. After round 63, the registers hold:

c7557574 bb68ca3f ff1bcc94 a8fa0c2a 101ccd4f dcc300a8 3c78bb1c fcb420ba

This is not yet the hash. SHA-256 adds each register to the value it started from (here H0), modulo 2^32. This feed-forward step is what makes the compression function hard to invert even if you could undo the rounds:

a: c7557574 + 6a09e667 = 315f5bdb
b: bb68ca3f + bb67ae85 = 76d078c4
c: ff1bcc94 + 3c6ef372 = 3b8ac006
d: a8fa0c2a + a54ff53a = 4e4a0164
e: 101ccd4f + 510e527f = 612b1fce
f: dcc300a8 + 9b05688c = 77c86934
g: 3c78bb1c + 1f83d9ab = 5bfc94c7
h: fcb420ba + 5be0cd19 = 5894edd3

Concatenated: 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3. That is the SHA-256 of Hello, world!, identical to what node:crypto and printf '%s' 'Hello, world!' | sha256sum print. For a longer message, these eight words would become the starting state for the next block instead of H0, and the process would repeat.

Avalanche: change one character, count the flipped bits

A good hash should make a tiny input change look like a completely new random output. Change the final ! into ?:

Hello, world!  315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3
Hello, world?  407e1b6fc892e3340482da07d6c07d8180bdbb1fcf4329ba96559db159316ce7

Counting the differing bits gives 109 out of 256. Changing H to h flips 124 bits, and ! to . flips 121. To get beyond anecdotes, we replaced each of the 13 characters with every other printable ASCII character: 1,222 variants in total. The average distance was 128.2 bits, exactly half of 256, with a minimum of 104 and a maximum of 151. That is what you expect from 256 independent coin flips.

It is also instructive to watch where the difference appears. The ! lives in W3, so rounds 0 to 2 are identical for both messages. After round 3 the registers differ by 6 bits, after round 4 by 39, after round 5 by 66 and after round 7 by 118. From then on, the difference hovers around 128 until the end. The SHA-256 page lets you type two messages and see that curve live.

Why nobody can reverse SHA-256

Two different attacks are worth separating, because their costs are very different.

  • Preimage: given a digest, find any input that produces it. With no shortcut known, you must try inputs until one matches, about 2^256 attempts. For comparison, 2^128 is already far beyond any conceivable computing budget, as the brute-force and key size page shows with live numbers.
  • Collision: find any two inputs with the same digest. Here the birthday paradox helps the attacker: after hashing about 2^128 random inputs, a matching pair becomes likely. So SHA-256 offers 128 bits of collision resistance, which is still out of reach.

Its predecessors MD5 and SHA-1 both have practical collisions. SHA-256 has none after 25 years of public study.

The caveat is the input space, not the function. If the input is guessable, such as a 6-digit PIN or a common password, an attacker simply hashes every candidate and compares. SHA-256 cannot hide what is easy to guess.

Where SHA-256 is used

  • TLS certificates. When your browser checks a site's certificate, the certificate authority's signature is typically computed over a SHA-256 hash of the certificate contents.
  • Git. Git names objects by their hash. It historically uses SHA-1 and has offered a SHA-256 object format since version 2.29, as part of a slow migration away from SHA-1.
  • Bitcoin. Block headers are hashed with double SHA-256, and mining is the search for a header whose hash falls below a target.
  • Downloads and backups. Publishing the SHA-256 of a file lets anyone verify that the copy they received is bit-for-bit identical.

What not to use SHA-256 for

Password storage. SHA-256 is fast on purpose, and that is the problem: GPUs can test billions of candidate passwords per second against a leaked hash. Store passwords with a slow, salted function built for it, such as Argon2 or bcrypt.

Secret-prefix authentication. It is tempting to authenticate a message by sending SHA-256(secret || message). Because the digest is the full internal state after the last block, an attacker can resume hashing from it and compute SHA-256(secret || message || padding || extra) without knowing the secret. This is the length extension attack, and it is why message authentication uses HMAC-SHA-256, which wraps the hash in two keyed passes.

Encryption. A hash is one way and has no key, so it cannot hide data you need to read back. For that you need a cipher such as AES.

Try it yourself

Doing one round by hand is the best way to see that SHA-256 is not magic: a few rotations, XORs and additions, repeated 64 times. To see all 64 rounds at once, open the interactive SHA-256 page, scroll through the registers for Hello, world!, then change one character and watch about half the bits flip. The other topics in modern cryptography work the same way.

Related articles

A Diffie-Hellman key exchange example with real numbers: p = 23, g = 5, secrets 6 and 15, shared key 2. See how it works and why Eve can't follow at 2048 bits.
How AES encryption works, step by step: we follow one real 16-byte block through key expansion and all 10 rounds of AES-128, with every hex value shown.
A full RSA example with small numbers: p = 61, q = 53, e = 17, d = 2753. Encrypt 65 to 2790 and back, with every step of the math shown and checked.