#281 How it works, by example 6.4 min
How Sites Store Passwords: Hash, Salt and Slow Hashes
Two users, one password, the same plain hash. A salt and a slow hash fix it.
Give bob a different password
Copy the code folder first. In store.py change bob's password from dragon42 to dragon43, then run python run.py. Predict whether bob and dave still share a row in the plain table, and how many users get cracked. You should see different rows for bob and dave, and 2 of 4 cracked in 8 hashes for the plain table. Change bob's password back to dragon42 afterwards, because the next exercise starts from the original file.
A hint
The attacker's wordlist in crack.py does not contain dragon43.
Put carol's password on the wordlist
In crack.py add the text otter-kettle-lantern to the end of the WORDLIST. Predict how many users are cracked and how many hashes each attack uses, then run python run.py. You should see 4 of 4 cracked, with 9 hashes for the plain table and 25 for the salted one.
A hint
The plain attack hashes each word once. The salted attack hashes guesses again for every user.
Predict a near-miss login
In run.py, change the right-password check for carol so the attempt has a capital O at the start, for example Otter-kettle-lantern. Predict what verify prints, then run python run.py slow. The printed label still says right password, so read it as the Otter attempt. Explain what the stored salt is used for in the check. Afterwards change the attempt back to otter-kettle-lantern.
A hint
The check hashes the attempt with the stored salt and compares it with the stored hash.
Make scrypt cheap
In store.py change the cost value in the slow function from n=2**14 (that is 16384) to n=16 (the same as 2**4). Predict what the line about one scrypt guess costing over 1,000 SHA-256 guesses will say, then run python run.py slow. In a test run it printed False, though timings depend on your machine. Why is a cost this low a bad idea outside a demo?
A hint
Only the slow hash makes each guess cost more. The timing depends on your machine, so a result may differ.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
All users, passwords and the wordlist are made up; the scrypt cost is a demo value, not a recommendation.
Up next in How it works, by example
More in How it works, by example
- #274 How File Compression Works: Huffman Coding in 40 Lines
- #275 Bloom Filters Explained: Build One and Measure Errors
- #276 How Database Indexes Work: B-tree vs Full Scan in SQLite
- #277 How REST APIs Work: Build One with the Standard Library
- #278 Why Redis Is Fast: Build a Cache with Expiry and Eviction
- #280 How HTTPS Works: Watch a TLS 1.3 Handshake in Python
Related videos
- #90 Stop Leaking API Keys: Build a Secret Scanner in Python AI security and governance
- #282 5 Password Attacks and the Defence for Each How it works, by example
- #01 Local AI Agent in Python: No API Key (Ollama) AI agents and MCP
- #02 RAG From Scratch in Python: Chunk, Embed, Retrieve (Local) RAG and vector search
