Skip to main content

What Is SHA-256?

SHA-256 is one of the best-known cryptographic hash functions.

You encounter the algorithm in many places: when verifying files, in cryptographic protocols, in digital signatures, or as a building block of other cryptographic schemes.

From an input of any supported length, SHA-256 produces a hash value of exactly:

256 bits.

For example:

Hello World

becomes the following with SHA-256:

a591a6d40bf420404a011733cfb7b190
d62c65bf0bcda32b57b277d9ad9f146e

The hash consists of 256 bits and is typically represented as a 64-character hexadecimal number.

But how does SHA-256 actually arrive at this result?

In this article, we first look at the structure and the most important properties of the algorithm. I explain how the individual calculation steps work in detail afterward, in the article How Does SHA-256 Work?.

What Does SHA-256 Mean?​

SHA stands for:

Secure Hash Algorithm

The number 256 denotes the length of the hash value produced:

SHA-256
↓
256 bits
↓
32 bytes
↓
64 hex characters

The length of the input is not decisive for the length of the output.

A short text:

abc

produces a 256-bit hash just like a very large file.

I explain what a hash function fundamentally does in the article What Is a Hash Function?.

SHA-256 Belongs to the SHA-2 Family​

SHA-256 is not a single, isolated scheme, but belongs to the SHA-2 family.

NIST's Secure Hash Standard (FIPS 180-4) includes, among others:

AlgorithmHash length
SHA-224224 bits
SHA-256256 bits
SHA-384384 bits
SHA-512512 bits
SHA-512/224224 bits
SHA-512/256256 bits

The names can be somewhat misleading.

SHA-512/256, for example, also produces a 256-bit hash, but is internally based on the SHA-512 structure.

I go into this in detail in the article What Are SHA-512/224 and SHA-512/256?.

SHA-256 and SHA-512 also differ by more than just the length of their result. Among other things, they work with different block sizes, word sizes, and numbers of rounds.

I explain this difference in detail in the article SHA-256 vs. SHA-512 – What Is the Difference?.

What Happens Fundamentally in SHA-256?​

At a high level, SHA-256 can be represented roughly like this:

Input
↓
Padding
↓
512-bit blocks
↓
Message schedule
↓
64 rounds
↓
Internal hash state
↓
256-bit hash

Behind each of these steps is a precisely defined calculation.

To get started, five properties are particularly important:

Block size: 512 bits
Word size: 32 bits
Hash value: 256 bits
Working variables: 8 × 32 bits
Rounds: 64

We'll now look at these building blocks in a bit more detail.

SHA-256 Processes Data in 512-Bit Blocks​

SHA-256 doesn't process a message as one data block of arbitrary size.

Instead, the prepared message is divided into blocks of:

512 bits
=
64 bytes

each.

Such a block in turn consists of:

16 × 32 bits

These 32-bit units are often called words.

In simplified form:

512-bit block

┌──────┬──────┬──────┬──────┬─────┬──────┐
│ W₀ │ W₁ │ W₂ │ W₃ │ ... │ W₁₅ │
└──────┴──────┴──────┴──────┴─────┴──────┘

32 bits each

These first 16 words later form the basis for the so-called message schedule.

Why Does SHA-256 Need Padding?​

A message normally doesn't happen to have exactly a length that fits the block format SHA-256 requires.

It is therefore extended before the actual processing.

This process is called padding.

In simplified terms, the following happens:

original message
↓
append a 1 bit
↓
add 0 bits
↓
append the original message length
↓
multiple of 512 bits

The length of the original message is encoded in a 64-bit field at the end of the prepared message.

Take, for example:

abc

The three ASCII characters require:

3 × 8 bits
=
24 bits

After padding, the message has a length that can be divided completely into 512-bit blocks.

I show what this padding looks like bit by bit, using a real input, in the article "SHA-256 Step by Step."

What Is the Message Schedule?​

From each 512-bit message block, 16 words of 32 bits each are created first.

During its compression calculation, however, SHA-256 works with a total of:

64 words

Further words are therefore computed from the original 16 words.

In the end, there is:

W₀
W₁
W₂
...
W₁₅

from the message block, and then:

W₁₆
W₁₇
...
W₆₃

through the operations defined in the SHA-256 standard.

Together, these 64 words form the message schedule.

Each of the 64 rounds uses one of these words.

64 Rounds per Block​

For each 512-bit block, SHA-256 performs a total of:

64 rounds.

In doing so, the algorithm works with eight 32-bit working variables:

a
b
c
d
e
f
g
h

At the start of processing a block, they are initialized from the current internal hash state.

During each round, the values are modified using various operations.

These include, among others:

  • bitwise logical operations,
  • rotations,
  • shifts,
  • additions modulo 2³²,
  • a word from the message schedule,
  • a constant defined for the respective round.

The result of one round influences the next round.

After 64 rounds, the resulting values are combined with the previous hash state.

With multiple message blocks, this new state is then used for the next block.

The Internal State of SHA-256​

SHA-256 has an internal state of 256 bits in total.

It consists of eight 32-bit values:

H₀
H₁
H₂
H₃
H₄
H₅
H₆
H₇

Before the first message block is processed, these values have fixed, defined initial hash values.

After a block has been processed, a new state results from it:

Block 1
↓
Hash state 1
↓
Block 2
↓
Hash state 2
↓
Block 3
↓
...

After the last block, the eight 32-bit values are concatenated:

H₀ || H₁ || H₂ || H₃ || H₄ || H₅ || H₆ || H₇

Eight values of 32 bits each give:

8 × 32 bits
=
256 bits

That is the final SHA-256 hash value.

Where Do the Initial Hash Values and Constants Come From?​

The values SHA-256 works with were not chosen arbitrarily.

The eight initial hash values are derived from the fractional bits of the square roots of the first eight prime numbers.

The 64 round constants likewise come from the fractional bits of the cube roots of the first 64 prime numbers.

To use SHA-256, you don't normally have to calculate these values yourself — they are fixed in the standard.

For understanding the algorithm, though, it is interesting that these are publicly verifiable constants.

In the article How Does SHA-256 Work?, we look at some of these values directly.

How Secure Is SHA-256?​

An important distinction applies here, one we already got to know in the fundamentals.

SHA-256 has a 256-bit output.

That doesn't mean, however, that every security property also has a strength of 256 bits.

NIST lists the following security strengths for SHA-256, among others:

PropertySecurity strength
Preimage resistance256 bits
Collision resistance128 bits

For a generic preimage search, the workload is thus on the order of:

2²⁵⁶

For the generic search for a collision, by contrast, the birthday effect comes into play.

The effort is therefore on the order of:

2¹²⁸

This is not a special flaw of SHA-256, but the expected generic collision security of an ideal hash function with 256 bits of output.

I explain why in:

Second-preimage resistance is deliberately not in this simplified table: NIST specifies its strength for SHA-256 depending on the message length. I explain why in the article What Is Second-Preimage Resistance?.

What Happens With a Small Change to the Input?​

Another important property of cryptographic hash functions can be observed very well with SHA-256.

If we change:

Hello World

to:

hello World

only one character changes.

The resulting SHA-256 hash values nevertheless differ greatly from each other.

This behavior is called the avalanche effect.

A small change to the input typically leads to changes in many bits of the output.

In the article What Is the Avalanche Effect?, you can try this behavior out yourself with the interactive avalanche tool.

What Is SHA-256 Used For?​

Cryptographic hash functions such as SHA-256 are building blocks for numerous security mechanisms.

Hash values can be used, for example, to check data for changes.

If a SHA-256 hash obtained from a trustworthy source has been published for a file, you can calculate the hash of the downloaded file and compare the two values.

expected hash
↓
comparison
↑
hash of the downloaded file

If the values differ, the file is not identical to the expected input.

A matching hash alone does not automatically prove the authenticity of a file, however. For that, the reference value in particular must also come from a trustworthy source.

Hash functions are also used as building blocks of other cryptographic constructions, for example in connection with digital signatures, message authentication codes, and other cryptographic schemes.

It is important to note:

SHA-256 itself is not encryption and has no secret key.

Is SHA-256 Suitable for Passwords?​

A common question is:

Can I simply hash passwords with SHA-256 and then store the hash?

For secure password storage, a single fast SHA-256 hash is not the suitable method.

The problem is not that SHA-256 would be fundamentally insecure as a cryptographic hash function.

On the contrary: its high calculation speed is desirable in many applications.

In password attacks, however, this very speed helps the attacker, because a very large number of password candidates can be tried.

For passwords, suitable password-hashing schemes are therefore used, which work with a salt and a cost factor, among other things, and deliberately make guessing attempts more expensive. In SP 800-63B, NIST, too, provides for a suitable password-hashing scheme with a salt and a cost factor for stored passwords.

Why a strong general-purpose hash algorithm is therefore still not automatically a good password hash is something I cover in detail later in the Password Security section.

Try SHA-256 Yourself​

The properties of SHA-256 are easiest to grasp when you try out different inputs yourself.

Use the Hash Generator and calculate, for example, one after another:

Hello World
hello World
Hello World!

Pay attention to two things:

The length always stays the same.

Every SHA-256 hash has 256 bits, or 64 hex characters.

The content changes greatly.

Even small changes to the input produce completely different-looking hash values.

AlgorithmSHA-256

In the next step, we won't just look at the result anymore, but open up the algorithm itself.

The Key Takeaways​

SHA-256 is a cryptographic hash function from the SHA-2 family.

The most important properties:

  • SHA-256 produces a hash value of 256 bits.
  • The hash is usually represented as 64 hex characters.
  • SHA-256 processes prepared messages in 512-bit blocks.
  • The algorithm works with 32-bit words.
  • Each message block goes through 64 rounds.
  • Eight 32-bit working variables are used in the process.
  • From 16 words of the message block, a message schedule with 64 words is generated.
  • The internal hash state consists of eight 32-bit values.
  • After the last block, these together make up the 256-bit digest.
  • According to NIST, SHA-256 has 256 bits of preimage resistance and 128 bits of collision resistance.
  • SHA-256 is not encryption.
  • A plain SHA-256 hash alone is not the suitable method for password storage.

Where To Go From Here​

So far, we've looked at SHA-256 mainly from the outside.

We now know which large building blocks the algorithm consists of:

Padding
↓
512-bit blocks
↓
Message schedule
↓
64 rounds
↓
Hash state
↓
256-bit digest

In the next article, we open up this black box.

There, we take a closer look at the bit operations, functions, constants, and individual calculation steps:

How Does SHA-256 Work?

After that, in SHA-256 Step by Step, you can follow your own text through the algorithm.

Sources and Standards​

The following primary and standards sources form the basis for the technical statements in this article:

  1. National Institute of Standards and Technology (NIST), FIPS 180-4 – Secure Hash Standard (SHS) Specification of SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, and SHA-512/256 (update of August 2015).

  2. National Institute of Standards and Technology (NIST), Hash Functions Security properties and security strengths of the approved hash functions.

  3. National Institute of Standards and Technology (NIST), SP 800-63B – Digital Identity Guidelines Requirements for the secure storage of passwords.