Software

Why You Shouldn't Store Passwords with SHA-256

·10 min read·한국어
ContentsThree promises a hash keepsFour steps that mix up 'abc'How 'abc' becomes one 512-bit blockConstants taken from primes64 rounds: one turn that mixes eight wordsChange one letter, flip half the bitsWhy not for passwordsReason 1: it is too fastReason 2: the same password gives the same hashWhat to use: functions that are slow and memory-hungry on purposeCollision attacks have reached step 35 of 64Still strengths in what it was made forReferences

As a developer you see 64-digit hex strings all the time. The digest after sha256: that scrolls past when you pull a container image, the SHA-256 checksum on a download page, the shasum -a 256 you type to check a file you just downloaded. They are all output from a hash function called SHA-256. Here is what you get for the three letters abc.

$ printf abc | shasum -a 256
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad  -

Whether the input is three letters or a multi-gigabyte file, the output is always 256 bits, 64 hex digits. And SHA-256 is still an excellent, unbroken hash as of November 2026. Even so, you should not store passwords with it. In short, two of SHA-256's strengths, being fast and always giving the same output for the same input, turn directly into weaknesses when the thing you are hashing is a password.

Three promises a hash keeps

SHA-256 is specified in FIPS 180-4 from the US National Institute of Standards and Technology (NIST). The document defines a secure hash like this: computationally infeasible 1) to find a message that corresponds to a given message digest, or 2) to find two different messages that produce the same message digest. This is usually split into three promises.

  • Preimage resistance: given only a hash, you cannot find the input.
  • Second-preimage resistance: given one input, you cannot find a different input with the same hash.
  • Collision resistance: you cannot find any two inputs with the same hash.

'Cannot find' means the amount of computation is too large to be practical. The password story starts here. All three promises stop you from computing an input 'backward' from a hash. None of them stops an attacker from hashing candidate passwords one by one and comparing them to a stored value. That is computing the hash 'forward', and a hash function is built to do exactly that quickly.

Four steps that mix up 'abc'

How 'abc' becomes one 512-bit block

SHA-256 processes input in 512-bit (64-byte) blocks, so it first pads the input to fit. The rule in FIPS 180-4: append a single 1 bit to the message, add 0 bits until the length leaves a remainder of 448 when divided by 512, then write the original message length in the final 64 bits.

abc is 24 bits. Add one 1, 423 0s and a 64-bit length field, and you get 24 + 1 + 423 + 64 = 512 bits, exactly one block. The FIPS document uses this very example.

The top bar draws a 512-bit block to scale: 24 blue message bits at the left, a single red 1 bit, 423 hatched zero bits, and a 64-bit orange length field at the right. The middle shows the same block as 64 hex bytes: 61 62 63, then 80, then zeros, ending in 18. The bottom shows that a 55-byte message fits in one block, while a 56-byte message needs two blocks because there is no room left for the length field.

The rule creates an interesting boundary. The 64-bit length field has to sit at the end of the block, so the message plus the 1 bit must fit in 448 bits for one block to be enough. Up to 55 bytes (440 bits) you get one block. From 56 bytes (448 bits), adding the 1 already passes 448, so a whole extra block of zeros is added and the length goes at its end.

Most passwords are shorter than 55 bytes. So hashing one password with SHA-256 means running the compression function exactly once. For an attacker, checking one guess is just as cheap.

Constants taken from primes

SHA-256 has two sets of constants: eight initial values H(0) that the hash starts from, and 64 round constants K, one per round. At first glance they look arbitrary.

H(0) = 6a09e667 bb67ae85 3c6ef372 a54ff53a 510e527f 9b05688c 1f83d9ab 5be0cd19
K    = 428a2f98 71374491 b5c0fbcf e9b5dba5 ... c67178f2   (64 values)

FIPS 180-4 says where they come from. H(0) is the first 32 bits of the fractional parts of the square roots of the first eight primes (2, 3, 5, …, 19). K is the same for the cube roots of the first 64 primes (2 through 311).

From the password side, it means there is no secret anywhere in the algorithm. The constants and the rules are public, so the attacker's SHA-256 computes exactly what the server's does. Whatever protects a password has to live outside the hash function: a salt, and a function made slow on purpose.

64 rounds: one turn that mixes eight words

The compression function carries a state of eight 32-bit words, a through h. For the first block that state is H(0). One round mixes the eight words once, and this repeats 64 times. The figure shows the actual values that change in the first round (t = 0) for abc.

The top row is the state a to h before the round, with each value (the initial value H(0)). The bottom row is the state after the round. b, c, d and f, g, h inherit the value of the word to their left, shifting one place. Only a (5d6aebcd) and e (fa2a4622), drawn in red, are newly computed. Two boxes below explain T2 = Σ0(a) + Maj(a,b,c) and T1 = h + Σ1(e) + Ch(e,f,g) + Kt + Wt, with arrows showing new a = T1 + T2 and new e = d + T1.

As the figure shows, six words simply shift over. Two values are newly computed.

T1=h+Σ1(e)+Ch(e,f,g)+Kt+WtT2=Σ0(a)+Maj(a,b,c)a′=T1+T2,e′=d+T1\begin{aligned} T_1 &= h + \Sigma_1(e) + \mathrm{Ch}(e,f,g) + K_t + W_t \\ T_2 &= \Sigma_0(a) + \mathrm{Maj}(a,b,c) \\ a' &= T_1 + T_2, \quad e' = d + T_1 \end{aligned}

Every addition here is modulo 2^32. The four functions have unfamiliar names, but what they do is simple.

  • Σ0, Σ1: rotate the word (ROTR) by three different distances and XOR the results. A change in one bit spreads to three places in the word.
  • Ch(e, f, g): short for 'choose'. For each bit, take f's bit if e is 1 and g's bit if e is 0.
  • Maj(a, b, c): short for 'majority'. For each bit, take whichever value most of a, b and c have.
  • K_t: the cube-root constant from earlier. Adding a different value each round keeps the rounds from behaving identically.
  • W_t: a word made from the message. The first 16 (W_0 to W_15) are the 512-bit block cut into 32-bit pieces, and the other 48 are made by rotating, shifting (σ0, σ1) and adding earlier W values.

After 64 rounds, the final state is added word by word to the starting state, and that is the result for this block. If there are more blocks, the result becomes the next block's starting state, and the eight words of the last block's result, joined together, are the hash. abc has a single block, so this is the ba7816bf… from the top of the post.

Look at the operations again: 32-bit addition plus bit operations like rotation and shifting, XOR, AND and NOT. These are the fastest kinds of operations a CPU has, and modern chips even ship dedicated instructions for the round computation. That is where the 'too fast' reason below comes from.

Change one letter, flip half the bits

After 64 rounds, a tiny difference in the input spreads across the whole output: the avalanche effect. Try changing one letter in either of the two inputs below. In the 256-bit grid, the disc flips over in every cell where the two hashes differ, and the comparator window shows the count as N / 256 (xx.x%).

Avalanche effectInteractive
The inputs differ by one character, yet close to half the output bits flipped.
122
Flipped disc: a bit that differs between the hashesUnflipped disc: a bit that is the sameEach disc is one bit of the hash output. One row of the panel is four hex digits (16 bits) of the hashes above, laid out from the top left in the order the hex is read. So a place on the grid is a place in the output, not a character position in the input. Change a single character and the flipped discs do not gather anywhere; they scatter across the whole panel, and there are about half of 256 of them.

A flip-disc panel showing the SHA-256 avalanche effect: each of its 256 discs is one output bit, and a disc flips over when that bit differs between the hashes of input A and input B. Change one character (abc to abd) and 122 of 256 bits (47.7%) flip. The grid follows the order of the output bits, not the positions of the input characters.

abc and abd differ only in the last letter, yet their hashes differ in 122 of 256 bits (47.7%). Two random 256-bit values differ in 128 bits on average, so the two hashes look like unrelated values.

abc → ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
abd → a52d159f262b2c6ddb724a61840befc36eb30c88877a4030b65cbe86298449c9

So you cannot tell from the hashes that two passwords are similar, like password1 and password2. But that is all the avalanche effect does. The same input gives the same hash no matter how many times you feed it in. That property is the second reason in the next section.

Why not for passwords

Reason 1: it is too fast

A hash function is meant to check large files quickly, so faster is better. For this post, during research, we measured throughput by repeatedly hashing a 1 MB buffer with Python hashlib (OpenSSL 3.6.3) on an Apple M2 Mac. SHA-256 reached 2,265 MB per second, about 3.5 times faster than MD5 (634 MB).

Bar chart of throughput when repeatedly hashing a 1 MB buffer with Python hashlib (OpenSSL 3.6.3) on an Apple M2. SHA-256 is longest at 2,265 MB/s, followed by BLAKE2b at 675 MB/s, MD5 at 634 MB/s and SHA3-256 at 523 MB/s, all of similar length.

SHA-256 beats the simpler MD5 because of hardware. The M2 has dedicated SHA-256 instructions (hw.optional.arm.FEAT_SHA256: 1). That is welcome news for checking file integrity, and equally welcome news for whoever is guessing passwords.

Passwords cannot keep up with that speed. The number of passwords people actually choose is nowhere near 2^256. When one hash is cheap, an attack that hashes a list of common passwords and their variations finishes in a practical amount of time.

Reason 2: the same password gives the same hash

SHA-256 has no secret, so the hash of password123 is the same on every server. If two users pick the same password, the same value lands in the database twice.

On the left, SHA-256 without a salt: users A and B both use password123, and both hashes start with ef92b778bafe771e. On the right, each user gets a different salt (9f2c41d8, 5be07a13), and the same password produces different hashes, 73e603ef… and 14acf319….

This lets the attacker do the work ahead of time. Hash common passwords once and keep them in a table (a rainbow table is a way of storing such a table in less space), and cracking a leaked list of hashes becomes a lookup. The computation happens once, and the same table works against every service that leaks.

The fix is to mix a random value that differs per user, a salt, into the password. As on the right of the figure, the same password now gives different hashes, so a precomputed table is useless and the attacker has to start over for each user. A salt alone leaves reason 1 untouched, so in practice you use the functions in the next section.

What to use: functions that are slow and memory-hungry on purpose

Password hashing functions are designed the opposite way from SHA-256. They build in a salt and deliberately spend time and memory on each computation. The server only computes it once per login, so a little slowness is bearable. OWASP's general rule is that one hash should take less than a second. An attacker who has to try billions of guesses pays that cost every single time.

The OWASP Password Storage Cheat Sheet recommends these, in this order, with these minimum settings (for scrypt, N is the CPU and memory cost, r the block size and p the parallelism).

FunctionOWASP minimumNote
Argon2id19 MiB of memory, 2 iterations, parallelism 1First choice
scryptN = 2^17, r = 8, p = 1When Argon2id is not available
bcryptwork factor 10 or moreOnly in legacy systems where Argon2 and scrypt are not available. 72-byte input limit
PBKDF2600,000 or more iterations of HMAC-SHA-256When FIPS compliance is required

Below are Python examples, simplified for explanation. First, what not to do.

import hashlib

# Don't: done in one pass, and the same password always gives the same value
stored = hashlib.sha256(password.encode()).hexdigest()

Here it is with Argon2id, using the argon2-cffi package and the OWASP minimums from the table.

# pip install argon2-cffi
from argon2 import PasswordHasher
from argon2.exceptions import VerifyMismatchError

ph = PasswordHasher(time_cost=2, memory_cost=19 * 1024, parallelism=1)  # 19 MiB, 2 passes, parallelism 1

stored = ph.hash(password)  # '$argon2id$v=19$m=19456,t=2,p=1$<salt>$<hash>' all in one string

try:
    ph.verify(stored, attempt)  # True if it matches
except VerifyMismatchError:
    ...  # wrong password

The stored string carries the algorithm, the settings and the salt, so there is no separate salt to manage.

As the PBKDF2 row in the table above shows, SHA-256 does not disappear from password storage entirely. Wrapped in HMAC and repeated 600,000 times, it is fit for storing passwords. The problem is not SHA-256 itself but running it just once.

Collision attacks have reached step 35 of 64

Research into breaking SHA-256 starts with versions that have fewer rounds (papers call them steps). How many steps have produced an actual colliding pair is the gauge of how close attacks have come.

A bar marked from 0 to 64 steps. 0 to 31 is light blue, 31 to 35 is dark blue, and 35 to 37 is hatched. Step 31 is labeled December 2024 ASIACRYPT, 1.2 hours on 64 threads, and step 35 is labeled May 2026 ePrint (CRYPTO 2026). Steps 36 to 37 are theory only, step 38 failed because the probability was too low, and step 64, the real SHA-256, has no practical collision.

In December 2024, the ASIACRYPT talk The First Practical Collision for 31-Step SHA-256 presented an actual colliding pair, which per the slides took 1.2 hours on 64 threads.

In May 2026 the record moved again. Pushing the Limit of Memory-efficient Collision Attack Framework for SHA-2, accepted to CRYPTO 2026, reported the first practical collision attacks on 35-step SHA-256 and SHA-512. Practical collisions moved 4 steps further, and theoretical attacks reached 36 to 37 steps.

That is 4 more steps in two years. Still, there is no practical attack on all 64 steps, and counting steps alone, 29 remain.

Still strengths in what it was made for

Passwords call for care, but being fast and always giving the same output are plain strengths in what SHA-256 was made for. Identical content gets an identical name, so files and data can be referred to by their content.

Six panels showing where SHA-256 is used as of November 2026. Bitcoin mining applies SHA-256 twice to the block header. TLS certificates are hashed with SHA-256 or similar and then signed, and SHA-1 certificates have not been issued since 2016. Git still defaults to SHA-1 with SHA-256 as an option, and SHA-256 is planned to become the default for new repositories in Git 3.0. Docker and OCI images must support SHA-256, with SHA-512 optional. npm package-lock uses SHA-512, not SHA-256. SRI requires support for SHA-256, 384 and 512.

References

Standards and official documents

Password storage

Attacks and incidents

Where it's used

Quantum

GeekNews (Korean)

Cover photo: Robert Stump on Unsplash.