Showing raw Markdown: the renderer could not load.
## Why this matters
An employee gets an email that looks like it comes from the company's single sign-on page. She types her password. A minute later, the attacker logs in to her mailbox from another country and starts reading invoices. Nothing was "hacked" in the technical sense: no software bug, no exploit. The attacker simply proved to the system that they were her, using the one thing the system asked for.
Stolen or guessed credentials are one of the most common ways in to an organization. Three questions decide whether that story ends badly. How strong is the proof the system asks for? What does the server keep, so that a stolen database doesn't hand out passwords? And does a second step exist that a fake web page can't copy? This lesson answers all three.
> [!WARNING]
> This is a defensive lesson. The attacks are explained so that you can recognize and stop them. Only test authentication on systems you own or are explicitly authorized to test.
## Learning objectives
- Explain the authentication factors (something you know, have, are) and the difference between identification, authentication and authorization.
- Apply current password guidance (NIST SP 800-63B): length over complexity, blocklists of breached passwords, no forced periodic changes.
- Explain how a server stores passwords safely with a salt and a slow hash function.
- Explain how time-based one-time passwords (TOTP, RFC 6238) are generated and verified.
- Compare MFA methods (SMS, TOTP, push notifications, FIDO2 security keys and passkeys) by their resistance to phishing.
## Key concepts
### Identification, authentication and authorization
Think of entering an office building. You say "I'm Alice from accounting" (that's a claim). The guard checks your badge photo against your face (that's proof). The badge then opens the accounting floor but not the server room (that's what you're allowed to do). Three different steps, and systems mix them up all the time.
- **Identification** is claiming an identity: a username, an email address, an account number. Anyone can claim anything.
- **Authentication** (often shortened to "authn") is proving the claim is true. This is what passwords, codes and security keys are for.
- **Authorization** (authz) decides what the proven identity may do: read this file, delete that user. It happens after authentication and is covered in the identity and access management (IAM) module.
<figure class="diagram" style="--dg-w: 940px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 940 160" width="940" height="160" role="img" aria-labelledby="auth-id-authn-authz-title auth-id-authn-authz-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="auth-id-authn-authz-title">Identification, authentication and authorization in order</title>
<desc id="auth-id-authn-authz-desc">Three numbered steps joined by arrows. 1 Identification: who do you claim to be, for example the username alice. 2 Authentication: prove it, for example with a password and a code. 3 Authorization: what may you do, for example read a file but not delete it.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="939" height="159" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<rect class="dg-card" x="24" y="24" width="274.667" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="46" cy="48" r="12" fill="#0369a1"/><text x="46" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(258.667 36) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M10 9H8"/><path d="M16 13H8"/><path d="M16 17H8"/></g></g>
<text x="38" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Identification</text>
<text x="38" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Who do you claim to be?</text>
<text x="38" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Example: username alice</text>
<path class="dg-line" d="M304.667 80H326.667" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M318.667 74l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="332.667" y="24" width="274.667" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="354.667" cy="48" r="12" fill="#0369a1"/><text x="354.667" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">2</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(567.333 36) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></g></g>
<text x="346.667" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Authentication</text>
<text x="346.667" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Prove it.</text>
<text x="346.667" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Example: password + code</text>
<path class="dg-line" d="M613.333 80H635.333" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M627.333 74l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="641.333" y="24" width="274.667" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="663.333" cy="48" r="12" fill="#0369a1"/><text x="663.333" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">3</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(876 36) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<text x="655.333" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Authorization</text>
<text x="655.333" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">What may you do?</text>
<text x="655.333" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">Example: read, not delete</text>
</svg></figure>
*Figure 1. Identification claims, authentication proves, authorization decides what is allowed. Strong authentication is wasted if authorization gives everyone everything.*
The words **authentication** and **authorization** are not interchangeable. "Alice is logged in" says nothing about what Alice may do.
> [!CHECK]
> A user types `alice` and a password, and the system confirms the password is right. Which step has just happened?
>
> - [ ] Identification only, because the username was entered
> - [x] Authentication: the claim "I am alice" was checked with a secret
> - [ ] Authorization, because access was granted
> - [ ] Encryption of the account
>
> Typing `alice` is identification. Checking the password is authentication. What alice may then do is authorization, a separate decision.
### Authentication factors and MFA
A **factor** is a category of evidence. There are three classic ones:
| Factor | Examples | Typical weakness |
|---|---|---|
| **Something you know** | Password, passphrase, personal identification number (PIN) | Can be guessed, phished, reused or leaked |
| **Something you have** | Phone with an authenticator app, security key, smart card | Can be lost or stolen |
| **Something you are** | Fingerprint, face, iris | Can't be changed if the template leaks; often used only to unlock a device |
**Multi-factor authentication (MFA)** means using two or more *different* factors. The idea is that an attacker who gets one kind of evidence still lacks the other. A stolen password isn't enough if the attacker doesn't also hold your phone.
<figure class="diagram" style="--dg-w: 1040px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1040 272" width="1040" height="272" role="img" aria-labelledby="auth-factors-title auth-factors-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="auth-factors-title">The three authentication factors</title>
<desc id="auth-factors-desc">Three cards. Something you know: a password, passphrase or PIN, which can be guessed, phished or leaked. Something you have: a phone with an authenticator app or a security key, which can be lost or stolen. Something you are: a fingerprint, face or iris, which can't be changed if it leaks. Below, a red panel says a password plus a security question is not multi-factor authentication because both are something you know, and a green panel says a password plus a code from your phone is two factors, know and have, so it is MFA.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1039" height="271" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<rect class="dg-card" x="24" y="24" width="320" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><g class="dg-icon" transform="translate(40 40) scale(1.2500)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></g></g>
<g class="dg-l4"><text x="82" y="61" font-size="15" class="dg-ink" fill="#c2410c" font-weight="700">Something you know</text></g>
<text x="42" y="92" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Password, passphrase, PIN</text>
<text x="42" y="112" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Can be guessed, phished or leaked</text>
<rect class="dg-card" x="360" y="24" width="320" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(376 40) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="14" height="20" x="5" y="2" rx="2"/><path d="M12 18h.01"/></g></g>
<g class="dg-l1"><text x="418" y="61" font-size="15" class="dg-ink" fill="#0369a1" font-weight="700">Something you have</text></g>
<text x="378" y="92" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Phone with an app, security key</text>
<text x="378" y="112" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Can be lost or stolen</text>
<rect class="dg-card" x="696" y="24" width="320" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(712 40) scale(1.2500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="7" height="7" x="3" y="3" rx="1"/><rect width="7" height="7" x="14" y="3" rx="1"/><rect width="7" height="7" x="14" y="14" rx="1"/><rect width="7" height="7" x="3" y="14" rx="1"/></g></g>
<g class="dg-l2"><text x="754" y="61" font-size="15" class="dg-ink" fill="#15803d" font-weight="700">Something you are</text></g>
<text x="714" y="92" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Fingerprint, face, iris</text>
<text x="714" y="112" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Can't be changed if it leaks</text>
<g class="dg-warn"><rect class="dg-tint" x="24" y="156" width="488" height="92" rx="16" fill="#fbf1f1" stroke="#e3a4a4" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(40 169) scale(0.7500)" fill="none" stroke="#b91c1c" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"/><path d="M12 9v4"/><path d="M12 17h.01"/></g>
<text x="66" y="183" font-size="13" class="dg-ink" fill="#b91c1c" font-weight="700">Password + security question</text></g>
<g class="dg-warn"><text x="42" y="220" font-size="13" class="dg-ink" fill="#b91c1c" font-weight="600">Both are 'know': one factor, so NOT MFA</text></g>
<g class="dg-ok"><rect class="dg-tint" x="528" y="156" width="488" height="92" rx="16" fill="#f1f7f3" stroke="#a1ccb1" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(544 169) scale(0.7500)" fill="none" stroke="#15803d" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g>
<text x="570" y="183" font-size="13" class="dg-ink" fill="#15803d" font-weight="700">Password + code from your phone</text></g>
<g class="dg-ok"><text x="546" y="220" font-size="13" class="dg-ink" fill="#15803d" font-weight="600">Know + have: two factors, this is MFA</text></g>
</svg></figure>
*Figure 2. What counts is the number of different factors, not the number of steps.*
Two passwords are **not** MFA. A password plus a security question ("your mother's maiden name") is still two things you know. One phishing page can ask for both, and both can be found in a breach. This is sometimes called "two-step" verification, but it's one factor.
Biometrics need care. A fingerprint is not a secret (you leave it on every glass) and you can't rotate it. In practice, a biometric usually unlocks a key stored on your device, and the device then proves possession. That is "have" protected by "are", which is a good design.
> [!CHECK]
> Which login is multi-factor authentication?
>
> - [ ] A password and a security question
> - [ ] A long passphrase that has 40 characters
> - [x] A password and a six-digit code from an app on your phone
> - [ ] Two different passwords, one for the site and one for the email
>
> MFA needs two different factors. Password plus phone-app code is know plus have. The other options are all "something you know".
### Passwords: what current guidance says
For years, advice was "use eight characters with a capital, a number and a symbol, and change it every 90 days." The result was `Summer2024!`, then `Summer2025!`. People followed the rules and made passwords that were easy to guess.
The **NIST Special Publication (SP) 800-63B** (Digital Identity Guidelines: Authentication and Authenticator Management) replaced that advice. Its current revision, 800-63B-4, was published in 2025. What the verifier (the service that checks passwords) is asked to do:
- **Minimum length:** at least **15 characters** when the password is the only factor, and at least **8** when it is one factor of MFA.
- **Allow long passwords:** accept at least 64 characters, including spaces and any Unicode characters, so people can use a passphrase.
- **No composition rules:** don't force upper case, digits or symbols.
- **Check against a blocklist:** reject passwords that are common, expected or found in known breaches. Compare the whole password.
- **No periodic changes:** don't force a change every N days. Force one only when there is evidence of compromise.
- **Rate limiting:** limit consecutive failed attempts. The 800-63B-4 ceiling is 100 consecutive failures on an authenticator, and a service should be much stricter in practice, with growing delays.
- **Allow paste and password managers:** never block pasting into the password field.
> [!NOTE]
> The exact numbers come from the revision of SP 800-63B-4 published in 2025 (sections 3.1.1.2 and 3.2.2). Earlier revisions (800-63B-3) used a minimum of 8 characters for all passwords. Standards get revised, so check the current text before you cite a number in a policy.
Why length beats complexity: each added character multiplies the work of an attacker who must guess. A passphrase such as `correct horse battery staple` is long and easy to remember. `P@ssw0rd!` is short, matches a pattern, and is on every cracking list. Complexity rules also push people to predictable tricks (a capital first letter, `1` or `!` at the end).
Nobody can remember a different 15-character password for 100 sites. A **password manager** solves that: it generates a unique random password for each site and fills it in. Because it fills only on the right web address, it also helps against phishing. You remember one strong passphrase for the manager and turn on MFA for it.
> [!CHECK]
> Which password policy matches NIST SP 800-63B-4?
>
> - [ ] At least 8 characters with upper case, digit and symbol, changed every 60 days
> - [x] Long passwords allowed, no composition rules, a blocklist of breached passwords, and no scheduled changes
> - [ ] Passwords must never be pasted, to stop automated attacks
> - [ ] Security questions as a second layer
>
> NIST favors length and blocklists over complexity rules and forced rotation, which push people to weak, predictable passwords.
### How servers store passwords
A service must be able to check your password, but it should never be able to tell it to anyone. The wrong way is to store the password itself. The slightly-less-wrong way is to store a plain hash such as SHA-256. A **hash function** turns any input into a fixed-size fingerprint and can't be reversed. But a plain hash fails in two ways:
1. **Same password, same hash.** If two users choose `sunshine1`, their stored values are identical. An attacker who steals the table sees who shares passwords, and can compute a hash for each common password once and look it up in the whole table (a precomputed or "rainbow" table).
2. **Fast hashes are fast to guess.** SHA-256 is designed to be quick. A single graphics card can test billions of guesses per second.
The fix has two ingredients:
- A **salt**: a random value, unique for each user, stored next to the hash. It's not secret. It makes the same password hash differently for every user, so no precomputed table works and each account must be attacked separately.
- A **slow, memory-hard password hash** (also called a key derivation function): an algorithm designed to cost real time and memory on each guess, such as **Argon2id** (RFC 9106), **scrypt** (RFC 7914), **bcrypt** or **PBKDF2** (RFC 8018). One login takes a fraction of a second, which nobody notices; an attacker trying billions of guesses is slowed to a crawl.
<figure class="diagram" style="--dg-w: 1040px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1040 332" width="1040" height="332" role="img" aria-labelledby="auth-password-storage-title auth-password-storage-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="auth-password-storage-title">How a server stores and checks a password</title>
<desc id="auth-password-storage-desc">Two rows of four numbered steps. Sign-up: the password you type, add a salt of 16 random bytes that is new for every user, run a slow hash such as scrypt, Argon2id, bcrypt or PBKDF2, and store the salt and the hash but never the password. Login: the password you type, read the salt from the user's stored record, run the same slow hash with the same settings, and compare the result with the stored hash in constant time.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1039" height="331" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<text x="24" y="36" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="700" letter-spacing="1.2">SIGN-UP: what the server saves</text>
<rect class="dg-card" x="24" y="48" width="222.5" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><circle class="dg-solid" cx="46" cy="72" r="12" fill="#c2410c"/><text x="46" y="76.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-l4"><g class="dg-icon" transform="translate(206.5 60) scale(1.0833)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></g></g>
<text x="38" y="110" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Password</text>
<text x="38" y="130" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">typed by you</text>
<text x="38" y="147" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">sunshine1</text>
<path class="dg-line" d="M252.5 104H274.5" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M266.5 98l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="280.5" y="48" width="222.5" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><circle class="dg-solid" cx="302.5" cy="72" r="12" fill="#c2410c"/><text x="302.5" y="76.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">2</text></g>
<g class="dg-l4"><g class="dg-icon" transform="translate(463 60) scale(1.0833)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="18" height="18" x="3" y="3" rx="2"/><path d="M8 8h.01M16 8h.01M12 12h.01M8 16h.01M16 16h.01"/></g></g>
<text x="294.5" y="110" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Add a salt</text>
<text x="294.5" y="130" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">16 random bytes,</text>
<text x="294.5" y="147" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">new for every user</text>
<path class="dg-line" d="M509 104H531" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M523 98l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="537" y="48" width="222.5" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><circle class="dg-solid" cx="559" cy="72" r="12" fill="#c2410c"/><text x="559" y="76.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">3</text></g>
<g class="dg-l4"><g class="dg-icon" transform="translate(719.5 60) scale(1.0833)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M5 22h14"/><path d="M5 2h14"/><path d="M17 22v-4.17a2 2 0 0 0-.59-1.42L12 12l-4.41 4.41A2 2 0 0 0 7 17.83V22"/><path d="M7 2v4.17a2 2 0 0 0 .59 1.42L12 12l4.41-4.41A2 2 0 0 0 17 6.17V2"/></g></g>
<text x="551" y="110" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Slow hash</text>
<text x="551" y="130" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">scrypt, Argon2id,</text>
<text x="551" y="147" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">bcrypt or PBKDF2</text>
<path class="dg-line" d="M765.5 104H787.5" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M779.5 98l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="793.5" y="48" width="222.5" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><circle class="dg-solid" cx="815.5" cy="72" r="12" fill="#c2410c"/><text x="815.5" y="76.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">4</text></g>
<g class="dg-l4"><g class="dg-icon" transform="translate(976 60) scale(1.0833)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M3 5V19A9 3 0 0 0 21 19V5"/><path d="M3 12A9 3 0 0 0 21 12"/></g></g>
<text x="807.5" y="110" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Store</text>
<text x="807.5" y="130" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">salt + hash,</text>
<text x="807.5" y="147" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">never the password</text>
<text x="24" y="184" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="700" letter-spacing="1.2">LOGIN: what the server checks</text>
<rect class="dg-card" x="24" y="196" width="222.5" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="46" cy="220" r="12" fill="#0369a1"/><text x="46" y="224.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(206.5 208) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></g></g>
<text x="38" y="258" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Password</text>
<text x="38" y="278" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">typed by you</text>
<text x="38" y="295" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">sunshine1</text>
<path class="dg-line" d="M252.5 252H274.5" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M266.5 246l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="280.5" y="196" width="222.5" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="302.5" cy="220" r="12" fill="#0369a1"/><text x="302.5" y="224.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">2</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(463 208) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><ellipse cx="12" cy="5" rx="9" ry="3"/><path d="M3 5V19A9 3 0 0 0 21 19V5"/><path d="M3 12A9 3 0 0 0 21 12"/></g></g>
<text x="294.5" y="258" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Read the salt</text>
<text x="294.5" y="278" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">from this user's</text>
<text x="294.5" y="295" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">stored record</text>
<path class="dg-line" d="M509 252H531" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M523 246l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="537" y="196" width="222.5" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="559" cy="220" r="12" fill="#0369a1"/><text x="559" y="224.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">3</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(719.5 208) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M5 22h14"/><path d="M5 2h14"/><path d="M17 22v-4.17a2 2 0 0 0-.59-1.42L12 12l-4.41 4.41A2 2 0 0 0 7 17.83V22"/><path d="M7 2v4.17a2 2 0 0 0 .59 1.42L12 12l4.41-4.41A2 2 0 0 0 17 6.17V2"/></g></g>
<text x="551" y="258" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Same slow hash</text>
<text x="551" y="278" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">same algorithm,</text>
<text x="551" y="295" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">same settings</text>
<path class="dg-line" d="M765.5 252H787.5" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M779.5 246l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="793.5" y="196" width="222.5" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><circle class="dg-solid" cx="815.5" cy="220" r="12" fill="#0369a1"/><text x="815.5" y="224.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">4</text></g>
<g class="dg-l1"><g class="dg-icon" transform="translate(976 208) scale(1.0833)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></g></g>
<text x="807.5" y="258" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Compare</text>
<text x="807.5" y="278" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">with the stored hash,</text>
<text x="807.5" y="295" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">in constant time</text>
</svg></figure>
*Figure 3. The server never stores the password. At login it repeats the computation and compares the results.*
Comparing the results should take the same time whether the first byte differs or the last one does; a normal `==` can leak timing information. Use a constant-time comparison, such as `hmac.compare_digest` in Python.
You'll try all of this in the lab.
> [!NOTE]
> Work factors (iterations, memory) must be raised as hardware gets faster. The OWASP Password Storage Cheat Sheet lists current minimum settings; check it rather than copying numbers from a lesson. Argon2id is the usual first choice for new systems; bcrypt has a 72-byte input limit.
> [!CHECK]
> Two users of a site both chose the password `sunshine1`. The site stores a random salt and a scrypt hash for each user. What does an attacker with a copy of the database see?
>
> - [ ] The same stored value twice, so one cracked password opens both
> - [x] Two different stored values, so each account must be attacked separately
> - [ ] The two passwords in plain text, because salts can be removed
> - [ ] Nothing: the salt is secret and encrypts the database
>
> The salt differs per user, so identical passwords produce different hashes. The salt itself isn't secret; its job is to defeat precomputed tables and shared work.
### Attacks on passwords
Knowing how the attacks work tells you which defenses matter. Everything below describes how to defend, not how to attack a real service.
| Attack | How it works | What helps |
|---|---|---|
| **Brute force** | Try many passwords against one account, or all possible passwords against a stolen hash | Long passwords, slow hashes, rate limiting and lockout delays |
| **Password spraying** | Try one or a few very common passwords (such as `Winter2025!`) against many accounts, to stay under per-account lockouts | Blocklist of common passwords, MFA, alerts on one source failing across many accounts |
| **Credential stuffing** | Take email and password pairs leaked from one site and try them on others, betting on reuse | Unique passwords (password manager), breach checks, MFA |
| **Phishing** | A fake page collects the password (and maybe the code) | Phishing-resistant MFA, password manager autofill, training |
| **Offline cracking** | After stealing a database, guess against the hashes on the attacker's own hardware | Salt and slow hash; no one can rate-limit an attacker offline |
Online attacks can be rate limited by the server. Offline attacks can't, which is why the storage method matters so much.
> [!CHECK]
> An attacker tries `Winter2025!` against 5,000 employee accounts, one attempt each. What is this attack?
>
> - [ ] Credential stuffing, because the password is reused from a leak
> - [ ] Offline cracking of a stolen database
> - [x] Password spraying: one common password against many accounts, below per-account lockout limits
> - [ ] A man-in-the-middle attack
>
> Spraying spreads attempts across accounts so that no single account sees enough failures to lock. Credential stuffing uses real leaked pairs for each account.
### One-time passwords: HOTP and TOTP
A password you type is a shared secret that an attacker can steal and replay. A **one-time password (OTP)** changes every time, so a stolen code is useless later. Authenticator apps use two standards built on the same idea.
**HOTP** (HMAC-based One-Time Password, RFC 4226) combines a secret key and a counter. Both sides keep the counter and add 1 after each use.
**TOTP** (Time-based One-Time Password, RFC 6238) replaces the counter with the time. No need to stay in sync by pressing a button; the clock does it. Both sides share a secret key once (when you scan the Quick Response (QR) code) and then compute the same code independently.
The calculation, shown in Figure 4:
1. **Time step.** `T = floor(Unix time / 30)`. Unix time counts seconds since 1 January 1970 (in Coordinated Universal Time, UTC). Dividing by 30 gives a number that stays the same for 30 seconds.
2. **HMAC.** Compute `HMAC-SHA1(secret, T)`, with `T` written as an 8-byte big-endian number. HMAC (Hash-based Message Authentication Code) is a keyed hash: without the secret you can't produce or predict the output. It gives 20 bytes. (RFC 6238 also allows SHA-256 and SHA-512; most apps use SHA-1, which is safe in HMAC even though SHA-1 alone is broken for collisions.)
3. **Dynamic truncation.** The low 4 bits of the last byte give an offset (0 to 15). Take the 4 bytes starting there and clear the top bit, giving a 31-bit number.
4. **Digits.** Compute that number modulo 10^6 and pad with zeros to 6 digits. That's the code.
<figure class="diagram" style="--dg-w: 1040px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 1040 224" width="1040" height="224" role="img" aria-labelledby="auth-totp-title auth-totp-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="auth-totp-title">How a TOTP code is generated</title>
<desc id="auth-totp-desc">Five numbered steps. 1 Shared secret, scanned once from the QR code. 2 Time step: T is the Unix time divided by 30, rounded down. 3 HMAC-SHA1 with the secret as key and T as an 8-byte message. 4 Truncate: the last 4 bits pick 4 bytes, of which 31 bits are kept. 5 Six digits: that number modulo 10 to the power 6, padded with zeros. A note says the server repeats the same steps with its copy of the secret and accepts the code for T and usually also T minus 1 and T plus 1 to tolerate clock drift.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="1039" height="223" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<rect class="dg-card" x="24" y="24" width="174.4" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><circle class="dg-solid" cx="46" cy="48" r="12" fill="#b45309"/><text x="46" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-l3"><g class="dg-icon" transform="translate(158.4 36) scale(1.0833)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="7.5" cy="15.5" r="5.5"/><path d="m21 2-9.6 9.6"/><path d="m15.5 7.5 3 3L22 7l-3-3"/></g></g>
<text x="38" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Shared secret</text>
<text x="38" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">scanned once from</text>
<text x="38" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">the QR code</text>
<path class="dg-line" d="M204.4 80H222.4" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M214.4 74l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="228.4" y="24" width="174.4" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><circle class="dg-solid" cx="250.4" cy="48" r="12" fill="#b45309"/><text x="250.4" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">2</text></g>
<g class="dg-l3"><g class="dg-icon" transform="translate(362.8 36) scale(1.0833)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 6v6l4 2"/></g></g>
<text x="242.4" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Time step</text>
<text x="242.4" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">T = Unix time</text>
<text x="242.4" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">divided by 30, floor</text>
<path class="dg-line" d="M408.8 80H426.8" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M418.8 74l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="432.8" y="24" width="174.4" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><circle class="dg-solid" cx="454.8" cy="48" r="12" fill="#b45309"/><text x="454.8" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">3</text></g>
<g class="dg-l3"><g class="dg-icon" transform="translate(567.2 36) scale(1.0833)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M4 9h16"/><path d="M4 15h16"/><path d="M10 3 8 21"/><path d="m16 3-2 18"/></g></g>
<text x="446.8" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">HMAC-SHA1</text>
<text x="446.8" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">key = secret,</text>
<text x="446.8" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">message = T (8 bytes)</text>
<path class="dg-line" d="M613.2 80H631.2" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M623.2 74l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="637.2" y="24" width="174.4" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><circle class="dg-solid" cx="659.2" cy="48" r="12" fill="#b45309"/><text x="659.2" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">4</text></g>
<g class="dg-l3"><g class="dg-icon" transform="translate(771.6 36) scale(1.0833)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="6" cy="6" r="3"/><circle cx="6" cy="18" r="3"/><path d="M20 4 8.12 15.88"/><path d="M14.47 14.48 20 20"/><path d="M8.12 8.12 12 12"/></g></g>
<text x="651.2" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Truncate</text>
<text x="651.2" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">last 4 bits pick 4</text>
<text x="651.2" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">bytes, keep 31 bits</text>
<path class="dg-line" d="M817.6 80H835.6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M827.6 74l8 6-8 6" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<rect class="dg-card" x="841.6" y="24" width="174.4" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l3"><circle class="dg-solid" cx="863.6" cy="48" r="12" fill="#b45309"/><text x="863.6" y="52.5" font-size="12.5" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">5</text></g>
<g class="dg-l3"><g class="dg-icon" transform="translate(976 36) scale(1.0833)" fill="none" stroke="#b45309" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="18" height="18" x="3" y="3" rx="2"/><path d="M8 8h.01M16 8h.01M12 12h.01M8 16h.01M16 16h.01"/></g></g>
<text x="855.6" y="86" font-size="14" class="dg-text" fill="#17171c" font-weight="700">6 digits</text>
<text x="855.6" y="106" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">number mod 10^6,</text>
<text x="855.6" y="123" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">padded with zeros</text>
<rect class="dg-panel" x="24" y="156" width="992" height="44" rx="12" fill="#f0f0ec"/>
<text x="520" y="183" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">The server repeats the same steps with its copy of the secret and accepts the code for T, and usually also T-1 and T+1 (clock drift)</text>
</svg></figure>
*Figure 4. The phone and the server never send the secret to each other again; both just compute the same function of the secret and the clock.*
**Verification** is the same calculation on the server. Because phone clocks drift and typing takes seconds, the server accepts the codes for the current step and usually one step before and after (RFC 6238 recommends allowing at most a small window, one step either side). A code should also be accepted only once, otherwise an attacker who sees it can replay it within the window.
Notice what a TOTP code does **not** do: it isn't tied to any website. Whoever you type it into can use it. That is the weakness the next section exploits.
> [!CHECK]
> Why do the phone and the server produce the same 6-digit code without talking to each other?
>
> - [ ] The server sends each code to the phone through the Internet
> - [ ] The phone sends a new secret to the server every 30 seconds
> - [x] Both hold the same secret and compute an HMAC of the same time step
> - [ ] The code is a random number that both sides guess
>
> TOTP is deterministic: the same secret and the same time step always give the same code. Only the secret has to be shared, once.
### Comparing MFA methods
Not all second factors are equally strong. The key question is: **can a fake website relay what you gave it to the real one?** This is **phishing resistance**.
**SMS codes.** (Short Message Service.) The server texts a code. Convenient, nothing to install, but weak: an attacker can persuade the carrier to move your number to their SIM card (**SIM swap**, where SIM is the subscriber identity module card in your phone), exploit weaknesses in the telephone network, or simply phish the code. NIST treats SMS as a "restricted" authenticator and recommends alternatives where possible. Better than no MFA, worse than the rest.
**TOTP apps.** No network involved, so no SIM swap or interception. But the code is typed by you, so a phishing page can ask for it and pass it to the real site within its 30 seconds. See Figure 5.
**Push notifications.** The app asks "Approve this sign-in?" and you tap. Easy for users, but exposed to **push fatigue** (also called MFA bombing): the attacker, who has your password, triggers prompt after prompt, hoping you tap "Approve" to make it stop, or at 3 a.m. by mistake. Number matching (you must type the number shown on the login screen into the app) and showing the location make it harder, but a real-time relay can still work.
<figure class="diagram" style="--dg-w: 940px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 940 339" width="940" height="339" role="img" aria-labelledby="auth-phishing-relay-title auth-phishing-relay-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="auth-phishing-relay-title">A phishing site relaying a one-time code in real time</title>
<desc id="auth-phishing-relay-desc">Three lifelines: you, a look-alike site bank.example.net and the real site bank.example.com. You type your password and the code 492817 into the look-alike site. It relays both at once to the real site. The real site accepts the login and sends a session cookie. A note says the attacker now holds your session. The code worked because it is not tied to the site it was typed into.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="939" height="338" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<path class="dg-line" d="M150 98V323" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<path class="dg-line" d="M470 98V323" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<path class="dg-line" d="M790 98V323" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<rect class="dg-card" x="45" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-icon-neutral" transform="translate(59 38) scale(1.1667)" fill="none" stroke="#17171c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 16V7a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v9m16 0H4m16 0 1.28 2.55a1 1 0 0 1-.9 1.45H3.62a1 1 0 0 1-.9-1.45L4 16"/></g>
<text x="97" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">You</text>
<text x="97" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">browser</text>
<rect class="dg-card" x="365" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-warn"><g class="dg-icon" transform="translate(379 38) scale(1.1667)" fill="none" stroke="#b91c1c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/></g></g>
<text x="417" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Look-alike site</text>
<text x="417" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">bank.example.net</text>
<rect class="dg-card" x="685" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(699 38) scale(1.1667)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="2" rx="2"/><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6 6h.01"/><path d="M6 18h.01"/></g></g>
<text x="737" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Real site</text>
<text x="737" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">bank.example.com</text>
<rect class="dg-bg-plain" x="214" y="115" width="192" height="17" fill="#ffffff"/>
<text x="310" y="128" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">Password and code 492817</text>
<g class="dg-l4"><circle class="dg-solid" cx="202" cy="124" r="11" fill="#c2410c"/><text x="202" y="128.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-l4"><path class="dg-stroke" d="M154 142H464" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><path class="dg-stroke" d="M456 136l8 6-8 6" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<rect class="dg-bg-plain" x="552.75" y="167" width="154.5" height="17" fill="#ffffff"/>
<text x="630" y="180" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">Relays both at once</text>
<g class="dg-warn"><circle class="dg-solid" cx="540.75" cy="176" r="11" fill="#b91c1c"/><text x="540.75" y="180.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">2</text></g>
<g class="dg-warn"><path class="dg-stroke" d="M474 194H784" stroke="#b91c1c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-warn"><path class="dg-stroke" d="M776 188l8 6-8 6" stroke="#b91c1c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<rect class="dg-bg-plain" x="511.5" y="219" width="237" height="17" fill="#ffffff"/>
<text x="630" y="232" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">Login accepted: session cookie</text>
<g class="dg-l2"><circle class="dg-solid" cx="499.5" cy="228" r="11" fill="#15803d"/><text x="499.5" y="232.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">3</text></g>
<g class="dg-l2"><path class="dg-stroke" d="M786 246H476" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-l2"><path class="dg-stroke" d="M484 240l-8 6 8 6" stroke="#15803d" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-warn"><rect class="dg-tint" x="359.5" y="268" width="221" height="31" rx="8" fill="#f8e8e8" stroke="#e09999" stroke-width="1.2"/>
<text x="470" y="285" font-size="12.5" class="dg-ink" fill="#b91c1c" font-weight="600" text-anchor="middle">Attacker holds your session</text>
</g>
</svg></figure>
*Figure 5. A one-time code that you type can be relayed within seconds. It was valid; it just wasn't bound to the site.*
**FIDO2 security keys and passkeys.** FIDO2 is the umbrella name for the **Web Authentication (WebAuthn)** standard of the W3C, which browsers and sites use, and the **Client to Authenticator Protocol (CTAP2)**, which the browser uses to talk to a key or phone. It uses public-key cryptography:
1. When you register, the authenticator creates a key pair **for that site**. The public key goes to the site; the private key never leaves the authenticator.
2. When you sign in, the site sends a random **challenge**. The authenticator signs it, after you touch the key or use a fingerprint or PIN.
3. The site verifies the signature with the stored public key.
The signed data includes the **origin** (the web address), which the *browser* supplies, not the page. On a look-alike site, the origin is wrong: the key has no credential for it and does nothing; and a signature made for the fake origin is rejected by the real site. There is no code for you to type and nothing to relay. This is **origin binding**, and it is why WebAuthn is called phishing-resistant.
<figure class="diagram" style="--dg-w: 940px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 940 540" width="940" height="540" role="img" aria-labelledby="auth-webauthn-title auth-webauthn-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="auth-webauthn-title">WebAuthn: the origin is part of what the key signs</title>
<desc id="auth-webauthn-desc">Three lifelines: a security key or passkey, the browser and the real site bank.example.com. On the look-alike site bank.example.net the browser passes the challenge and the origin bank.example.net to the key. The key has no credential for that origin and stays silent, so there is nothing to relay. On the real site, the browser sends the challenge and the origin bank.example.com, the key returns a signature, and the real site verifies it with the stored public key.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="939" height="539" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<path class="dg-line" d="M150 98V524" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<path class="dg-line" d="M470 98V524" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<path class="dg-line" d="M790 98V524" stroke="#cfcfc8" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/>
<rect class="dg-card" x="45" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(59 38) scale(1.1667)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="7.5" cy="15.5" r="5.5"/><path d="m21 2-9.6 9.6"/><path d="m15.5 7.5 3 3L22 7l-3-3"/></g></g>
<text x="97" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Security key</text>
<text x="97" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">or passkey</text>
<rect class="dg-card" x="365" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-icon-neutral" transform="translate(379 38) scale(1.1667)" fill="none" stroke="#17171c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/></g>
<text x="417" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Browser</text>
<text x="417" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500">knows the real origin</text>
<rect class="dg-card" x="685" y="24" width="210" height="74" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(699 38) scale(1.1667)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="2" rx="2"/><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6 6h.01"/><path d="M6 18h.01"/></g></g>
<text x="737" y="56" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Real site</text>
<text x="737" y="76" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">bank.example.com</text>
<rect class="dg-bg-plain" x="235.375" y="115" width="149.25" height="17" fill="#ffffff"/>
<text x="310" y="128" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">challenge + origin</text>
<rect class="dg-bg-plain" x="208.688" y="132" width="202.625" height="17" fill="#ffffff"/>
<text x="310" y="145" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">origin = bank.example.net</text>
<g class="dg-warn"><circle class="dg-solid" cx="223.375" cy="124" r="11" fill="#b91c1c"/><text x="223.375" y="128.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-warn"><path class="dg-stroke" d="M466 159H156" stroke="#b91c1c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-warn"><path class="dg-stroke" d="M164 153l-8 6 8 6" stroke="#b91c1c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-ok"><rect class="dg-tint" x="110" y="181" width="400" height="48" rx="8" fill="#e8f2ec" stroke="#96c6a8" stroke-width="1.2"/>
<text x="310" y="198" font-size="12.5" class="dg-ink" fill="#15803d" font-weight="600" text-anchor="middle">No credential for this origin:</text>
<text x="310" y="215" font-size="12.5" class="dg-ink" fill="#15803d" font-weight="600" text-anchor="middle">the key stays silent</text>
</g>
<g class="dg-l2"><rect class="dg-tint" x="363" y="247" width="214" height="31" rx="8" fill="#e8f2ec" stroke="#96c6a8" stroke-width="1.2"/>
<text x="470" y="264" font-size="12.5" class="dg-ink" fill="#15803d" font-weight="600" text-anchor="middle">Same key, on the real site</text>
</g>
<rect class="dg-bg-plain" x="235.375" y="299" width="149.25" height="17" fill="#ffffff"/>
<text x="310" y="312" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">challenge + origin</text>
<rect class="dg-bg-plain" x="208.688" y="316" width="202.625" height="17" fill="#ffffff"/>
<text x="310" y="329" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">origin = bank.example.com</text>
<g class="dg-l1"><circle class="dg-solid" cx="223.375" cy="308" r="11" fill="#0369a1"/><text x="223.375" y="312.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">2</text></g>
<g class="dg-l1"><path class="dg-stroke" d="M466 343H156" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M164 337l-8 6 8 6" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<rect class="dg-bg-plain" x="187.75" y="368" width="244.5" height="17" fill="#ffffff"/>
<text x="310" y="381" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">Signature of challenge + origin</text>
<g class="dg-l1"><circle class="dg-solid" cx="175.75" cy="377" r="11" fill="#0369a1"/><text x="175.75" y="381.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">3</text></g>
<g class="dg-l1"><path class="dg-stroke" d="M154 395H464" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M456 389l8 6-8 6" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<rect class="dg-bg-plain" x="507.75" y="420" width="244.5" height="17" fill="#ffffff"/>
<text x="630" y="433" font-size="12.5" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">Signature of challenge + origin</text>
<g class="dg-l1"><circle class="dg-solid" cx="495.75" cy="429" r="11" fill="#0369a1"/><text x="495.75" y="433.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">4</text></g>
<g class="dg-l1"><path class="dg-stroke" d="M474 447H784" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M776 441l8 6-8 6" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-ok"><rect class="dg-tint" x="649" y="469" width="277" height="31" rx="8" fill="#e8f2ec" stroke="#96c6a8" stroke-width="1.2"/>
<text x="787.5" y="486" font-size="12.5" class="dg-ink" fill="#15803d" font-weight="600" text-anchor="middle">Verified with the stored public key</text>
</g>
</svg></figure>
*Figure 6. The origin is part of what is signed, so a signature can't be reused on another site.*
A **passkey** is a WebAuthn credential designed to replace the password. It can be stored on a hardware security key, or synced between your devices by a platform provider, so losing one phone doesn't lock you out. Synced passkeys trade a little of the strictness of a hardware key for much easier recovery and wider adoption.
| Method | Stops password theft alone | Resists real-time phishing | Main weakness |
|---|---|---|---|
| SMS code | Yes | No | SIM swap, interception, phishing |
| TOTP app | Yes | No | Code can be relayed |
| Push (with number matching) | Yes | Partly | Fatigue, user tapping Approve |
| FIDO2 key or passkey | Yes | **Yes** | Needs a supported device; recovery process |
> [!CHECK]
> A phishing site shows a perfect copy of your bank's login page and relays your password and TOTP code to the real bank in real time. Which second factor would still stop the attack?
>
> - [ ] A six-digit code sent by SMS
> - [ ] A six-digit code from an authenticator app
> - [x] A FIDO2 security key or passkey
> - [ ] A longer password
>
> The key signs the origin it sees. For the look-alike site there is no credential, so there's nothing to relay. Codes of any kind can be typed into the wrong site.
## How it works
Putting the pieces together, here is a typical modern login, from sign-up to a phishing attempt:
1. **Sign-up.** You choose a long passphrase. The server checks it against a blocklist, generates a random salt, computes a slow hash and stores the salt and hash.
2. **Enrolment.** You add a second factor: scanning a QR code gives the app the TOTP secret, or you register a passkey (a key pair is created; the server gets the public key). The server stores the TOTP secret or the public key. (The TOTP secret must be stored so the server can compute codes; protect it like a key, for example with encryption.)
3. **Login.** You send the username (identification). The server reads your salt, repeats the slow hash and compares in constant time. This is the "know" factor. If it matches, the server asks for the second factor: it recomputes the TOTP for the current time step (and one either side), or it sends a WebAuthn challenge and verifies the signature.
4. **Session.** After success the server creates a session, usually a random cookie. The server doesn't ask again for each request. Anyone who steals this cookie can act as you until it expires, which is why attackers like real-time relays: they get the cookie.
5. **Authorization.** On every request, the server checks what this identity may do.
> [!CHECK]
> After a successful login with MFA, the server sets a session cookie. Why is a stolen cookie dangerous even though MFA was used?
>
> - [ ] The cookie contains the user's password
> - [x] The server treats the cookie as proof that authentication already happened, so whoever holds it acts as the user until it expires
> - [ ] MFA codes are stored inside the cookie
> - [ ] Cookies disable authorization checks
>
> MFA protects the moment of login. After that, the session cookie is the proof. Short lifetimes, binding sessions to devices and re-authentication for sensitive actions reduce the risk.
## Worked example
Let's compute a real TOTP value by hand, using the test vector in RFC 6238, Appendix B. The shared secret is the ASCII string `12345678901234567890` and the time is 59 seconds after the Unix epoch. (The RFC's test vectors use 8 digits; apps use 6.)
1. **Time step:** `T = floor(59 / 30) = 1`. As 8 bytes: `00 00 00 00 00 00 00 01`.
2. **HMAC-SHA1** of those 8 bytes with the secret gives 20 bytes:
```text
75a48a19d4cbe100644e8ac1397eea747a2d33ab
```
3. **Offset:** the last byte is `ab` = 171; its low 4 bits (`b`) = 11. Take the 4 bytes starting at index 11: `c1 39 7e ea`.
4. **31-bit number:** clear the top bit: `0x41397eea` = 1,094,287,082.
5. **Digits:** modulo 10^8 gives `94287082` (the RFC's expected value). Modulo 10^6 gives `287082`, the 6-digit code an app would show.
A server that receives `287082` at time 59 computes the same value and accepts. You'll write this calculation in Python in the lab and check it against the RFC.
## Common pitfalls
- **Two passwords called "two-factor"** — a password plus a security question is one factor. Count factor types, not steps.
- **Forced complexity and rotation** — they make passwords weaker in practice. Follow SP 800-63B: length, blocklist, change only on compromise.
- **A plain or fast hash for passwords** — SHA-256 or MD5 alone is not password storage. Use a salt and Argon2id, scrypt, bcrypt or PBKDF2.
- **Treating SMS as strong** — it's better than nothing, but it's the first method to replace for admins and high-value accounts.
- **Unlimited push prompts** — enable number matching and limit the number of prompts, or you invite push fatigue.
- **Weak account recovery** — see the next section.
- **Reusing passwords** — the root of credential stuffing. A password manager removes the need to reuse.
## Security considerations
**Account recovery is the weak spot.** If "forgot password" can bypass your strong login, attackers use it instead. Recovery by email link is only as strong as the mailbox; recovery by SMS inherits SIM swap; security questions ("first pet") are guessable or public; and a help desk can be talked into resetting an account by someone pretending to be the user. Defend by: giving users backup methods set up in advance (a second security key, one-time recovery codes stored offline), verifying identity carefully for help-desk resets, notifying the user on every reset and every new device, and applying MFA to the recovery path too.
**Single sign-on (SSO)** lets users sign in once at an identity provider (using protocols such as SAML or OpenID Connect) and reach many applications. It reduces password sprawl and gives you one place to enforce strong MFA, but that identity provider becomes the key to everything: protect it with phishing-resistant MFA and monitor it closely.
Other practices:
- Offer MFA to everyone and **require** it for administrators and remote access.
- Prefer phishing-resistant methods (passkeys, security keys) where you can; keep a fallback that isn't weaker than the rest.
- Rate limit logins and alert on many failures, on one source failing across many accounts (spraying) and on logins from impossible locations.
- Check new passwords against breached-password lists.
- Never log passwords, codes or secrets, not even failed ones.
- Store TOTP secrets encrypted and treat the database that holds them as sensitive.
## Summary
- Identification claims an identity, authentication proves it, authorization decides what it may do.
- MFA means two different factors (know, have, are). Two passwords are one factor.
- NIST SP 800-63B favors long passwords, blocklists and no forced rotation over complexity rules.
- Servers store a per-user salt and a slow hash (Argon2id, scrypt, bcrypt, PBKDF2), and compare in constant time.
- TOTP is HMAC-SHA1 of a 30-second time step with a shared secret, truncated to 6 digits; servers accept a small window.
- Only origin-bound methods (FIDO2 security keys and passkeys) resist real-time phishing. SMS, TOTP and push can all be relayed or abused.
- Recovery paths often undo strong authentication; design them as carefully as the login.
## Glossary
| Term | Definition |
|---|---|
| Identification | Claiming an identity, such as a username. |
| Authentication | Proving that a claimed identity is genuine. |
| Authorization | Deciding what an authenticated identity may do. |
| Factor | A category of evidence: something you know, have or are. |
| Multi-factor authentication (MFA) | Authentication that uses two or more different factors. |
| Verifier | The service that checks a credential. |
| Blocklist | A list of common or breached passwords that users aren't allowed to choose. |
| Password manager | A tool that generates, stores and fills unique passwords, protected by one master secret. |
| Hash function | A one-way function that turns input into a fixed-size fingerprint. |
| Salt | A random, per-user value stored with the hash so identical passwords hash differently. |
| Key derivation function | A deliberately slow (and often memory-hungry) function used to hash passwords, such as Argon2id, scrypt, bcrypt or PBKDF2. |
| Brute force | Trying many passwords until one works. |
| Password spraying | Trying one or a few common passwords against many accounts. |
| Credential stuffing | Reusing leaked username and password pairs on other services. |
| One-time password (OTP) | A code valid for a single use or a short time. |
| HOTP | HMAC-based one-time password, built on a counter (RFC 4226). |
| TOTP | Time-based one-time password, built on the clock (RFC 6238). |
| HMAC | A keyed hash that only someone with the key can compute. |
| SIM swap | Moving a phone number to an attacker's SIM card to receive its SMS messages. |
| Push fatigue | Wearing a user down with repeated approval prompts until they approve. |
| Phishing resistance | The property that a credential can't be captured and replayed on the real site by a fake one. |
| FIDO2 / WebAuthn | Standards for public-key sign-in with security keys and passkeys. |
| Origin binding | Including the web address in what the authenticator signs, so a signature works only for the real site. |
| Passkey | A WebAuthn credential used instead of a password, stored on a device or synced between devices. |
| Single sign-on (SSO) | Signing in once at an identity provider to access many applications. |
## Check your understanding
1. A colleague says "we use two-factor authentication: a password and the name of your first school." Explain why this isn't MFA and what you would change.
2. Your company policy forces a new 8-character password with symbols every 60 days. Which parts of it go against SP 800-63B, and what would you propose?
3. Explain in your own words why two users with the same password should have different stored values, and what would go wrong if the table used plain SHA-256.
4. A TOTP code is 6 digits long, so there are only a million values. Why can't an attacker simply guess it, and what must the server do to keep it that way?
5. For a company's administrators, rank SMS, a TOTP app, push with number matching and a FIDO2 key, and justify the order.
6. Design a safe "I lost my phone" process for an account that uses a passkey. Which weak points do you need to avoid?
## References
- [NIST SP 800-63B-4](https://pages.nist.gov/800-63-4/sp800-63b.html), Digital Identity Guidelines: Authentication and Authenticator Management (sections 3.1.1 and 3.2.2)
- [RFC 4226](https://www.rfc-editor.org/rfc/rfc4226), HOTP: An HMAC-Based One-Time Password Algorithm
- [RFC 6238](https://www.rfc-editor.org/rfc/rfc6238), TOTP: Time-Based One-Time Password Algorithm
- [RFC 9106](https://www.rfc-editor.org/rfc/rfc9106), Argon2 memory-hard function; [RFC 7914](https://www.rfc-editor.org/rfc/rfc7914), scrypt; [RFC 8018](https://www.rfc-editor.org/rfc/rfc8018), PKCS #5 (PBKDF2)
- [W3C Web Authentication](https://www.w3.org/TR/webauthn-2/) and the [FIDO Alliance specifications](https://fidoalliance.org/specifications/)
- [OWASP Password Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html)