Passwords, MFA and passkeys
About this lesson
Authentication is how a site checks that you are really you. Follow one sign-up from start to finish: the site saves a random salt and a slow, one-way hash of the password (eight lines of real Python), never the password itself, and signs you in by comparing hashes. See what a database leak gives away, what NIST's password rules ask for, how an authenticator code adds a second factor and how it can still be phished, and why a passkey, a key pair tied to one site, resists phishing.
Proving who you are
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).
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
Read the lesson step by step
Proving who you are
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).
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 faceSigning up with a password
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.
site shop.example user ana passphrase plum-ferry-1979 (15 characters) travels inside HTTPS (encrypted) saved the username, never the passwordA random salt
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.
salt = os.urandom(16) 16 random bytes (128 bits) example 1f9a2c7e5b3d8046a1c9e27f0b6d4a35 one per user same password, different salt, different hash NIST a salt of at least 32 bitsA slow, one-way hash
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.
hashlib.pbkdf2_hmac("sha256", pw, salt, 600_000) rounds 600,000 (OWASP for PBKDF2-HMAC-SHA256) hash 7e2781fcb0c52bfa31cc04f6f532be08 f5e336fd50e114f0d6db9449941a0f83 saved salt + hash; the password is forgottenSigning in: compare hashes
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.
typed = slow(what she typed, saved salt) saved 7e2781fcb0c52bfa31cc04f6… typed 7e2781fcb0c52bfa31cc04f6… print → True she is in compare_digest takes the same time either wayIf the database leaks
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.
stolen salt + hash for every user not stolen the passwords themselves each guess 600,000 rounds of SHA-256 each salt guessed separately weak spot short or common passwordsNIST rules for passwords
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.
NIST SP 800-63B-4 length 15+ alone, 8+ inside MFA allow at least 64 characters no forced symbols, routine changes block known breached or common passwords you a password manager: one per siteMFA: something you have
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.
MFA two or more different factors code 492 039 (example) TOTP 6 digits, a new code every 30 s from a secret shared at setup she reads it and types it on the laptop (the app itself sends nothing) result password + code: she is inCodes can still be phished
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.
look-alike sh0p.example (a zero, not an o) asks for password + code weakness a person can type a code anywhere CISA app codes: vulnerable to phishing any MFA is better than no MFAPasskeys: a key pair
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.
passkey a key pair for one site private stays on Ana's device public saved by shop.example public key checks signatures, cannot make them tied to shop.exampleSigning a challenge
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.
site → phone a fresh random challenge Ana unlocks: fingerprint or PIN phone signs it with the private key phone → site the signature site checks it with the public keyWhy passkeys resist phishing
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.
asking sh0p.example passkey only for shop.example result no match: nothing is signed leaked a public key is useless to an attacker CISA FIDO / WebAuthn: phishing-resistant
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A site stores each user's salt and a PBKDF2 hash of their password. What does an attacker get if they steal that database?
A hash is one-way, so the passwords are not in the database. The attacker can only guess and check, paying the full slow hash for every guess and for every salt. Short or common passwords can still be found, which is why length matters.
02Under NIST SP 800-63B-4, a password that is the only factor must be at least how long?
At least 15 characters when it is the only factor (8 when it is part of MFA). Sites should allow at least 64, and must not force symbols or routine changes.
03Why does the code compare hashes with hmac.compare_digest instead of ==?
Python's docs describe compare_digest as a comparison designed to reduce the risk of timing attacks: how long it takes does not reveal how much of the value matched.
04Ana typed her password and her 6-digit authenticator code into a look-alike site. Which sign-in method would have protected her?
A passkey is tied to the real site's name, so her device will not sign anything for the look-alike. Codes and passwords can be typed anywhere; CISA calls FIDO/WebAuthn (passkeys) phishing-resistant MFA.
05With passkeys, what does the website store?
The private key stays with the user and is never sent to the site. The public key can check signatures but cannot make them, so a leak of public keys signs nobody in. Fingerprints stay on the device and only unlock it.
Glossary
- Authentication and factors
- Authentication is proving you are who you say you are. A factor is one kind of proof: something you know (a password), something you have (a phone or security key) or something you are (a fingerprint).
- Salt
- Random bytes made for each user and saved next to their hash. A salt is not secret; it makes the same password give different hashes for different people, so each must be guessed separately.
- Password hash (PBKDF2)
- A one-way, deliberately slow scramble of the password and salt. The site saves the hash and compares hashes at sign-in. PBKDF2 with SHA-256 should use at least 600,000 rounds (OWASP); Argon2id is OWASP's first choice.
- Password manager
- An app that makes and remembers a long, different password for every site, so a leak at one site cannot be reused at another.
- Multi-factor authentication (MFA)
- Signing in with two or more different factors, such as a password plus a code from an authenticator app. A stolen password alone is then not enough.
- TOTP code
- A time-based one-time password (RFC 6238): an authenticator app turns a secret shared at setup and the current 30-second time step into a short code, usually 6 digits.
- Phishing
- Tricking someone into giving away a password or code, often with a fake email and a look-alike website.
- Passkey
- A key pair made for one website. The private key stays with the user, the site keeps only the public key, and the device signs a fresh challenge only for the site it belongs to, which makes passkeys phishing-resistant.
Notes and sources
- OWASP lists Argon2id as its first choice for storing passwords, then scrypt, bcrypt and PBKDF2. The figure uses PBKDF2 because it is in Python’s standard library; with SHA-256, OWASP recommends at least 600,000 rounds.
- 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.
- Authenticator codes follow RFC 6238 (TOTP): a code from a shared secret and the current 30-second time step. The code shown is an example. Codes sent by text message are weaker still: CISA warns SMS codes can be phished or stolen by SIM swap, and says to use them only as a temporary step toward stronger MFA.
- 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.
- Even with good hashing, sites should check new passwords against lists of breached ones, and change a password at once if there is evidence it leaked.
- This course is about defence. It explains how attacks work only at the level needed to stop them, and never how to attack a real system.
- NIST SP 800-63B-4: Digital Identity Guidelines, Authentication and Authenticator Management
- OWASP Password Storage Cheat Sheet
- Python docs: hashlib (pbkdf2_hmac)
- Python docs: hmac.compare_digest
- Python docs: os.urandom
- CISA fact sheet: Implementing Phishing-Resistant MFA (October 2022)
- CISA: More than a Password (multi-factor authentication)
- RFC 6238: TOTP, Time-Based One-Time Password Algorithm
- FIDO Alliance: Passkeys
- W3C: Web Authentication (WebAuthn) Level 3
Quiz Unlocks after the last step.