SHA-256 Step by Step
In the article How Does SHA-256 Work?, I explained the components of the algorithm: padding, the message schedule, the functions Ch, Maj, Σ₀, and Σ₁, and the 64 compression rounds.
This article is different. Instead of just looking at the theory, here you can enter your own name or any short text and follow the actual SHA-256 calculation for it.
All the values the explorer shows come from a real SHA-256 calculation for exactly your input. SHA-256 is the best-known member of the SHA-2 family.
SHA-256 Explorer
What happens inside SHA-256 when you hash your own name? Here you see the real calculation – step by step.
This implementation is for illustrating SHA-256. For production cryptographic applications, you should use your platform's cryptography functions or an established library.
The explorer guides you through the algorithm in seven steps:
- Input & bytes
- Padding
- 512-bit block
- Message schedule
- Initial state
- 64 compression rounds
- Final hash
I go through these seven steps one after another below. Open them in the explorer in parallel and look at your own values each time.
The explorer is a learning implementation. It serves to make SHA-256 understandable and is not a cryptography library for production use.
Step 1: Your Text Becomes Bytes
SHA-256 doesn't process the visible characters, but bytes.
For a text to become bytes, it has to be encoded. On the web, UTF-8 is the usual choice for this, and the explorer uses UTF-8 as well. Each character is translated into one to four bytes.
The number of characters and the number of bytes therefore don't have to match. A simple Latin character such as a consists of one byte. Characters such as ä, é, or ñ require two bytes, and many emoji require four.
Enter your name or text in the explorer and start the exploration. In step 1, you see the bytes belonging to each character in hexadecimal and binary notation, as well as the total number of bytes and bits of your input.
Feel free to try characters with umlauts or accents as well, and watch how the byte count changes.
Step 2: Padding
SHA-256 processes messages in blocks of 512 bits each. Your input is almost always too short for that and is therefore padded:
[ message ][ 1 ][ zeros ][ original length ]
The following happens:
- Directly behind your message, a single
1bit is appended. - After that come as many
0bits as are needed so that exactly 64 bits remain free at the end. - The last 64 bits contain the original length of your message in bits.
For the short inputs the explorer supports, exactly one block of 512 bits results this way.
How many zeros there are depends on the length of your input. The explorer therefore calculates the sizes of the individual sections for your text. Click “Show bits” to see the individual bits of the padding.
Step 3: The 512-Bit Block
The padded block consists of 512 bits. SHA-256 works with 32-bit words:
512 bits / 32 bits = 16 words
These 16 words are called:
W₀ ... W₁₅
Select W₀ in the explorer. There, you'll probably recognize the first bytes of your own input, because the bytes are simply grouped into words in groups of four.
Directly behind the bytes of your input, you'll find the appended 1 bit as the byte 80. The encoded original bit length of your input is in the last two words, W₁₄ and W₁₅. With short inputs, W₁₄ is zero and the length is entirely in W₁₅.
Step 4: The Message Schedule
The 64 compression rounds need 64 words, but the block provides only 16. The words
W₀ ... W₁₅
are therefore extended to
W₀ ... W₆₃
Each new word is formed from four earlier words. For W₁₆, the formula is:
W₁₆ =
σ₁(W₁₄)
+ W₉
+ σ₀(W₁)
+ W₀
mod 2³²
Select W₁₆ in the explorer. There, you see the actual operands for your input and the result of the addition. Via “Break down σ₀/σ₁,” you can also look at the underlying ROTR and SHR operations.
Then select further words from W₁₇ onward. In the grid, you can tell from the border which words come from the message and which were computed.
Step 5: The Initial State
Before the first block is processed, SHA-256 has a fixed starting state of eight words:
H₀ = 6a09e667
H₁ = bb67ae85
H₂ = 3c6ef372
H₃ = a54ff53a
H₄ = 510e527f
H₅ = 9b05688c
H₆ = 1f83d9ab
H₇ = 5be0cd19
These values are defined in the standard. They are the same for every input and therefore don't depend on your text.
For processing a block, they are copied into the eight working variables:
a = H₀
b = H₁
c = H₂
d = H₃
e = H₄
f = H₅
g = H₆
h = H₇
Step 6: The 64 Compression Rounds
Now the core of the algorithm begins. Open round 0 in the explorer.
In each round, two auxiliary values are calculated:
T₁ =
h + Σ₁(e) + Ch(e,f,g) + K₀ + W₀
mod 2³²
and:
T₂ =
Σ₀(a) + Maj(a,b,c)
mod 2³²
Then the new state is created:
a' = T₁ + T₂
b' = a
c' = b
d' = c
e' = d + T₁
f' = e
g' = f
h' = g
Most registers are therefore simply shifted along. Only a' and e' are newly computed.
The explorer shows you all of these values for your input. Now move the slider and go through the rounds one after another:
0 → 1 → 2 → ... → 63
Each round uses its own word Wₜ from the message schedule and its own constant Kₜ, and changes the internal state further.
What Does “Changed Bits” Mean?
Below the round state, the explorer also shows in how many of 256 bits the state before the selected round differs from the initial state. The bits of the eight working variables a through h are compared with the bits of H₀ through H₇.
This number is a learning aid. It shows how far the internal state has moved away from the initial state over the course of the rounds. It is neither an avalanche percentage nor a security measurement.
I cover the avalanche effect itself in the article What Is the Avalanche Effect?. There, the subject is the comparison of two inputs and their finished hash values. You can also try this out in the Avalanche Effect Visualizer.
Step 7: Feed-Forward and Final Hash
After round 63, the working variables are added to the previous hash state:
H₀ = H₀ + a
H₁ = H₁ + b
...
H₇ = H₇ + h
Each addition is performed modulo 2³².
For messages with multiple blocks, this new state would be the starting state for the next block. The explorer shows only one block, so here it is already the final result.
Finally, the eight words are placed one after another:
H₀ || H₁ || ... || H₇
That gives:
8 × 32 bits = 256 bits
In step 7 of the explorer, you see the feed-forward addition with your values, and below it the complete SHA-256 hash of your input. I explain how much security these 256 bits mean in How Secure Are 128, 256, and 512 Bits?.
Experiment: Change a Single Character
- Copy your hash or remember the first few characters.
- In the explorer, change a single character of your input.
- Start the exploration again.
- Compare the new hash with the old one.
Watch where the change begins: a changed byte changes one word of the block. This word flows into several words of the message schedule, and these in turn change the compression rounds. That is why a large part of the hash differs in the end.
I explain this effect and its limits in What Is the Avalanche Effect?. You can compare two inputs directly in the Avalanche Effect Visualizer.
Why Only Short Inputs?
The explorer accepts inputs of up to 55 UTF-8 bytes. This is a limitation of the presentation, not a limitation of SHA-256.
SHA-256 itself processes messages of very large length. The learning implementation behind the explorer can also already calculate multiple blocks. The interface, however, deliberately shows only messages that fit into a single 512-bit block after padding: 55 bytes, a 1 bit, and the 64-bit length field add up to exactly 512 bits.
This keeps the presentation clear. With multiple blocks, the same calculation would be repeated, with the hash state passed on from block to block.
Note that the limit applies in bytes, not in characters. With characters such as ä or emoji, you reach it sooner.
Is This Really SHA-256?
Yes. The explorer performs the real SHA-256 operations. It is based on NIST's Secure Hash Standard (FIPS 180-4) and calculates the padding, message schedule, and all 64 rounds itself, instead of just displaying a finished result.
I checked the implementation against official test values and against independent implementations of the platform. It nevertheless remains a learning implementation: it is designed for comprehensibility, not for speed or resistance to side-channel attacks. For production applications, you should use your platform's cryptography functions or an established library.
The Key Takeaways
You've followed SHA-256 from beginning to end with your own input:
Text
↓
UTF-8
↓
Padding
↓
512-bit block
↓
W₀ ... W₁₅
↓
W₀ ... W₆₃
↓
Initial state
↓
64 compression rounds
↓
Feed-forward
↓
256-bit hash
- SHA-256 processes bytes, not visible characters.
- The number of characters and the number of bytes can differ.
- The padding adds a
1bit, zeros, and the original length. - A block consists of 16 words of 32 bits each.
- The message schedule extends them to 64 words.
- The initial state is the same for every input.
- The 64 rounds change the state with
Wₜ,Kₜ,T₁, andT₂. - In the feed-forward, the state and the working variables are added.
H₀ || ... || H₇gives the 256-bit hash.
Where To Go From Here
With this article, three pieces belong together:
- What Is SHA-256? explains structure, properties, and security strength.
- How Does SHA-256 Work? explains the components of the algorithm.
- SHA-256 Step by Step shows the calculation for your own input.
You can also find the explorer as a standalone tool in the Tools section.
Next, I compare SHA-256 with SHA-512, using your own input: SHA-256 vs. SHA-512 – What Is the Difference?. SHA-512 is not simply SHA-256 with a longer output: it works with 64-bit words, 1024-bit blocks, and 80 rounds instead of 64.
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 values for SHA-256 that can be used to check an implementation's intermediate results for specific messages. NIST does not provide reference values for arbitrary inputs of your own.
-
National Institute of Standards and Technology (NIST), CAVP – Secure Hashing Test vectors and information on validating implementations of the Secure Hash Algorithms.