3 B616263How does a message of any length become a 32-byte digest? In SHA-256, its bytes pass through a small set of operations, repeated enough times that each part of the input can influence the whole result.
We will follow that calculation in four stages: prepare the bytes → expand the message → mix the state → assemble the digest. SHA-256 is our concrete example; Keccak-256
, used by Ethereum, follows a different construction.
Text is encoded, files are read, and structured objects are serialized. By the time data reaches SHA-256, it is a sequence of bytes. The hash calculation does not need to know which format it came from.
These paths produce the same kind of representation, not identical content. An image file's bytes include its format and metadata; hashing a decoded image is a different choice. Structured data needs an agreed serialization so the same object produces the same bytes.
Drag the length slider to follow a shorter or longer input through every step. The sample repeats the letters a–z, one UTF-8 byte per letter. The length is measured before padding; zero means an empty input. All length controls below share this same input. The byte preview pairs each letter with its hexadecimal value. Every interactive figure labels the current input; reset returns to abc.
3 B616263SHA-256 reads 64 bytes at a time. Before processing, it adds an ending that makes the message fill a whole number of blocks.
For a byte-aligned message, append 80, enough zero bytes, and finally eight bytes recording the original bit length. The 80 byte is a 1 bit followed by seven zeros; it marks where the message ends. The length field records how long the original message was.
Each position below is one byte. Dark circles show the actual input bytes in hex. The blue-violet marker is 80, hollow dots are zero padding, and the final eight outlined cells encode the original bit length, most-significant byte first. The shaded outline groups all padding, including the length field. Each block holds 64 bytes.
3 BMove the input slider between 55 and 56 bytes. At 55, the ending fits in one block: . At 56, it spills into a second block. At 64 input bytes, the message fills a block exactly, so its ending needs a new block of its own.
For an original bit length , append one 1 bit and the smallest nonnegative number of zero bits such that:
Append the 64-bit encoding of to reach a multiple of 512 bits.
Split each 64-byte block into sixteen words, each four bytes long. A word is just a 32-bit number. Read the most significant byte first, so 61 62 63 80 becomes the word 61626380.
SHA-256 uses a few operations on these words:
ffffffff + 00000001 = 00000000.1 when they differ.This illustration uses a fixed eight-bit value, independent of the message, so the movement is easy to see. SHA-256 does the same operations on 32 bits.
Rotations move an input bit to other positions. Addition brings in carries: even 0111 + 0001 = 1000 changes several bits. Combining these operations lets a local difference spread as the computation proceeds.
We will write XOR as and wrapping addition as :
Each block goes through 64 rounds, and each round needs one message word. We have sixteen words so far. To get the remaining 48, SHA-256 repeatedly combines four earlier words.
Call the word for round . The first sixteen come straight from the selected block, four bytes per word. They include padding bytes when those fall inside the block.
From word 16 onward:
The two functions mix a word by XORing rotated and shifted copies of it. Two of the four earlier words pass through these functions; the other two enter the sum unchanged.
0000000000000000000000006162638061626380Sum mod 2³²Move the word slider to see which four earlier words contribute. Change the input length or the selected block to follow a different calculation. This is the message schedule: it keeps reintroducing the input in different combinations across the rounds. It creates dependencies, not extra information.
rotates right by bits; shifts right and fills with zeros.
These fixed amounts are part of SHA-256's design.
Alongside the message schedule, SHA-256 keeps eight working words named . Together they hold 256 bits. Think of them as eight slots whose contents change every round.
The first block starts with eight fixed values. Each round reads one schedule word and a fixed round constant , then computes two temporary values:
There are three small ideas inside these equations. mixes bit positions using rotations and XOR. Choose, , uses each bit of to select a bit from or . Majority, , takes whichever bit appears at least twice among .
The update is simpler than the equations suggest: six words move to the next slot; two receive newly mixed values.
T₁ = 54da50e8 · T₂ = 08909ae5a′ = 54da50e8 + 08909ae5 = 5d6aebcde′ = a54ff53a + 54da50e8 = fa2a4622The arrows show where each new value comes from. All eight updates use the old state. The message enters through , affects both and , then moves through the other slots in later rounds.
Expand “Round arithmetic” to see the two additions for the selected round. The next round repeats the same rule with the updated state and the next schedule word. After 64 rounds, all eight words have been repeatedly mixed.
Here means bitwise AND and flips all 32 bits. Choose and majority operate independently at each bit position:
The uppercase functions used in rounds differ from the lowercase functions used in the schedule:
The initial eight words are:
6a09e667 bb67ae85 3c6ef372 a54ff53a
510e527f 9b05688c 1f83d9ab 5be0cd19They come from the first 32 fractional bits of the square roots of the first eight primes. The 64 round constants come from cube roots of the first 64 primes; is 428a2f98. These values are fixed and public. Their origin makes them reproducible, not a proof of security. Curve Origins
examines that distinction for elliptic-curve parameters.
One step remains after round 63. Add each final working word to the corresponding word from before this block began, again keeping only 32 bits. This is called feed-forward.
For each of the eight positions:
6a09e667506e3058ba7816bfSelect a word to see its addition and its place in the block output. On the last block, joining the eight results gives the final 32-byte digest. Earlier blocks produce an intermediate state instead.
For a longer message, those eight results become the starting state for the next block. Build that block's schedule, run 64 rounds, and add back again. Only after the last block do we serialize the state as the digest.
A changed input bit enters both the schedule and the working state. Rotations move its influence across bit positions; Boolean selection and carries make the effect depend on the surrounding values. Repetition spreads that influence across the output. The avalanche effect emerges from this process.
This explains how the mixing happens, but it does not prove that reversing the hash is hard. A rotation, for example, is easy to undo. The difficult problem is finding an unknown message that satisfies the entire linked calculation when only the final digest is given. SHA-256's security depends on how well this specific construction withstands cryptanalysis.
For the full specification, see FIPS 180-4, sections 4–6.