When a site stores your password, it should store a hash — a scrambled fingerprint, not the password itself. This lesson shows how hashes work and why weak ones fall to a dictionary attack in seconds, by building a small cracker with Python's hashlib. Use it only on hashes of your own test passwords.
What a hash is
A hash function turns any input into a fixed-length string, and can't be reversed. The same input always gives the same output.
import hashlib
h = hashlib.md5('password123'.encode()).hexdigest()
print(h)
Sites compare hashes, never the raw password — so a database leak exposes fingerprints, not plaintext. In theory.
Why a dictionary attack works
Because hashing is one-way but repeatable, you can hash a list of common passwords and look for a match. No reversing needed — just checking fast.
import hashlib
target = '482c811da5d5b4bc6d497ffa98491e38'
with open('wordlist.txt') as f:
for line in f:
guess = line.strip()
if hashlib.md5(guess.encode()).hexdigest() == target:
print(f'[+] Cracked: {guess}')
break
Point it at a list of common passwords and a weak one falls instantly.
| Hash type | Security today |
|---|---|
| MD5 | Broken — crackable in seconds |
| SHA-1 | Broken — avoid |
| SHA-256 | Strong as a hash, but still guessable if the password is weak |
| bcrypt / argon2 | Built for passwords: deliberately slow to crack |
Tip: The real lesson here is for defenders. Fast hashes like MD5 were never meant for passwords. Real systems should use
bcryptorargon2, which are deliberately slow and salted — a random value added before hashing so two identical passwords don't share a hash, which kills precomputed attacks.
What this teaches you
You just proved, in a few lines, why 'password123' is worthless and why a long, unique passphrase isn't. That's the real takeaway: understand the attack, and you'll build systems that resist it.