SHA-2
SHA-256 is the hash under TLS certificates, software signatures, Git's newer object format and Bitcoin. It was published in 2001, its predecessor SHA-1 has since fallen to practical collisions, and SHA-256 itself has no known attack that comes close to the full function.
- Rests on
- Cryptanalysis of the compression function; no reduction to a mathematical problem
- Standards
- FIPS 180-4
- Sizes
- SHA-224 and SHA-256 on 32-bit words; SHA-384 and SHA-512 on 64-bit words
- Best known attack
- None on the full functions; collisions are known only for reduced-step versions
- Quantum
- Generic speedups only
The construction
SHA-2 uses the Merkle–Damgård construction, proposed independently by Merkle and by Damgård in 1989. The message is padded to a multiple of the block size, with its length in bits written into the last block, and then processed one block at a time by a compression function that maps the current 256-bit state and a 512-bit block to a new state. The final state is the hash. If the compression function is collision-resistant, so is the whole hash; the length in the padding is what makes that proof go through.
SHA-256's compression function expands the 16 words of a block into a schedule of 64 words, then runs 64 steps of additions, rotations and two bitwise functions, choose and majority, over eight 32-bit state words. Each step adds one word of the schedule and one round constant. At the end the result is added back to the input state, which makes the function hard to run backwards.
Nothing up the sleeve
A designer who chose the 64 round constants freely could, in principle, have chosen them to hide a weakness. SHA-256 avoids the question by deriving them: each constant is the first 32 bits of the fractional part of the cube root of one of the first 64 primes, 2 to 311, and the eight words of the initial state come from the square roots of the first eight primes in the same way. The code behind this page computes all of them with integer roots, and they come out as the published values: K0 = 428a2f98 from the cube root of 2, K63 = c67178f2 from 311, and the first initial word 6a09e667 from the square root of 2.
With the constants in place the whole function is about forty lines. It reproduces the standard examples, among them the hash of "abc":
SHA-256("abc") =
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
Recomputed at build time, with the empty-string and two-block examples, and compared with Python's hashlib on 200 random inputs
Avalanche
A good hash behaves like a random function: changing any input bit should change each output bit with probability one half. The build hashed 500 random 32-byte messages, flipped one random bit in each, and counted how many of the 256 output bits changed. The mean was 128.1, against 128 for a random function, and the counts ranged from 105 to 151, which is the spread expected from 256 fair coin flips.
Cryptanalysis
SHA-2 has held up. The best published collision attacks reach fewer than half of SHA-256's 64 steps, and preimage attacks reach somewhat further but at costs close to brute force. Nothing comparable to the attacks on its predecessors exists: MD5 collisions became practical in 2004, and SHA-1 fell to a public collision in 2017 and to chosen-prefix collisions, the kind that forge certificates, in 2020. The design of SHA-2 is related to SHA-1, which is why NIST ran the open competition that produced SHA-3 as a structurally different alternative. That precaution has not been needed so far.
Length extension, and HMAC
The Merkle–Damgård construction has one structural property that matters in practice. The hash output is the internal state after the last block, so anyone who knows SHA-256(m) and the length of m can continue hashing from that state and compute the hash of m followed by its padding and any further data, without knowing m. That breaks the naive message authentication code SHA-256(key ‖ message), and it has broken real APIs.
The standard fix is HMAC, by Bellare, Canetti and Krawczyk, which hashes twice with the key mixed in at both ends, and whose security follows from properties of the compression function. SHA-512/256 and SHA-384, which output only part of the state, and SHA-3, which is not a Merkle–Damgård design, do not have the property at all.
The quantum picture
Quantum computers give only generic speedups against hash functions. Grover's algorithm finds preimages of an n-bit hash in about 2n/2 steps, and quantum collision search is generally not expected to beat classical collision search once its memory and communication costs are counted. SHA-256 therefore keeps its role in the post-quantum world: SLH-DSA is built entirely on it or on SHAKE, and ML-KEM and ML-DSA use SHA-3 and SHAKE throughout.
Primary sources
- NIST FIPS 180-4, Secure Hash Standard (SHS) (2015)
- R. C. Merkle, "One way hash functions and DES", CRYPTO 1989
- I. B. Damgård, "A design principle for hash functions", CRYPTO 1989
- M. Bellare, R. Canetti and H. Krawczyk, "Keying hash functions for message authentication", CRYPTO 1996
- M. Stevens, E. Bursztein, P. Karpman, A. Albertini and Y. Markov, "The first collision for full SHA-1", CRYPTO 2017