Hashing: How Passwords Stay Secret
About this lesson, for parents and teachers
When you sign up for a website, a good one never stores your actual password. So how does it know you typed the right one next time? With a one-way fingerprint called a hash.
Ages 12–14 · about 7 minutes · builds on Secret Codes With a Cipher Wheel · leads to Public-Key Secrets With Paint Mixing
Try this
- Tap
cat, thenbat. Count how many of the 32 bits flip when only one letter changes. - Type anything in the box and watch the step-by-step table mix in one byte at a time.
- Log in as Sam by trying passwords. The site only compares hashes.
- Press Try every PIN to crack a leaked 4-digit PIN hash by brute force.
How it works
A hash function takes any text, short or long, and produces a fixed-size number called a hash or digest. Ours outputs 32 bits, shown as 8 hex digits. Hashing is deterministic: the same input always gives the same output, so a hash works like a fingerprint for the text. Even a whole book hashes to the same small size.
This lesson uses a real algorithm called FNV-1a. It starts with a fixed number, then for every byte of the text it does two things: XOR the byte into the hash (h ^= byte), then multiply by a special prime, 16777619, keeping only 32 bits. The multiply spreads each change across many bits, so changing one letter flips about half of the 32 bits. That is called the avalanche effect.
Websites store the hash of your password, not the password. When you log in, they hash what you typed and compare the two hashes. If a hacker steals the database, they get hashes, not passwords. Good hashes are one-way: there is no formula to run them backwards. The only attack is guessing inputs and hashing each guess, called brute force.
Brute force is why short passwords are weak: there are only 10,000 four-digit PINs, so a computer tries them all instantly. FNV-1a is a fast toy and not safe for secrets. Real systems use SHA-256 (256 bits) for fingerprints, and for passwords they use bcrypt or Argon2, which are slow on purpose and add a random salt to each password so identical passwords get different hashes.
Where engineers use this
Every login system, Git commit ID, file download checksum and Bitcoin block uses hash functions. Password managers and sites like Have I Been Pwned compare hashes so they never need to see your real password.
Words to know
- Hash function
- An algorithm that turns any input into a fixed-size fingerprint number.
- Avalanche effect
- When a tiny change in the input changes about half of the output bits.
- Brute force
- Cracking a hash by trying every possible input until one matches.
- Salt
- Random data added to each password before hashing, so the same password gives different hashes for different people.
Talk about it
Goal: See how a hash turns any text into a fingerprint, and why one tiny change scrambles it.
- Why is it safer for a website to store your password's hash instead of the password itself?
- A 4-digit PIN has 10,000 options. How many options does an 8-character password with letters and digits have?
- Why would engineers want a password hash to be slow?
Without a screen: Invent a paper hash: give each letter its alphabet number, then for each letter multiply your running total by 7, add the letter, and keep only the last two digits. Hash a few words and see if changing one letter scrambles the result, and whether two words can collide.
Curriculum links: CSTA K-12 2-NI-05 (how information is protected), 3A-NI-07 (security measures and trade-offs); AP Computer Science Principles Big Idea 5 (cybersecurity: protecting data)
Questions
What is hashing in simple words?
Hashing turns any piece of text into a fixed-length fingerprint number. The same text always gives the same fingerprint, but you cannot work backwards from the fingerprint to the text.
How are passwords stored safely?
Good websites store a salted, slow hash of your password using an algorithm like bcrypt or Argon2. At login they hash what you typed and compare, so the real password is never saved.
Is hashing the same as encryption?
No. Encryption is two-way: with the right key you can decrypt the message back. A hash is one-way: it is designed so there is no way to recover the original input except by guessing.