Cryptography / SHA-256 hashing

SHA-256 hashing

How any message — three letters or a whole film — becomes a fixed 256-bit fingerprint. We follow “abc” all the way through.

Intermediate7 steps~7 min
Message block — the padded input
Registers — a…h, the working state
Digest — the 256-bit fingerprint
// THE SHORT VERSION

In 30 seconds

SHA-256 turns a message of any length into a 256-bit fingerprint.

The message is padded, cut into 512-bit blocks, and each block is stirred into eight running values through 64 rounds of shifts, rotations and additions.

The same input always gives the same output, flipping one input bit changes about half the output bits, and nobody knows a practical way to run it backwards or to find two messages with the same fingerprint.

SHA-256 hashing, step by step

STEPS01 / 07

Every value shown is the real one for “abc”. Long messages simply run the same compression once per 512-bit block.

STEP 01 / 07 Text becomes bits
THE MATH
"abc" → 61 62 63 → 01100001 01100010 01100011
  1. Step 1: Text becomes bits. Hashing works on bytes. Each ASCII character is one byte, so “abc” is 24 bits of input.
  2. Step 2: Pad to 512 bits. SHA-256 chews 512-bit blocks. The message gets a single 1 bit, enough zeros, and its own length in the last 64 bits.
  3. Step 3: Message schedule. The 16 block words are expanded to 64 using shifts, rotations and additions. Each round will consume one word.
  4. Step 4: Initial hash values. Eight 32-bit registers start from fixed constants taken from square roots of the first eight primes.
  5. Step 5: One compression round. Ch picks bits from f or g depending on e; Maj takes the majority of a, b, c; Σ functions rotate and XOR. All additions wrap at 2³².
  6. Step 6: 64 rounds, then add. After 64 rounds the registers are added back onto the starting values. The eight words, side by side, are the 256-bit digest.
  7. Step 7: The avalanche effect. A tiny change in input scrambles the whole output. That is why a digest works as a fingerprint: you can’t nudge a message toward a target hash.
VALUES SO FAR
msg616263input
len····pad
block····pad
W16····schedule
H0····const
a₁····round
digest····public
flips····avalanche
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

Spotted a mistake on this page?