Level 3 · How it worksThe village#178

SHA-256 explained: a public recipe that stirs your data 64 times

SHA-256 is a public recipe, published by NIST, that pads any data to a fixed block size and stirs it through 64 rounds to produce a 256-bit fingerprint. Anyone can follow it and get the same result. Bitcoin runs it twice on every block header and every transaction.

Every house in the village presses its own seals. Nobody sends pages to a central seal-maker. Instead, a recipe card hangs in every kitchen, the same card in every house, and anyone who follows it on the same page gets the same seal.

Grandma Rosa reads the card and frowns. If the recipe is pinned up for all to see, can't Ben read it too? He can. He has. He knows every step, every number, every stir. It doesn't help him one bit.

That recipe is SHA-256. Knowing how it works lets you check it. It doesn't let you run it backwards, and it doesn't let you forge a seal.

What it is

SHA-256 is one of the hash functions in NIST's Secure Hash Standard, FIPS 180-4. It takes a message of less than 2 to the power of 64 bits, works through it in blocks of 512 bits, uses 32-bit words, and produces a 256-bit digest (NIST FIPS 180-4). Written out, that's 64 characters.

The recipe is fully public. NIST even publishes a worked example with every intermediate value, so anyone writing their own version can check it step by step (NIST SHA-256 example).

How it works: sealing "abc"

NIST's own example message is three letters: "abc". Here is what happens to it, step by step, in plain words. All details are from FIPS 180-4.

  1. Turn it into bits. "abc" is three bytes, 24 bits.
  2. Pad it to a full block. The recipe says: "Append the bit "1" to the end of the message, followed by k zero bits", and finish with 64 bits holding the original length. Our 24 bits become exactly one block of 512. Even an empty message gets padded to a full block.
  3. Cut and stretch. The block is cut into 16 words of 32 bits, and the recipe stretches them into 64 words.
  4. Set the starting pot. Eight starting words go in. They are not chosen by anyone's taste: they are "the first thirty-two bits of the fractional parts of the square roots of the first eight prime numbers". The first is 6a09e667. We checked it: take the square root of 2, keep the part after the decimal point, and its first 32 bits read 6a09e667.
  5. Stir 64 times. The recipe runs 64 rounds. Each round mixes the eight working words with one of the 64 stretched words and one of 64 constants, which are "the first thirty-two bits of the fractional parts of the cube roots of the first sixty-four prime numbers". The first, from the cube root of 2, is 428a2f98.
  6. Read the seal. After the 64th round, the result is added to the starting words. For "abc", NIST prints: BA7816BF 8F01CFEA 414140DE 5DAE2223 B00361A3 96177A9C B410FF61 F20015AD (NIST SHA-256 example).

Why square and cube roots of primes? Because anyone can recalculate them. Nobody has to trust that the numbers were picked innocently. The recipe hides no secret ingredient, so every house can check every other house's cooking.

Why it matters

Bitcoin uses SHA-256 twice in a row, written "SHA256(SHA256())". Run "abc" through it twice and you get 4f8b42c22dd3729b..., our own calculation. That double recipe seals each 80-byte block header and builds the Merkle root from the transaction hashes (Bitcoin developer reference).

It's also the engine of mining. The white paper describes proof of work as "scanning for a value that when hashed, such as with SHA-256", gives a hash starting with enough zero bits (Nakamoto, 2008). In practice, a header hash must be "equal to or below" a target, and miners keep changing the nonce, "an arbitrary number miners change to modify the header hash" (Bitcoin developer reference). There is no clever shortcut. Miners press seal after seal until one comes out low enough.

Who gains: every node. Checking a block means running the recipe once. Who pays: the miners, who run it until they get lucky, and anyone hoping to forge a seal, who faces 256 bits against going backwards and 128 bits against finding a collision (NIST SP 800-107r1).

Where people get it wrong

  • "Bitcoin invented SHA-256." It's a NIST standard in FIPS 180-4, used far beyond crypto. Bitcoin picked it off the shelf.
  • "Miners solve complex maths problems." They run one public recipe over and over, changing a number each time, until the seal happens to be low enough. It's closer to rolling dice than to solving equations.
  • "A public recipe must be easy to reverse." The recipe being known is what lets every house check it. Knowing the steps lets you run them forwards only: NIST's standard calls finding a message for a given digest computationally infeasible.
  • "Two rounds of SHA-256 make it twice as safe." The double hashing is how Bitcoin defines its seals. Don't read it as twice the security. The 256-bit output is the same size either way.

Press the seal yourself

Run this one line if you have Python: python3 -c "import hashlib; print(hashlib.sha256(b'abc').hexdigest())". You should see ba7816bf at the start, the same seal NIST printed. Every house, every computer, every year: the same answer.

That's the strange comfort of SHA-256. Ben has had the full recipe card for years. Every miner on earth follows it around the clock. And the only thing anyone has ever got out of it is exactly the seal the recipe promised.

Frequently asked questions

What is SHA-256?

SHA-256 is a cryptographic hash function defined by the US National Institute of Standards and Technology in FIPS 180-4. It turns any message of less than 2 to the power of 64 bits into a 256-bit digest, usually written as 64 hexadecimal characters.

How many rounds does SHA-256 have?

SHA-256 processes each 512-bit block in 64 rounds. Each round mixes eight 32-bit working words with a word from the message and one of 64 fixed constants taken from the cube roots of the first 64 prime numbers.

What is the SHA-256 hash of "abc"?

The SHA-256 hash of the three-letter text "abc" is ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad. NIST publishes this as its standard worked example, so it is a common test for any SHA-256 tool.

Why does Bitcoin hash everything twice?

Bitcoin defines its block header hashes and Merkle tree hashes as SHA-256 applied twice, written SHA256(SHA256()). It's simply how the protocol defines its fingerprints. The final output is still 256 bits.

How does SHA-256 relate to Bitcoin mining?

Miners repeatedly hash a block header with double SHA-256, changing a number called the nonce each time. A block is valid only when its header hash is equal to or below a target set by the network, so finding one takes many attempts while checking it takes one.

Sources

  1. NIST, FIPS 180-4 Secure Hash Standard (August 2015)
  2. NIST, Cryptographic Standards and Guidelines, SHA-256 example with intermediate values
  3. NIST, SP 800-107 Revision 1 (August 2012)
  4. Bitcoin developer reference, Block chain
  5. Satoshi Nakamoto, Bitcoin: A peer-to-peer electronic cash system (2008)

Read next.