Short answer: SHA-256 maps a message to a 32-byte digest. It pads with a 1 bit, zeros, and the 64-bit length, then compresses each 64-byte block into eight 32-bit words. The initial words are the first 32 bits of the fractional square roots of the first eight primes. The sixty-four round constants are the first 32 bits of the fractional cube roots of the first sixty-four primes. The digest of abc is ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad.
What the problem actually asks
A cryptographic hash has to map an arbitrary byte string to a fixed digest so that a changed input changes the digest and a search for a second input with the same digest is infeasible. SHA-256 is the Merkle-Damgard construction with this compression function. The message is not a secret key. The same message always produces the same digest. That is the wrong shape for a password store and the wrong shape for a tag, which is why a password uses a stretching function and a tag uses HMAC.
What the algorithm guarantees
Every message pads to a whole number of 64-byte blocks, including the length, so two messages of different lengths do not collapse to the same padded string. Each round updates eight working words with rotations, the choice function, the majority function, one message word, and one round constant. The fragment generates the constants. The first initial word is 0x6a09e667 and the first round constant is 0x428a2f98. The digest is 256 bits. The function is public.
The working steps
- Take the initial state from the fractional square roots of the first eight primes, and the round constants from the fractional cube roots of the first sixty-four primes.
- Append
0x80, zeros, and the bit length as a 64-bit integer so the length is a multiple of 64 bytes. - Expand each 64-byte block to sixty-four words and run sixty-four rounds.
- Add the working words back onto the state. The digest is the eight state words.
Approaches that look simpler and fail
| Approach | Why it fails as a password store or a tag |
|---|---|
| Store SHA-256 of a password | The function is fast and unsalted in this form. A password store uses a salt and a cost, as in PBKDF2 or scrypt. |
| Append a key and call the digest a tag | A secret prefix or suffix is not HMAC. HMAC uses the inner and outer pads. |
| Omit the length from the padding | The length block is what separates messages that would otherwise share a padded prefix. |
| Use the digest as an encryption | A hash does not have a decryption. It is a one-way digest. |
HMAC-SHA-256 is the keyed tag. PBKDF2 and scrypt are the password stretches. SHA-256 is the digest.
A working example
This Python fragment derives the SHA-256 words from the primes and hashes the bytes abc. It prints ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad.
import struct
def _primes(k: int) -> list[int]:
out: list[int] = []
n = 2
while len(out) < k:
if all(n % p for p in out if p * p <= n):
out.append(n)
n += 1
return out
def _isqrt(n: int) -> int:
x = int(n ** 0.5)
while x * x > n:
x -= 1
while (x + 1) * (x + 1) <= n:
x += 1
return x
def _icbrt(n: int) -> int:
x = int(round(n ** (1 / 3)))
while x * x * x > n:
x -= 1
while (x + 1) ** 3 <= n:
x += 1
return x
_PS = _primes(64)
_H256 = [_isqrt(p << 64) & 0xFFFFFFFF for p in _PS[:8]]
_K256 = [_icbrt(p << 96) & 0xFFFFFFFF for p in _PS]
def _rotr(x: int, n: int) -> int:
return ((x >> n) | (x << (32 - n))) & 0xFFFFFFFF
def sha256(data: bytes) -> bytes:
h = _H256[:]
bit_len = len(data) * 8
msg = data + b"\x80"
msg += b"\x00" * ((56 - len(msg) % 64) % 64)
msg += struct.pack(">Q", bit_len)
for off in range(0, len(msg), 64):
w = list(struct.unpack(">16I", msg[off : off + 64]))
for i in range(16, 64):
s0 = _rotr(w[i - 15], 7) ^ _rotr(w[i - 15], 18) ^ (w[i - 15] >> 3)
s1 = _rotr(w[i - 2], 17) ^ _rotr(w[i - 2], 19) ^ (w[i - 2] >> 10)
w.append((w[i - 16] + s0 + w[i - 7] + s1) & 0xFFFFFFFF)
a, b, c, d, e, f, g, hh = h
for i in range(64):
s1 = _rotr(e, 6) ^ _rotr(e, 11) ^ _rotr(e, 25)
ch = (e & f) ^ (~e & g)
t1 = (hh + s1 + ch + _K256[i] + w[i]) & 0xFFFFFFFF
s0 = _rotr(a, 2) ^ _rotr(a, 13) ^ _rotr(a, 22)
maj = (a & b) ^ (a & c) ^ (b & c)
t2 = (s0 + maj) & 0xFFFFFFFF
hh, g, f, e, d, c, b, a = g, f, e, (d + t1) & 0xFFFFFFFF, c, b, a, (t1 + t2) & 0xFFFFFFFF
h = [(x + y) & 0xFFFFFFFF for x, y in zip(h, (a, b, c, d, e, f, g, hh))]
return struct.pack(">8I", *h)
assert _H256[0] == 0x6A09E667 and _K256[0] == 0x428A2F98
digest = sha256(b"abc")
assert digest.hex() == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
print(digest.hex())
Where it is used
Bitcoin hashes block headers with SHA-256, TLS and certificate fingerprints use it, and Git’s new object ids are SHA-256. HMAC-SHA-256 is the keyed form. A password store still wants a salt and a cost on top of the digest.
When you can drop this algorithm
Drop raw SHA-256 as a password hash and as a message tag. Keep it as the 256-bit digest inside HMAC, HKDF, and a signature that hashes before signing.
Why this stays published
We publish this so a search for SHA-256 returns the prime-derived words and the digest of abc. SHA-256 pads a message with its length and compresses each 64-byte block with sixty-four rounds into a 32-byte digest.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
