Build SHA-256 from Scratch
Implement SHA-256 from raw bit operations. You'll build ROTR/Σ/σ/Ch/Maj, the message schedule, the 64-round compression function, padding, and the full Merkle-Damgård construction. Then add HMAC for message authentication. By the end you'll deeply understand the hash function used in Bitcoin, Git, TLS, and JWT.
intermediateFree3 hours12 lessons
Start learningor sign up to track progress
Curriculum
Loading reference solution…
Going further
Read
- Serious Cryptography (Aumasson) chapter 6 — covers SHA-2 family in depth.
- FIPS 180-4 (the spec itself, ~30 pages).
- The official test vectors — verify your implementation against these before declaring victory.
Build next
- HMAC-SHA-256: ~10 lines of
outer(inner(message))on top of what you have. Foundation of JWT, TLS, almost every authenticated message format. - PBKDF2 + HKDF: key derivation built on HMAC. PBKDF2 is the password hasher; HKDF is what TLS 1.3 uses to derive session keys.
- SHA-3 / Keccak: a totally different design (sponge construction). Worth implementing once because the structure is so different from SHA-2.
- BLAKE3: modern, faster, parallelizable. The reference implementation is small + readable.
Continue with our courses
- Build JWT — uses HMAC-SHA-256 directly.
- Build TLS 1.3 — uses HMAC, HKDF, and SHA-256 throughout.
- Build a Blockchain — uses SHA-256 for proof of work and Merkle roots.