Skip to content
CryptographyInternals

04 / 11 · Modern cryptography

SHA-256

A fingerprint for any data: change one character and half the bits flip.

Standard
FIPS 180-4 (SHA-2, 2001)
Output
256 bits
Used in
TLS certificates, Git, Bitcoin

A hash function turns input of any size into a short, fixed-size fingerprint. SHA-256 was designed by the NSA and published by NIST in 2001 as part of the SHA-2 family.

There is no key. Its security comes from being easy to compute in one direction and, as far as anyone knows, impossible to run backwards or to make collide.

Fingerprint
“a” · 1 bytes
↓ SHA-256
ca978112ca1bbdca…
“Hello, world!” · 13 bytes
↓ SHA-256
315f5bdb76d078c4…
“…Hello, world!” · 260 bytes
↓ SHA-256
0d34221a80e41817…
Zoom

any size in

01 / 23 · Inside SHA-256

A hash is a fingerprint

A hash function turns any input, one letter or a whole film, into a short fixed-size fingerprint called a digest. SHA-256’s digest is always 256 bits, written as 64 hex characters.

A fingerprint lets you check a file is unchanged, or store a password’s fingerprint instead of the password, without keeping the original.

Playground

Hash your own message, round by round

Type a message and step through its compression: padding, the eight starting registers, all 64 rounds and the final addition. The right panel works out every round on your real words. A second input shows the avalanche.

Your input

Any length. Messages over 55 bytes take several 64-byte blocks; pick which one to step through.

13 B · Block 1 of 1

109 of 256 bits differ (42.6%)

Pad
48656c6c6f2c20776f726c6421800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000068

Pad the message to whole blocks

What happens

Your text becomes bytes (UTF-8, shown in hexadecimal, base 16). SHA-256 eats 64-byte blocks, so after the message come one 0x80 byte (a single 1 bit, then zeros), zero bytes, and finally the message length in bits on 8 bytes.

On your values

13 + 1 + 42 + 8 = 64 B = 1 × 64

13 × 8 = 104 bits = 0x0000000000000068

Why it matters

Padding must never turn two different messages into the same block: with zeros only, “abc” and “abc” plus a zero byte would collide. The 0x80 marker and the length field make the padding unambiguous.

The basics: Hexadecimal →

Start

Pad

SHA-256(A)

315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3

SHA-256(B)

407e1b6fc892e3340482da07d6c07d8180bdbb1fcf4329ba96559db159316ce7
The 256 output bits

109 of 256 bits differ (42.6%)

Same bitDifferent bit
Bits of the working state a…h that differ between A and B, round by round (this block)
0128256128 = 50%
Bits of the working state a…h that differ between A and B, round by round (this block)
Round 1: 0
Round 2: 0
Round 3: 0
Round 4: 6
Round 5: 39
Round 6: 66
Round 7: 99
Round 8: 118
Round 9: 116
Round 10: 116
Round 11: 113
Round 12: 123
Round 13: 128
Round 14: 135
Round 15: 143
Round 16: 139
Round 17: 126
Round 18: 115
Round 19: 103
Round 20: 110
Round 21: 124
Round 22: 135
Round 23: 142
Round 24: 133
Round 25: 124
Round 26: 127
Round 27: 121
Round 28: 124
Round 29: 134
Round 30: 131
Round 31: 130
Round 32: 129
Round 33: 124
Round 34: 117
Round 35: 116
Round 36: 122
Round 37: 114
Round 38: 122
Round 39: 125
Round 40: 119
Round 41: 128
Round 42: 124
Round 43: 127
Round 44: 135
Round 45: 141
Round 46: 142
Round 47: 146
Round 48: 141
Round 49: 126
Round 50: 126
Round 51: 118
Round 52: 110
Round 53: 117
Round 54: 121
Round 55: 129
Round 56: 143
Round 57: 144
Round 58: 142
Round 59: 138
Round 60: 132
Round 61: 129
Round 62: 122
Round 63: 125
Round 64: 123

Registers after this step: A over B, differences highlighted · Start

Aa6a09e667bbb67ae85c3c6ef372da54ff53ae510e527ff9b05688cg1f83d9abh5be0cd19
Ba6a09e667bbb67ae85c3c6ef372da54ff53ae510e527ff9b05688cg1f83d9abh5be0cd19

How it works

  1. 1The message is padded to a multiple of 512 bits, with its length written at the end.
  2. 2Eight 32-bit working variables a…h start from fixed constants (the fractional parts of square roots of the first primes).
  3. 3Each 512-bit block is expanded into 64 words, and 64 rounds mix them into a…h with additions, rotations and bitwise choice and majority functions.
  4. 4The result is added to the running state; after the last block, the eight words are the 256-bit hash.

Why it holds

Avalanche
Every round feeds its output into the next one through non-linear functions and modular additions. A changed message word only enters at its own round, but about five rounds later half of the 256 state bits differ, and they stay that way to the end. The chart shows it.
Preimage resistance
Given a hash, finding any input that produces it costs about 2²⁵⁶ tries. Nothing known does better.
Collision resistance
Finding any two inputs with the same hash costs about 2¹²⁸ (the birthday bound). Its predecessors MD5 and SHA-1 have practical collisions; SHA-256 has none after 25 years of study.

What can go wrong

Passwords
SHA-256 is fast, which is bad for password storage: attackers can test billions of guesses per second. Use a slow, salted function such as Argon2 or bcrypt.
Length extension
From H(secret ‖ message) an attacker can compute H(secret ‖ message ‖ extra) without knowing the secret. HMAC exists to prevent exactly this.
Read the in-depth articleHow Does SHA-256 Work? A Step-by-Step ExampleHow 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.