How Does SHA-256 Work?
SHA-256 takes a message, processes it in several precisely defined steps, and produces a hash value of 256 bits from it — as the best-known member of the SHA-2 family.
At first glance, the algorithm looks complicated: padding, 512-bit blocks, rotations, XOR operations, a message schedule, and finally 64 compression rounds per block.
When you look at these steps one by one, though, the structure becomes much easier to understand.
In this article, I therefore open the black box of SHA-256 and show what happens during the calculation.
If you first want to know what SHA-256 is in general, what security strength the algorithm has, and where it is used, you'll find the introduction in the article What Is SHA-256?. I explain what makes a hash function what it is, in general terms, in What Is a Hash Function?.
The SHA-256 Process
In simplified form, the calculation can be represented like this:
Input
↓
Message as bits
↓
Padding
↓
512-bit blocks
↓
16 × 32-bit words
↓
Message schedule W₀ … W₆₃
↓
Initial hash state H₀ … H₇
↓
64 compression rounds
↓
New hash state
↓
Next 512-bit block
↓
...
↓
256-bit digest
SHA-256 works mainly with 32-bit words.
Many operations that look complicated at first are ultimately combinations of a few basic bit operations:
- AND
- XOR
- NOT
- Right shift
- Rotate right
- Addition modulo
2³²
We encounter these operations throughout the entire algorithm.
Step 1: The Message Becomes Bits
SHA-256 ultimately processes a sequence of bits.
As a simple example, take the ASCII characters:
abc
Their byte values are:
a = 0x61
b = 0x62
c = 0x63
In binary:
a b c
01100001 01100010 01100011
Our message therefore initially has:
3 × 8 bits = 24 bits
However, these 24 bits can't yet be processed directly by the SHA-256 compression function.
First, the message has to be prepared.
Step 2: The Padding
SHA-256 processes messages in blocks of 512 bits each.
The original message is therefore extended by a precisely defined padding.
First, a single 1 bit is appended:
Message
01100001 01100010 01100011
↓
01100001 01100010 01100011 1
After that come as many 0 bits as are needed so that exactly 64 bits remain free within the last 512-bit block before the final length field.
Finally, the length of the original message is appended as a 64-bit integer.
For abc, this length is:
24 bits
that is:
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00011000
The padded message then has a length that is a multiple of 512 bits.
For short messages such as abc, exactly one 512-bit block results.
For longer messages, multiple blocks result.
Why Is the Message Length Stored?
The padding doesn't simply make sure the last block gets filled.
The original message length is part of the padded message.
As a result, a message and the same message with additional zero bits appended, for example, don't simply lead to an identical padded representation.
For SHA-256, the following applies:
message length < 2⁶⁴ bits
The original length can therefore be represented in the 64-bit field provided for it.
Step 3: Division Into 512-Bit Blocks
After padding, the message is divided into blocks of 512 bits each:
M¹
M²
M³
...
Mᴺ
Each block consists of:
512 bits
÷
32 bits
=
16 words
These words are used as:
M₀ M₁ M₂ ... M₁₅
or, when forming the message schedule, as:
W₀ W₁ W₂ ... W₁₅
In our example abc, the block begins in hexadecimal representation with:
61626380
This contains:
61 = a
62 = b
63 = c
80 = start of the padding
The last 32-bit word finally contains part of the encoded original message length:
00000018
0x18 corresponds to 24 in decimal — exactly the length of abc in bits.
Step 4: The Message Schedule
For its 64 rounds, the compression function needs a total of 64 words:
W₀ ... W₆₃
The message block, however, initially provides only 16 words.
The first 16 values are taken directly from the current 512-bit block:
W₀ ... W₁₅
The remaining ones:
W₁₆ ... W₆₃
are computed from words that already exist.
For this, SHA-256 defines two functions:
σ₀(x)
σ₁(x)
For SHA-256, the following applies:
σ₀(x) =
ROTR⁷(x)
XOR ROTR¹⁸(x)
XOR SHR³(x)
and:
σ₁(x) =
ROTR¹⁷(x)
XOR ROTR¹⁹(x)
XOR SHR¹⁰(x)
For t = 16 ... 63, this gives:
Wₜ =
σ₁(Wₜ₋₂)
+ Wₜ₋₇
+ σ₀(Wₜ₋₁₅)
+ Wₜ₋₁₆
All additions are performed modulo 2³².
From 16 words of the message block, a message schedule of 64 words in total is thus created.
What Does Rotate Right Mean?
ROTR stands for Rotate Right.
In a rotation, bits are shifted to the right. The bits that would fall off on the right are re-inserted on the left.
With a simplified 8-bit example:
x = 10110001
ROTR²(x)
10110001
└─┐
└┼────────┐
↓ ↓
01101100
The two right-hand bits 01 reappear on the left.
That is what distinguishes a rotation from an ordinary right shift.
What Does Right Shift Mean?
In a logical right shift, the bits are also shifted to the right.
The positions that become free on the left, however, are filled with zeros.
Example:
x = 10110001
SHR²(x)
00101100
Let's compare the two:
x 10110001
ROTR² 01101100
SHR² 00101100
In a rotation, all bits are retained and change position.
In a shift, bits disappear at the right edge.
SHA-256 uses both operations.
The 8-bit examples are for illustration only: SHA-256 itself always applies ROTR and SHR to 32-bit words.
Why Do 64 Words Result From This?
The message schedule ensures that the bits of a message block keep flowing into the following compression rounds in new combinations.
Each of the 64 rounds uses exactly one word:
Round 0 → W₀
Round 1 → W₁
Round 2 → W₂
...
Round 63 → W₆₃
The message data is thus distributed across the entire message schedule.
Step 5: The Initial Hash State
Before the first message block is processed, SHA-256 has a fixed, defined internal state.
It consists of eight 32-bit words:
H₀ = 6a09e667
H₁ = bb67ae85
H₂ = 3c6ef372
H₃ = a54ff53a
H₄ = 510e527f
H₅ = 9b05688c
H₆ = 1f83d9ab
H₇ = 5be0cd19
These initial hash values are part of the SHA-256 specification.
They correspond to the first 32 bits of the fractional parts of the square roots of the first eight prime numbers.
For each message block, eight working variables are first derived from them:
a = H₀
b = H₁
c = H₂
d = H₃
e = H₄
f = H₅
g = H₆
h = H₇
These eight values are now modified during the 64 rounds.
Step 6: The SHA-256 Functions
For the compression rounds, SHA-256 defines four central functions:
Ch
Maj
Σ₀
Σ₁
The capital sigma functions are not the same functions as the lowercase σ₀ and σ₁ from the message schedule.
Ch – Choose
The function Ch is defined as:
Ch(x,y,z) =
(x AND y)
XOR
(NOT x AND z)
Mathematically:
Ch(x,y,z) = (x ∧ y) ⊕ (¬x ∧ z)
The name Choose describes quite well what happens at the bit level.
For each bit, in simplified terms:
if x = 1 → choose the bit from y
if x = 0 → choose the bit from z
For individual bits, for example:
| x | y | z | Ch |
|---|---|---|---|
| 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
So x decides which of the two other inputs the result bit comes from.
Maj – Majority
The function Maj is defined as:
Maj(x,y,z) =
(x AND y)
XOR
(x AND z)
XOR
(y AND z)
or:
Maj(x,y,z) =
(x ∧ y) ⊕ (x ∧ z) ⊕ (y ∧ z)
The name stands for Majority.
For each bit, the value that occurs at least twice wins:
0 0 1 → 0
0 1 1 → 1
1 0 1 → 1
1 1 0 → 1
So Ch and Maj combine the current working variables in different ways.
The Capital Sigma Functions
In addition, the compression function uses:
Σ₀(x) =
ROTR²(x)
XOR ROTR¹³(x)
XOR ROTR²²(x)
and:
Σ₁(x) =
ROTR⁶(x)
XOR ROTR¹¹(x)
XOR ROTR²⁵(x)
Here, only rotations are used.
For comparison:
Message schedule:
σ₀ → ROTR 7, 18 + SHR 3
σ₁ → ROTR 17, 19 + SHR 10
Compression:
Σ₀ → ROTR 2, 13, 22
Σ₁ → ROTR 6, 11, 25
The distinction between lowercase and capital sigma is therefore important.
Step 7: The 64 Rounds
Now we have almost all the components together:
W₀ ... W₆₃
from the message schedule,
a b c d e f g h
as working variables, and four functions:
Ch
Maj
Σ₀
Σ₁
In addition, each round has its own constant:
K₀ ... K₆₃
For each round t, two temporary values are calculated.
The first is:
T₁ =
h
+ Σ₁(e)
+ Ch(e,f,g)
+ Kₜ
+ Wₜ
The second:
T₂ =
Σ₀(a)
+ Maj(a,b,c)
These additions, too, are performed modulo 2³².
What Does Addition Modulo 2³² Mean?
A 32-bit word can only represent values from:
0
to:
2³² - 1
If this range is exceeded in an addition, only the lower 32 bits of the result are used.
You can picture it as a counter that starts over from the beginning after reaching its highest value.
For example:
0xffffffff + 1
gives, in a 32-bit addition modulo 2³²:
0x00000000
This kind of addition is used in many places within SHA-256.
How Do T₁ and T₂ Change the State?
After T₁ and T₂ have been calculated, the eight working variables are updated.
For each round, the following applies:
h = g
g = f
f = e
e = d + T₁
d = c
c = b
b = a
a = T₁ + T₂
The two additions are again performed modulo 2³².
As a result, parts of the previous state move on, while T₁ and T₂ bring in new values.
Then the next round begins.
Round 0
↓
new values a ... h
↓
Round 1
↓
new values a ... h
↓
...
↓
Round 63
After 64 rounds, the working state has changed greatly.
What Are the Constants K₀ to K₆₃?
Each round additionally uses a fixed 32-bit constant:
K₀
K₁
...
K₆₃
For example, the list begins with:
K₀ = 428a2f98
K₁ = 71374491
K₂ = b5c0fbcf
K₃ = e9b5dba5
The 64 constants correspond to the first 32 bits of the fractional parts of the cube roots of the first 64 prime numbers.
These values, too, are completely public and defined in the Secure Hash Standard.
So they are not secret keys.
What Happens After the 64 Rounds?
The working variables don't simply replace the previous hash state.
Instead, they are added to the eight words of the previous hash state:
H₀ = H₀ + a
H₁ = H₁ + b
H₂ = H₂ + c
H₃ = H₃ + d
H₄ = H₄ + e
H₅ = H₅ + f
H₆ = H₆ + g
H₇ = H₇ + h
Again, each one modulo 2³².
This produces the new intermediate hash value.
If further 512-bit blocks are present, this state serves as the starting point for processing the next block.
Initial values
↓
Block 1
↓
H₀ ... H₇
↓
Block 2
↓
H₀ ... H₇
↓
Block 3
↓
...
So the blocks are not hashed independently of one another.
The state is carried forward from block to block.
How Does the Final SHA-256 Hash Arise?
After the last block has been processed, SHA-256 has eight final 32-bit words:
H₀
H₁
H₂
H₃
H₄
H₅
H₆
H₇
These are placed one after another:
H₀ || H₁ || H₂ || H₃ || H₄ || H₅ || H₆ || H₇
That gives us:
8 × 32 bits
=
256 bits
This is the SHA-256 digest.
The commonly used hexadecimal representation therefore has:
256 ÷ 4 = 64
hex characters.
Where Does the Avalanche Effect Come From?
Now it is also easier to see why a small change to the input can have such large effects.
A changed input bit can first change one word of the message block.
This word influences the message schedule:
W₀ ... W₆₃
The words of the message schedule in turn flow into the individual compression rounds.
There, they are combined with:
Ch
Maj
Σ₀
Σ₁
Kₜ
and the current state.
The changed state values then influence the following rounds.
A change can thus spread through the calculation.
With a good cryptographic hash function, it is to be expected that a small change to the input ultimately influences many bits of the output.
This behavior is called the avalanche effect.
In the article What Is the Avalanche Effect?, you can already try the effect out interactively.
In the next SHA-256 article, I go one step further and show how a concrete input moves through the algorithm.
Do I Have to Implement SHA-256 Myself?
For production cryptographic software, you should normally not implement SHA-256 yourself.
The description in this article serves to understand the algorithm.
In real software, established cryptographic libraries, or the cryptographic functions of the respective platform, should be used.
A custom implementation can contain errors despite the seemingly simple algorithm — for example in padding, byte order, 32-bit operations, or the processing of multiple blocks.
For learning purposes, on the other hand, a custom implementation is very interesting.
That is exactly what I will use for the interactive SHA-256 Explorer: a comprehensible learning implementation that can additionally be checked against known test vectors and an established SHA-256 implementation.
The Key Takeaways
SHA-256 doesn't process a message in a single step.
The process consists of several parts that build on one another:
- The message is padded.
- The prepared message is split into 512-bit blocks.
- Each block initially consists of 16 words of 32 bits each.
- From these, a message schedule with 64 words is created.
- SHA-256 has eight 32-bit words as its internal hash state.
- For each block, eight working variables
athroughhare used. - Each block goes through 64 compression rounds.
- The functions
Ch,Maj,Σ₀, andΣ₁combine parts of the current state. - Each round uses a word
Wₜand a constantKₜ. - The temporary values
T₁andT₂determine the next working state. - Additions are performed modulo
2³². - After 64 rounds, the result is combined with the previous hash state.
- After the last block,
H₀throughH₇are assembled into the 256-bit digest.
Where To Go From Here
With that, the individual components of the SHA-256 calculation are known.
The next article gets practical.
There, I take a concrete input and follow it step by step through the algorithm:
And you shouldn't only be able to look at my example values.
With the SHA-256 Explorer, you can then use an input of your own — your name, for example — and watch how bytes first arise from it, then a padded message block, the message schedule, the individual compression rounds, and finally your SHA-256 hash.
Then:
Henrik
doesn't just become:
SHA-256 → hash value
but a comprehensible path through the entire algorithm.
I show how SHA-256 differs from SHA-512 in SHA-256 vs. SHA-512 – What Is the Difference?.
Sources and Standards
The following primary and standards sources form the basis for the technical statements in this article:
-
National Institute of Standards and Technology (NIST), FIPS 180-4 – Secure Hash Standard (SHS) Primary source for SHA-256, padding, message schedule, functions, constants, and compression algorithm.
-
National Institute of Standards and Technology (NIST), Cryptographic Standards and Guidelines: Example Values Official example and test values for cryptographic algorithms.
-
National Institute of Standards and Technology (NIST), CAVP – Secure Hashing Test vectors and information on validating implementations of the Secure Hash Algorithms.