Storing a password
Authentication means proving you are who you say you are before a site lets you in. Each proof is a factor. There are three kinds: something you know (a password), something you have (a phone) and something you are (a fingerprint).
Ana signs up at shop.example with a long passphrase. It travels inside HTTPS, so nobody on the way can read it. The site saves her username. It must never save the password itself.
First the site makes a salt: 16 random bytes from os.urandom, new for every user. It is saved next to the username. A salt is not secret. It makes two people with the same password get different hashes.
slow() mixes the password and salt through PBKDF2, 600,000 rounds of SHA-256. The result, the hash, is saved. It is one-way: nobody can turn a hash back into the password. Then the password is thrown away.
To sign in, Ana types her passphrase again. The site runs slow() with her saved salt and compares the two hashes with hmac.compare_digest. They match, so it prints True and she is in. A wrong guess gives a different hash.
When passwords leak
Suppose the database is stolen. The attacker gets salts and hashes, not passwords. To find one they must guess, paying 600,000 rounds per guess, separately for each salt. Short or common passwords still fall fast. Long, unusual ones hold.
NIST says: at least 15 characters if the password is the only factor, allow 64 or more, no forced symbols and no routine changes. Sites must block passwords known from breaches. A password manager makes a different one for every site.
MFA and passkeys
Multi-factor authentication adds a second proof. An authenticator app on Ana's phone shows a 6-digit code that changes every 30 seconds, made from a secret shared at setup. She types it in after her password. A stolen password alone is no longer enough.
A code has one weakness: Ana can be tricked into typing it into a look-alike site, sh0p.example, which passes it on within seconds. CISA rates app codes as vulnerable to phishing. Any MFA is still far better than none.
A passkey replaces the password. Ana's phone makes a key pair just for shop.example. The private key stays on her device. The site saves only the public key, which can check signatures but cannot make them.
To sign in, the site sends a fresh random challenge. Ana unlocks her phone with a fingerprint or PIN, and it signs the challenge with the private key. The site checks the signature with the public key. No secret crosses the network.
Ana's browser tells her phone which site is asking. The passkey is tied to shop.example, so sh0p.example gets no signature, however real it looks. A leaked public key signs nobody in. CISA calls this phishing-resistant MFA.
What happens
;; step 1/12 · factors authentication proving who you are factor one kind of proof know a password or PIN have a phone, a security key are a fingerprint, a face
The salt and hash shown are real outputs for one fixed example salt. os.urandom gives a new salt every run, so your hash will differ. Real sites also add rate limits and checks on sign-in. Many passkeys are synced between your own devices by your phone maker or password manager, encrypted. Either way the private key is never sent to the website. Unlocking with a fingerprint or PIN makes a passkey two factors in one step.