1 The Puzzle
This is not magic. It is a beautifully simple piece of cryptography called TOTP — Time-Based One-Time Password. Once you understand it, you will never look at that little spinning countdown the same way again.
2 What Is 2FA?
Two-Factor Authentication (2FA) is the idea that logging in should require two separate proofs of identity — not just one. The classic analogy is a bank safe: the manager has one key, and the owner has another. Neither can open it alone. Both keys must be present.
For your online accounts, the two factors are typically:
- Something you know — your password
- Something you have — your phone (which generates the OTP)
Even if an attacker steals your password, they cannot log in without your phone. Here is how these two locks work together:
3 The Setup — Scanning That QR Code
Here is the key insight that most people miss: the magic happens once, during setup. When you enable 2FA on any service, the first thing that happens is this: the server generates a random secret key — a string of random bytes — and shows it to you as a QR code. You scan it. Now both your phone and the server hold that same secret. That is the only time any secret travels anywhere.
Think of it like two identical clocks that were synchronized once, then sealed in separate rooms. They never communicate again, but they always show the same time because they were set together at the start. The secret key is that initial synchronization.
4 The Formula
Every 30 seconds, both your phone and the server independently run the exact same calculation:
Let us break down each ingredient:
secret_key — A random string of bytes (shown as Base32 text like JBSWY3DPEHPK3PXP) that was in the QR code. Both your phone and the server have it. It never changes.
time_window — This is just the current Unix timestamp (seconds since January 1, 1970) divided by 30 and rounded down. Right now, that number is the same on your phone and every Google server everywhere in the world — because clocks agree. This is the secret sauce: time is a shared secret that nobody needs to communicate.
HMAC-SHA1 — HMAC stands for "Hash-based Message Authentication Code." SHA1 is a hashing algorithm. Together, HMAC-SHA1 takes the secret key and the time window, mixes them in a mathematically one-way function, and produces a long number. The last 6 digits (trimmed to 6 digits by a process called "dynamic truncation") become your OTP.
5 The 30-Second Window
Time is divided into 30-second blocks. Each block gets a completely different code, because the time_window input to the formula changes each time. Scroll the timeline below to see how each window maps to a different OTP:
The server is forgiving by about one window in either direction (so ±30 seconds of clock drift is fine). Beyond that, the codes simply won't match — which is by design. An OTP from 90 seconds ago is completely useless.
6 Live Demo — Phone and Server, In Sync
The real magic: watch your phone and Google's server both independently compute the same code, at the same time, without ever communicating. Both panels below update every second, driven only by your clock:
7 How the Server Verifies
When you type your OTP and click "Sign in," here is exactly what happens on the server side in milliseconds:
HMAC-SHA1(your_secret, floor(now/30)). It does this without your phone being involved at all.8 Why It Is Secure
TOTP has three separate security properties that make it robust against common attacks:
9 End-to-End TOTP Flow
Here is the complete lifecycle — from enabling 2FA to getting access granted — as a single connected flowchart:
10 The Code (For Developers)
If you are a developer, here is how trivially easy TOTP is to implement — because the heavy lifting is done by well-audited libraries:
pyotp, otpauth, speakeasy). The spec details in RFC 6238 (TOTP) and RFC 4226 (HOTP) are subtle, and a custom implementation is a security liability. The real code above is literally all you need.11 Frequently Asked Questions
TOTP depends entirely on both sides agreeing on the current time. If your phone clock drifts, the time_window values diverge and the codes stop matching. Most authenticator apps handle up to ±30 seconds of drift (one window in either direction). If your clock is significantly wrong, enabling automatic time sync in your phone settings (Settings → Date & Time → Set Automatically) fixes it immediately. Google Authenticator also has a "Time Correction for Codes" feature that compensates for drift by comparing against Google's servers periodically — without revealing your secret.
Technically, yes — but it is much harder to exploit than stealing a password. If an attacker intercepts your OTP via a man-in-the-middle attack (e.g., a fake login page that relays your credentials in real-time), they could use it within the 30-second window. This is called a real-time phishing attack. TOTP does not defend against this. For that level of protection, you need hardware security keys (FIDO2/WebAuthn, like a YubiKey), which are phishing-resistant by design. But for the vast majority of threats — account stuffing, credential dumps, remote attacks — TOTP is extremely effective.
RFC 6238, published in 2011 by the IETF (the body that standardizes internet protocols), specifies 30 seconds as the default window. It is a deliberate human-factors decision: user studies found that most people take between 5 and 25 seconds to type a 6-digit code after seeing it. A 30-second window gives comfortable headroom. 60-second windows are allowed by the spec, and some services use them, but 30 seconds is the default that Google Authenticator, Authy, and most implementations adopted. The shorter the window, the smaller the replay-attack surface, so 30 seconds became the sweet spot.
No — SMS OTP is significantly weaker. The code travels over the phone network, which is vulnerable to SIM-swapping attacks (where an attacker convinces your carrier to port your number to their SIM), SS7 protocol vulnerabilities (which can intercept SMS messages at the network level), and real-time phishing. TOTP codes are generated entirely on your device and never transmitted from your phone to any network. The NIST (National Institute of Standards and Technology) deprecated SMS as a second factor in 2016, and most security researchers recommend switching to an authenticator app (TOTP) or a hardware key. That said, SMS OTP is infinitely better than no 2FA at all — so if a service only offers SMS, use it.
Curious how other protocols protect you?
TOTP is one piece of the modern security stack. The same elegant cryptographic thinking shows up everywhere — in the TLS handshake that secures every HTTPS connection, in end-to-end encryption, in digital signatures. Keep reading:
Srikanth Badavath