Cybersecurity basics · Lesson 2 of 3
Cybersecurity basics · Lesson 2 of 3
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.
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).
All checks pass. Well done.
Next lesson: Spotting a misleading linkAn AI tutor that gives hints, not answers. It sees your code, its output and this lesson. It can be wrong.