Skip to content
Back to CurriculumLesson 09 of 30
cryptography-passwordsIntermediate

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:

SMS OTP

A 6-digit code sent via text message to your registered mobile phone number.

Email OTP

A temporary verification code or magic link delivered straight to your email inbox.

Authenticator App

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):

1. Something You Know
Password or PIN

Information memorized in your mind.

2. Something You Have
Phone or Security Key

A physical device you possess in your hands.

3. Something You Are
Biometrics

Your physical fingerprint, Face ID, or iris scan.

Why this works: Even if a criminal on the other side of the world steals your password (Factor 1), they cannot access your account because they do not physically hold your smartphone generating the second factor (Factor 2).

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:

TypeHow It WorksProsTrade-offs
SMS OTPCode is sent via mobile phone text messageVery familiar; requires no smartphone appsVulnerable to SIM swapping and mobile network interception
Email OTPCode is sent to user's registered emailFree to send; convenient on laptopsIf the email account is hacked, the OTP is compromised too
TOTP (Time-Based)Calculated locally using a shared secret and current timeWorks 100% offline; fast and immune to SIM swappingUser must have an authenticator app installed
HOTP (Counter-Based)Calculated using a shared secret and an incrementing counterIndependent of time; common in physical key fobsCounter can get out of sync if pressed repeatedly
Interactive Demonstration

Multi-Factor Authentication & OTP Studio

Local browser simulation • No external data sent
Concept:

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.

1. Request Verification Code

Destination: +1 (555) 019-2834

2. Enter Code on Website

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:

1. The One-Time Shared Secret

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!

2. Universal 30-Second Time Windows

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.

3. Deterministic Local Math (HMAC-SHA1)

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.

4. Server Verification & Replay Defense

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).

Interactive Visual Simulation: Step-by-Step in Action

Follow the live visual demonstration below to see how the phone and server interact, compute, and verify codes without ongoing network connectivity:

Step 1 of 7: Setup InitiationOffline Cryptographic Verification

Server Generates Secret & QR Code

Step 1 of 7
Mobile PhoneOffline Ready
12:00
✈️ AIRPLANE
G
Google Authenticator
Time-Based OTP Vault
No Accounts Configured
Tap "+" to scan the QR code displayed on your website security page.
Camera Standby
Setup Step 1

The user navigates to website security settings and clicks "Enable 2FA". The server creates a secret key.

Website / ServerDB Secure
https://cscosmos.edu/settings/security
Set Up Two-Factor Auth
Scan this code with Google Authenticator
Shared Secret Key
JBSWY3DPEHPK3PXP
CSCosmos Identity EngineRFC 6238 Compliant
Step 1 Educational Breakdown

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.

💡 Student Takeaway:The QR code is just a graphical shortcut. It exists solely so users don’t have to manually type a 16-character random string on a mobile keyboard.
Why Clock Drift Matters:

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:

TOTP (Time-Based)
Rotates with Time

Code automatically refreshes every 30 seconds as the clock advances. Most popular in smartphone apps today.

HOTP (Counter-Based)
Advances on Button Press

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):

Single-Use by Design

Each backup code can be used exactly once. As soon as you log in with a backup code, the server permanently invalidates it.

Stored as Hashes

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!

1

What does "OTP" stand for, and what makes it different from a standard password?

2

Multi-Factor Authentication (MFA) requires proof from at least two different categories. Which pair represents two genuine, distinct factors?

3

Why can Google Authenticator generate valid 6-digit TOTP codes even when your phone is in Airplane Mode with zero internet or cellular service?

4

What is the fundamental difference between TOTP and HOTP?

5

What is the primary purpose of emergency backup codes, and how should they be treated after they are used?