UnixDroid

Lesson 9 of 12 · 8 min

Build a hash cracker with hashlib (dictionary attack)

When a site stores a password it should store a hash. Here's how hashes work, and why weak ones fall to a dictionary attack in seconds.

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 typeSecurity today
MD5Broken — crackable in seconds
SHA-1Broken — avoid
SHA-256Strong as a hash, but still guessable if the password is weak
bcrypt / argon2Built 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 bcrypt or argon2, 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.

Try it yourself
  1. Hash a word of your choice with MD5 and copy the result.
  2. Put that word in wordlist.txt and run the cracker against your hash.
  3. Swap md5 for sha256 in both places and confirm it still finds the match.
No Linux machine? Practise in the free online terminal

Quick check

What random value, added to a password before hashing, stops two identical passwords from producing the same hash?

Need a hint?

You'd add it to food, too.

Finished this lesson?

Done lessons keep your daily streak going 🔥

0 day streak

0 lessons done

best 0 days

Log in to keep your streak on every device →