AI Launch Lab
Sign in FR Get updates

Cybersecurity basics · Lesson 2 of 3

How sites should store passwords

A site should never store your password itself. It stores a hash: the result of a one-way function. At sign-in, it hashes what you typed and compares the results. If the database leaks, the attacker gets hashes, not passwords.

Two more protections matter. A salt, a random value stored next to each hash, makes identical passwords produce different hashes, so one guess can’t crack everyone at once. And the hash should be slow on purpose, repeated many times, so each guess costs the attacker time.

Real systems use functions designed for this, such as Argon2, scrypt, bcrypt or PBKDF2, with settings far stronger than this exercise. Here we repeat SHA-256 a few times only to see the idea.

Your turn

Write hash_password(pw, salt, rounds=1000): start from (salt + pw).encode(), apply hashlib.sha256(...).digest() rounds times, and return the final result as hexadecimal (.hex()). Then write check(pw, salt, stored).

Loading Python (about 12 MB, once)…

    Checks

    • ○ one round is plain salted SHA-256
    • ○ the right password checks
    • ○ a typo is rejected
    • ○ different salts give different hashes

    Output

    ← Previous lesson All courses