Goal
Understand cryptographic hash functions: what they do, how they differ from encryption, why some are broken (MD5), and how to use SHA-256 for file integrity verification.
Prerequisites: Week 1 (Cypherpunk Ideals & Threat Modeling)
This is Part 1 of 3 - Covers hashing fundamentals and integrity verification.
Introduction: Why Cryptography Matters
Before the algorithms, here is what we’re building toward:
Cryptography is mathematics applied to privacy. It turns the abstract right to privacy into concrete protection built on proofs.
- Hashing proves integrity: “This file hasn’t been tampered with”
- Encryption provides confidentiality: “Only intended recipients can read this”
- Key derivation strengthens secrets: “Each password guess becomes expensive for an attacker”
- Randomness enables security: “Attackers can’t predict your keys”
By the end of this week, you’ll understand how and why the encrypted files, signed messages and verified downloads you run into actually work.
1. What Is a Hash Function?
A cryptographic hash function takes arbitrary data and produces a fixed-size “fingerprint” called a digest.
Key properties:
- Deterministic - Same input always produces same output
- Fast - Quick to compute
- One-way - Cannot reverse digest back to input (mathematically hard)
- Avalanche effect - Tiny change in input completely changes output
- Collision resistant - Computationally infeasible to find two inputs with same hash
Why Hash Functions Are Not Encryption
Critical distinction:
HASHING:
"Cypherpunks write code" → SHA256 → edf9d5a6a0761c64... (CANNOT BE REVERSED)
ENCRYPTION:
"Cypherpunks write code" + KEY → AES → f8a3b2c1d4e5f6... (CAN BE DECRYPTED WITH KEY)
Hashes are for:
- File integrity verification (checksums)
- Password storage (never store plaintext, and never hash a password with plain SHA-256: fast hashes are cheap to brute-force. Use a slow, salted password hash such as Argon2id, scrypt or bcrypt; see Week 2b)
- Digital signatures (covered in Week 3)
- Proof-of-work (Bitcoin mining)
Encryption is for:
- Confidentiality (hiding message contents)
- Two-way transformation (need to decrypt later)
2. Common Hash Algorithms
| Algorithm | Output Size | Status | Use Case |
|---|---|---|---|
| MD5 | 128 bits | BROKEN | Never use (collisions found) |
| SHA-1 | 160 bits | DEPRECATED | Legacy systems only |
| SHA-256 | 256 bits | SECURE | General purpose, Bitcoin |
| SHA-512 | 512 bits | SECURE | Extra security margin |
| BLAKE2b | 512 bits | SECURE | Faster than SHA-2, modern |
| SHA-3 | Variable | SECURE | Different design, future-proof |
Why MD5 Is Broken: Understanding Collisions
A collision is when two different inputs produce the same hash.
Example of an MD5 collision (the real 2004 Wang et al. result):
# Two DIFFERENT 128-byte binary blocks that hash to the SAME MD5.
# These are raw binary, not text. They share the opening prefix shown
# here but diverge in a handful of bytes further in (not visible in this
# truncated view), yet produce an identical digest:
Block A: d131dd02c5e6eec4 693d9a06 98aff95c 2fcab5... ┐
Block B: d131dd02c5e6eec4 693d9a06 98aff95c 2fcab5... ┘ (differ in later bytes)
both → MD5: 79054025255fb1a26e4bc422aef54eb4 (IDENTICAL)
(These are excerpts of the published 2004 collision blocks; full vectors: https://en.wikipedia.org/wiki/MD5#Collision_vulnerabilities. The digest shown is the MD5 of the full 128-byte message. This pair differs in only a few bytes. Later chosen-prefix collisions (2007 onward) let an attacker make two meaningfully different files collide.)
Why this matters:
- Attacker creates malicious file with same hash as legitimate file
- You verify hash, think it’s safe, actually execute malware
- Digital signatures become worthless
SHA-256 is still secure - No collision has ever been found for full SHA-256, despite decades of public cryptanalysis. (Bitcoin mining runs it trillions of times a second, but miners are searching for low hash values, not collisions.)
3. Hands-On: Hashing with SHA-256
# Hash a simple message
echo "Cypherpunks write code" | sha256sum
# Output: edf9d5a6a0761c647bac6525b9bc8bf0b15f8ecd82e136a78da31e801436186e -
# (Run it yourself — you'll get exactly this. echo adds a trailing newline,
# so `printf '%s' "Cypherpunks write code" | sha256sum` gives a different digest.)
# Hash a file
echo "Privacy is a human right" > manifesto.txt
sha256sum manifesto.txt
# Output: <hash> manifesto.txt
# Even tiny changes completely alter the hash (avalanche effect)
echo "Privacy is a human right." > manifesto-period.txt # Added period
sha256sum manifesto-period.txt
# Completely different hash despite one character change!
Verifying File Integrity
Real-world scenario: You download a Linux ISO. How do you know it wasn’t modified?
# Download publishes SHA-256 hash on website
# Expected: a1b2c3d4e5f6...
# You compute hash of downloaded file
sha256sum ubuntu-24.04-desktop-amd64.iso
# Compare hashes - if they match, the file is intact (matches what the publisher listed)
sha256sum -c <<EOF
a1b2c3d4e5f6... ubuntu-24.04-desktop-amd64.iso
EOF
Why this works:
- Catches corrupted downloads, and swapped downloads from a mirror, as long as the hash itself came from a trusted source
- Matching a hash taken from the same compromised website proves nothing: an attacker who controls the page replaces the ISO and the hash together
- Authenticity comes from verifying the publisher’s GPG signature on the checksum file (SHA256SUMS.gpg). That is Week 3.
4. Lab: Explore the Avalanche Effect
# Create test files with minimal differences
echo "The quick brown fox jumps over the lazy dog" > fox1.txt
echo "The quick brown fox jumps over the lazy dog." > fox2.txt # Added period
echo "the quick brown fox jumps over the lazy dog" > fox3.txt # Lowercase 't'
# Hash all three
sha256sum fox*.txt
# Notice: Completely different hashes despite tiny input changes
# This is the avalanche effect in action
Deliverable: Screenshot or notes showing three completely different hashes from nearly identical inputs.
Up Next
Week 2b covers symmetric encryption with AES, encryption modes (why ECB is broken), and key derivation functions.
Key Takeaways
- Hash functions create fixed-size fingerprints from arbitrary data
- One-way function - Cannot reverse a hash to get original input
- MD5 is broken - Collisions found, never use for security
- SHA-256 is secure - No known collisions after decades of public cryptanalysis
- Avalanche effect - Tiny input change produces completely different hash
- Integrity verification - Compare hashes to detect file tampering