Time-Based One-Time Passwords (TOTP)
What you'll learn: How TOTP generates temporary, expiring codes using shared secrets and time synchronization to strengthen authentication.
What is TOTP?
TOTP is a possession factor authentication mechanism that generates unique, short-lived codes based on the current time and a shared secret. It's the technology behind apps like Google Authenticator, Microsoft Authenticator, and Authy. Each code is valid for only 30 seconds (typically), making stolen codes nearly worthless.
How TOTP Works
The Shared Secret
When you set up TOTP, the server generates a random secret key and shares it with your authenticator app (usually via QR code). Both parties now possess the same secret—think of it as synchronized secret ingredients for a recipe that changes every 30 seconds.
Time-Step Algorithm
TOTP uses the current Unix timestamp divided into 30-second intervals (called "time steps"). Here's the process:
- Both server and app calculate the current time step (e.g., current time ÷ 30 seconds)
- They combine the shared secret with this time step value
- They apply a cryptographic hash function (typically HMAC-SHA1)
- The result is truncated to a 6-8 digit code
Since both parties use the same secret and time, they independently generate identical codes.
Authenticator Apps
Apps like Google Authenticator store your shared secrets securely on your device and continuously compute fresh codes. They display the current valid code along with a countdown timer. The server performs the same calculation when you submit the code and verifies it matches (usually allowing a small time window for clock drift).
Why It's Secure
Even if someone intercepts your TOTP code, it expires in seconds. The attacker would need both the code and access within that narrow window. The shared secret never leaves your device or the server after initial setup.
Key Takeaway: TOTP combines a shared secret with time-step intervals and cryptographic hashing to generate temporary codes that expire quickly, providing strong possession-based authentication without requiring network connectivity.