How two-factor authentication works
A password can be stolen. A second, independent factor is much harder to steal too.
2FA requires two of three independent factor types: something you know (password), something you have (phone/TOTP/hardware key), or something you are (biometric). The common second factor is a TOTP app that derives a time-based 6-digit code from a shared secret and the current time, so a leaked password alone isn't enough to get in.
Enter password
The user submits their password — the 'something you know' first factor — to the auth server over HTTPS. This is the same knowledge-based secret that, on its own, is vulnerable to phishing, reuse, and database leaks. 2FA exists precisely because this factor alone is no longer enough.
First factor accepted
The server checks the submitted password against its stored hash. The password is correct, but the server does not grant access yet — it now treats the user as partially authenticated. Requiring more than a correct password is the entire point of the second factor.
Prompt for second factor
The server challenges the user for a second, independent factor, typically a 6-digit code from an authenticator app. This factor is 'something you have' — proof of possession of the enrolled device. Because it's a different category from the password, an attacker who only stole the password is now stuck.
Generate a TOTP
The authenticator app computes a Time-based One-Time Password from the shared secret established at enrollment and the current Unix time, per RFC 6238. It runs an HMAC over the time step and truncates the result to six digits, entirely offline. Nothing about this computation ever touches the network.
Enter the code
The user reads the code from the app and types it into the auth server before it rolls over, usually within a 30-second window. The code itself is what travels — never the underlying secret. If it arrives after expiry, the user simply enters the next one.
Verify
The server independently derives the expected code from its own copy of the shared secret and the current time, then compares it to what the user submitted. It usually accepts the adjacent time window too, to tolerate small clock drift. A match proves the user physically holds the enrolled device.
Access granted
With both an accepted password and a valid second factor, the server finally establishes an authenticated session and lets the user in. An attacker holding only a stolen password would have stalled at the second-factor prompt. That gap between knowing the password and possessing the device is exactly the security 2FA buys.
The problem: passwords fail alone
A password is a shared secret, and shared secrets are fragile. Users reuse the same one across sites, so a leak anywhere becomes a leak everywhere (credential stuffing). Passwords get phished by convincing fake login pages. They get dumped in database breaches, and even hashed passwords fall to offline cracking if they're weak. The uncomfortable truth is that a password, by itself, can be stolen without the victim ever noticing, and the attacker then walks in looking exactly like the legitimate user.
Two-factor authentication (2FA) — more generally multi-factor authentication (MFA) — fixes this by demanding a *second, independent* proof of identity in addition to the password. The insight is not that the second factor is unbreakable, but that it's independent: compromising it requires a completely different kind of attack than stealing a password. An attacker would have to both phish your password *and* physically obtain your device, at the same time. Forcing two unrelated attacks to succeed together is what makes account takeover dramatically harder.
This is why a leaked password database is far less catastrophic for a 2FA-protected service: the passwords alone don't get anyone in.
The three factor categories
Authentication factors fall into three fundamentally different categories, and *genuine* 2FA means combining two different categories — not two things from the same one.
- Something you know — a password, PIN, or passphrase. A knowledge secret. Weakness: it can be copied, phished, guessed, or leaked without you noticing.
- Something you have — a physical possession: your phone running an authenticator app, a hardware security key (YubiKey), or a smart card. Weakness: it can be lost or stolen, but that's a *physical* act the owner tends to notice.
- Something you are — an inherent biometric: fingerprint, face, or iris. Weakness: it can't be changed if compromised, and matching is probabilistic.
The reason the categories must differ is that they fail to *different* attacks. Two passwords, or a password plus a security question, are both "something you know" — one phishing page or one leak captures both, so it isn't real 2FA. But a password (know) plus a code from your phone (have) forces an attacker to run two unrelated attacks. A common mislabel to watch for: a security question is *not* a second factor, because it's still knowledge and often publicly guessable.
How TOTP actually works
The most common second factor is a TOTP — a Time-based One-Time Password, the six digits your authenticator app (Google Authenticator, Authy, 1Password) shows and rotates every 30 seconds. It's defined by RFC 6238, building on the HMAC-based OTP of RFC 4226. Its cleverness is that *nothing secret ever crosses the network at login*.
At enrollment, the server generates a random shared secret and shows it to you as a QR code (which encodes an otpauth:// URI). Your app scans it, and now both the server and the app hold the *same* secret. That secret is exchanged exactly once, then never again.
To generate a code, the app takes the current Unix time, divides it into 30-second time steps, and computes HMAC-SHA1(secret, time_step). It then applies "dynamic truncation" to squeeze that hash down to a 6-digit number. Because the server has the identical secret and reads the same clock, it can compute the *same* code independently and compare. No code, and certainly no secret, needs to be transmitted for verification — the server derives what it expects.
Two practical details fall out of this design. First, both sides must agree on time, so servers typically accept the code from the adjacent time window (±1 step) to tolerate small clock drift. Second, because generation is pure math on a secret plus a clock, the authenticator app works fully offline — on a plane, with no signal — which is a real usability and security advantage.
The methods compared: SMS, TOTP, push, hardware keys
Not all second factors are equally strong, and the differences matter.
SMS one-time codes are the most common because they need no app — the server texts you a code. But SMS is the weakest widely-used method. The devastating attack is the SIM swap: an attacker socially engineers your mobile carrier into porting your number to their SIM, and every SMS code now arrives on *their* phone. SMS can also be intercepted via SS7 network flaws or read from lock-screen previews. It's still far better than no second factor, but it's the one to move away from.
TOTP authenticator apps are stronger because the shared secret lives only on your device and codes are generated offline — there's no phone number to hijack and no message to intercept. Their residual weakness is real-time phishing: a fake site can ask for your current code and relay it instantly.
Push-based approval (a "Approve this login?" prompt) is convenient but vulnerable to MFA fatigue, where an attacker spams prompts until a tired user taps approve. Number-matching mitigates this.
Hardware security keys using FIDO2/WebAuthn are the gold standard. The key does a cryptographic challenge-response tied to the exact site's domain, so it *cannot* be phished — a fake domain simply won't get a valid response. This is phishing-resistant MFA, and it's why high-security environments mandate it.
Recovery, trade-offs, and guidance
Adding a second factor creates a new problem: what if you lose it? If your phone with the authenticator app is lost, stolen, or wiped, you're locked out of your own account. This is why every serious 2FA rollout must include a recovery path, and the standard one is backup codes — a set of single-use codes generated at enrollment that the user is told to print or store somewhere safe. Each works once as a substitute second factor. Alternatives include enrolling a second hardware key, or a carefully secured account-recovery process. The tension is permanent: recovery paths are, by definition, ways *around* the second factor, so a weak recovery flow (like "answer a security question") quietly undoes all the protection.
The broader trade-off is security versus friction. Every added factor is one more step, one more thing to lose, one more support ticket. Mature systems soften this with risk-based / adaptive MFA — only prompting for the second factor when something looks unusual (new device, new country, sensitive action) and remembering trusted devices otherwise.
Practical guidance to carry away: enable 2FA everywhere it's offered; prefer a TOTP app or, better, a hardware security key over SMS; save your backup codes somewhere offline; and if you run a service, offer strong factors, require them for privileged accounts, and design the recovery flow as carefully as the login itself. The whole point is that stealing a password should no longer be enough — don't let a sloppy recovery path hand that advantage back.
2FA combines two of: something you know (password), something you have (phone/TOTP/hardware key), something you are (biometric).
TOTP apps generate a time-based 6-digit code from a shared secret plus the current time (RFC 6238).
The server and app derive the same code independently, so nothing secret travels over the network at login.
SMS OTP is weaker because SIM-swap attacks can redirect the text to an attacker.
The flow gates access on the second factor even after the password is accepted.