What Is a TOTP Code? Complete Guide to RFC 6238 Authentication

Understanding Time-Based One-Time Passwords (TOTP)
In modern identity security, passwords alone are no longer sufficient to protect sensitive enterprise accounts. A TOTP (Time-Based One-Time Password) is a temporary numeric passcode generated automatically by combining a shared secret key with the current timestamp.
Formally standardized under IETF RFC 6238, TOTP powers virtually all major authenticator apps, including Google Authenticator, Microsoft Authenticator, 1Password, and our own in-browser Free TOTP Generator & 2FA Tool.
1. The Core Components of TOTP
Every TOTP authentication exchange relies on four distinct parameters:
- The Shared Secret (K): A cryptographic key encoded in Base32 format (e.g.,
JBSWY3DPEHPK3PXP) established when you first configure two-factor authentication. Both your authenticator client and the authentication server hold an identical copy of this secret. - The Current Unix Time (T): The number of seconds elapsed since the Unix epoch (January 1, 1970 00:00:00 UTC).
- The Time Interval (X): The validity period for each code, which defaults to 30 seconds across the vast majority of consumer and enterprise systems.
- The Cryptographic Hash Algorithm: RFC 6238 specifies HMAC-SHA1 as the baseline standard, with formal extensions for HMAC-SHA256 and HMAC-SHA512.
2. The Step-by-Step Mathematical Generation Process
When you open your authenticator or generate a code, the following computation occurs instantaneously:
- Counter Calculation: The current Unix time $T$ is divided by the step window $X$ (30 seconds): $C = lfloor T / X floor$. This yields a stable integer counter that increments once every 30 seconds.
- HMAC Computation: The counter $C$ is formatted into an 8-byte big-endian binary buffer. The client computes the HMAC hash of this buffer using the decoded secret key: $HS = ext{HMAC}(K, C)$.
- Dynamic Truncation (RFC 4226): The last nibble (4 bits) of the HMAC hash determines an offset between 0 and 15. Starting at this offset, a 4-byte dynamic binary code is extracted, masking out the sign bit.
- Modulo Reduction: The extracted binary integer is reduced modulo $10^6$ (for 6-digit codes) or $10^8$ (for 8-digit codes) to output the familiar passcode.
3. 6-Digit vs. 8-Digit Codes
While consumer services such as Meta, Google, and GitHub use 6-digit codes ($10^6$ possible values), high-security financial institutions and enterprise VPNs frequently configure 8-digit codes ($10^8$ possible values) for increased entropy against brute-force attacks.
4. Why TOTP Outperforms SMS Two-Factor Authentication
While receiving SMS text messages is common, NIST guidelines discourage SMS for high-value authentication due to serious vulnerabilities:
- SIM Swapping: Attackers impersonate victims to deceive mobile carriers into reassigning phone numbers to fraudulent SIM cards.
- SS7 Network Interception: Global telecommunications signaling vulnerabilities allow sophisticated threat actors to intercept SMS traffic in transit.
- Network Outages: SMS requires cellular reception, failing during international travel or poor coverage.
In contrast, TOTP executes 100% locally and mathematically on your device, requiring zero cellular connectivity and remaining fully immune to SIM swaps.
5. Security Limitations of TOTP
While TOTP provides robust defense against credential stuffing and remote password reuse, users should be aware of its operational limitations:
- Real-Time Phishing Proxies: Reverse proxy attack frameworks (such as Evilginx) can trick users into entering valid TOTP codes on lookalike domains, immediately using the code before the 30-second window expires.
- Local Device Compromise: If malware or an unauthorized browser extension compromises your machine, stored secrets could theoretically be exposed.
To safely generate codes in your browser without saving secrets to disk, use our private in-browser 2FA live code generator. For troubleshooting issues when codes fail to authenticate, read our guide on how to fix authenticator code failures.