Multi-Factor Authentication: TOTP (RFC 6238), HOTP (RFC 4226) & Emergency Backup Codes
Multi-Factor Authentication (MFA) adds a vital second layer of defense beyond passwords. Time-Based One-Time Passwords (TOTP) generate rotating 6-digit codes on mobile authenticator apps using a shared secret and current time—working completely offline—backed by single-use recovery codes.
A. What Is an OTP?
An OTP (One-Time Password) is a temporary numeric code generated to verify a user's identity. Unlike your main account password—which remains the same every day until you change it—an OTP is valid for only one login attempt and expires within a short window of time (such as 30 seconds to a few minutes).
You have likely encountered OTPs when shopping online or logging into university portals:
A 6-digit code sent via text message to your registered mobile phone number.
A temporary verification code or magic link delivered straight to your email inbox.
A rotating code generated directly on your device using apps like Google Authenticator or Microsoft Authenticator.
B. What Is Multi-Factor Authentication (MFA)?
In traditional login systems, you only enter a username and password. If a malicious attacker discovers your password—through a database breach, malware, or a phishing email—they can log straight into your account.
Multi-Factor Authentication (MFA) solves this problem by requiring you to prove your identity using at least two different categories of evidence (factors):
Information memorized in your mind.
A physical device you possess in your hands.
Your physical fingerprint, Face ID, or iris scan.
C. Types of OTP: A Quick Comparison
Not all OTP systems operate the same way. Here is a simple comparison of how each delivers or calculates codes:
| Type | How It Works | Pros | Trade-offs |
|---|---|---|---|
| SMS OTP | Code is sent via mobile phone text message | Very familiar; requires no smartphone apps | Vulnerable to SIM swapping and mobile network interception |
| Email OTP | Code is sent to user's registered email | Free to send; convenient on laptops | If the email account is hacked, the OTP is compromised too |
| TOTP (Time-Based) | Calculated locally using a shared secret and current time | Works 100% offline; fast and immune to SIM swapping | User must have an authenticator app installed |
| HOTP (Counter-Based) | Calculated using a shared secret and an incrementing counter | Independent of time; common in physical key fobs | Counter can get out of sync if pressed repeatedly |
Multi-Factor Authentication & OTP Studio
A normal OTP is delivered directly to the user over an external channel (like a text message or email). It is temporary and valid for only one use.
Destination: +1 (555) 019-2834
D. How Google Authenticator Works (TOTP)
When you enable 2-Step Verification with Google Authenticator, a fascinating question arises: How does the app generate the exact same code that the server expects, even when your phone has no internet connection?
The secret is a standard algorithm called TOTP (Time-Based One-Time Password), defined in RFC 6238. Here is how it works under the hood:
When you enable 2FA, the website server generates a random 160-bit cryptographic key (e.g., JBSWY3DPEHPK3PXP). It encodes this key into a QR code. Your phone camera scans it once, storing it locally in its secure keychain. After this initial pairing, the phone never needs internet access again!
Instead of counters, TOTP uses standard Unix epoch time (seconds since Jan 1, 1970). Both devices divide current time by 30 seconds:Window = Floor(Unix Timestamp / 30)Because world time moves identically on Earth, both clocks land on the exact same window number (e.g. #59079001) simultaneously.
Both devices feed the exact same Shared Secret and Time Window into an HMAC-SHA1 hashing function. The resulting hash is truncated down to 6 numeric digits:HMAC-SHA1(Secret, Window) ➔ 482 910Identical inputs + identical algorithm = identical 6-digit result, completely offline.
You type the 6 digits into the website. The server simply verifies whether your submitted number matches the expected number it calculated. Because codes expire after 30 seconds, an attacker who intercepts an old OTP cannot reuse it later (protecting against replay attacks).
Follow the live visual demonstration below to see how the phone and server interact, compute, and verify codes without ongoing network connectivity:
Server Generates Secret & QR Code
The user navigates to website security settings and clicks "Enable 2FA". The server creates a secret key.
When you click "Enable 2FA" in your account security settings, the website server generates a random 160-bit shared secret key (e.g., JBSWY3DPEHPK3PXP). The server encodes this key into an otpauth:// URL and displays it on your screen as a high-contrast QR code.
Your phone's clock and the server's clock might differ by a few seconds. To prevent frustrating login failures, servers usually allow a small clock tolerance window (checking the current 30-second block as well as the immediate previous and next blocks).
E. HOTP (Counter-Based OTP)
Before TOTP became widespread, systems used HOTP (HMAC-Based One-Time Password). Instead of looking at a clock, HOTP uses a counter:
Code automatically refreshes every 30 seconds as the clock advances. Most popular in smartphone apps today.
Code changes only when a button is pressed or an event happens. Common in physical hardware key fobs.
F. Emergency Backup Codes
What happens if you drop your phone in water, shatter the screen, or lose it while traveling? Without your phone, you cannot open Google Authenticator. Would you be permanently locked out of your account forever?
To prevent permanent lockouts, secure applications issue a set of Emergency Backup Codes (Recovery Codes):
Each backup code can be used exactly once. As soon as you log in with a backup code, the server permanently invalidates it.
Just like passwords, servers store backup codes in hashed form so that an unauthorized database dump cannot reveal valid recovery codes.
H. Knowledge Check
Test your understanding with these 5 conceptual questions. Click an answer to check yourself!
What does "OTP" stand for, and what makes it different from a standard password?
Multi-Factor Authentication (MFA) requires proof from at least two different categories. Which pair represents two genuine, distinct factors?
Why can Google Authenticator generate valid 6-digit TOTP codes even when your phone is in Airplane Mode with zero internet or cellular service?
What is the fundamental difference between TOTP and HOTP?
What is the primary purpose of emergency backup codes, and how should they be treated after they are used?