Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
How 2FA codes are generated: the TOTP algorithm, shared secrets and QR codes, why SMS codes are weaker, and how security keys and passkeys resist phishing.
Quick answer: When you set up an authenticator app, the website generates a random secret key and shows it as a QR code. Your phone and the server now share that secret. Every 30 seconds, both sides independently run the same calculation: combine the secret with the current time, using a cryptographic function called HMAC, and reduce the result to six digits. Since both have the same secret and roughly the same clock, they arrive at the same code. No network connection is needed, and the code never has to be transmitted in advance. This standard is called TOTP.
Authentication factors fall into three categories:
Two-factor authentication (2FA) requires two from different categories. A password and a security question are both things you know, so that is not 2FA.
The point is simple. Passwords get phished, guessed and leaked in breaches, and a stolen login is a common way in for ransomware. If a login also requires a device the attacker does not have, a stolen password alone is not enough.
TOTP (Time-based One-Time Password) is defined in RFC 6238, building on HOTP from RFC 4226.
otpauth://totp/Example:[email protected]?secret=JBSWY3DPEHPK3PXP&issuer=Example&digits=6&period=30
From this moment, the server and your phone hold the same secret. It is never sent again.
Both sides perform the same steps:
import hmac, hashlib, struct, time
def totp(secret: bytes, period=30, digits=6):
counter = int(time.time()) // period
digest = hmac.new(secret, struct.pack(">Q", counter), hashlib.sha1).digest()
offset = digest[-1] & 0x0F
number = struct.unpack(">I", digest[offset:offset + 4])[0] & 0x7FFFFFFF
return str(number % 10**digits).zfill(digits)
That is the entire algorithm. It works offline, because it needs only the secret and a clock.
The server runs the same calculation and compares. Because clocks are never perfectly aligned (see why clocks can't be trusted) and typing takes time, servers usually also accept the code from the 30-second window before and after.
A well-built server also:
HOTP uses the same calculation with a counter in place of the time. The counter increases each time a code is generated, for example by pressing a button on a hardware token. The drawback is that the token and server can drift out of step. TOTP avoids this by using the clock as the counter.
The server generates a random code and texts it to you.
It is far better than a password alone, and it is also the weakest common method:
The service sends "Are you trying to sign in?" to an app on your phone. It is convenient, but attackers exploit prompt fatigue: triggering repeated requests until the user taps approve. Number matching, where you must type a number shown on the login screen, counters this.
These work on a different principle: public key cryptography, using the FIDO2 and WebAuthn standards.
Two properties make this much stronger:
A passkey is this same technology used as the main sign-in method, often synchronised between your devices through a password manager or platform account. The FIDO Alliance describes how they work. A passkey combines possession of the device with a biometric or PIN, so it is multi-factor on its own, and it replaces the password entirely.
| Method | Resists phishing | Works offline | Main weakness |
|---|---|---|---|
| SMS code | No | No | SIM swap, interception |
| TOTP authenticator app | No | Yes | Code can be relayed by a phishing site in real time |
| Push with number matching | Partly | No | Social engineering |
| Hardware security key | Yes | Yes | Cost; can be lost |
| Passkey | Yes | Yes | Depends on device or account recovery |
TOTP's limitation deserves a note. If you type your password and code into a convincing fake site, that site can pass both to the real one within the 30-second window. TOTP protects against leaked and reused passwords, not against real-time phishing.
The weakest part of many 2FA set-ups is what happens when the device is lost.
It computes each code from a secret stored on the phone and the current time. Nothing needs to be received.
The current time, in 30-second steps, is one of the inputs to the calculation. A new step produces a new code, so an intercepted code is only briefly useful.
It is much better than no second factor, but it is vulnerable to SIM swapping and phishing. Prefer an authenticator app, and better still a security key or passkey.
2FA adds a second step to a password. A passkey replaces the password with a cryptographic key on your device, unlocked by a biometric or PIN, and it cannot be phished.
A six-digit code looks like magic and is just arithmetic: a shared secret, the current time, and a keyed hash. It stops attackers who only have your password. For protection against phishing as well, move to security keys and passkeys, which prove possession of a private key that never leaves your device.