Totp Token Calculator
Generate time-based one-time passwords (TOTP) from a secret key for two-factor authentication. Enter values for instant results with step-by-step formulas.
Reviewed for accuracy by Daniel Agrici, Founder & Lead Developer
Totp Token Calculator
Calculator
Adjust values & calculateEnter your values below. Every result is computed in your browser โ no data is sent to any server.
Formula: TOTP = Truncate(HMAC-SHA1(Secret, floor(Time / TimeStep))) mod 10^Digits
Worked example โ 3-in-a-million brute-force odds | 160-bit secret entropy | 5,000 daily auth events | 0.015 expected false accepts per day
Formula
TOTP = Truncate(HMAC-SHA1(Secret, floor(Time / TimeStep))) mod 10^Digits
The TOTP algorithm divides the current Unix timestamp by the time step to produce a counter, applies HMAC-SHA1 using the shared secret, then truncates the hash output to produce a numeric code with the specified number of digits. The verification window extends acceptance to adjacent time steps.
Worked Examples
Example 1: Standard TOTP Configuration Analysis
Problem:A company uses standard TOTP with 20-byte secrets, 30-second steps, 6-digit codes, window size 1, for 1,000 users averaging 5 logins per day.
Solution:Total possible codes = 10^6 = 1,000,000 Valid codes per window = 1 + 2(1) = 3 Brute-force odds = 3/1,000,000 = 0.0003% Codes generated per day = 86,400/30 = 2,880 Secret entropy = 20 x 8 = 160 bits Base32 secret length = ceil(160/5) = 32 characters Daily auth events = 1,000 x 5 = 5,000 Expected false accepts/day = 5,000 x 0.000003 = 0.015
Result:3-in-a-million brute-force odds | 160-bit secret entropy | 5,000 daily auth events | 0.015 expected false accepts per day
Example 2: High-Security 8-Digit Configuration
Problem:A financial institution needs higher security using 32-byte secrets, 60-second steps, 8-digit codes, window size 0, for 50,000 users with 3 logins per day.
Solution:Total possible codes = 10^8 = 100,000,000 Valid codes per window = 1 + 2(0) = 1 Brute-force odds = 1/100,000,000 = 0.000001% Codes generated per day = 86,400/60 = 1,440 Secret entropy = 32 x 8 = 256 bits Base32 secret length = ceil(256/5) = 52 characters Daily auth events = 50,000 x 3 = 150,000 Expected false accepts/day = 150,000 x 0.00000001 = 0.0015
Result:1-in-100-million brute-force odds | 256-bit secret entropy | 150,000 daily events | Near-zero false accept rate
Frequently Asked Questions
What is TOTP and how does time-based one-time password generation work?
TOTP (Time-based One-Time Password) is an algorithm defined in RFC 6238 that generates temporary numeric codes using a shared secret key and the current time. The algorithm works by dividing the current Unix timestamp by a time step (typically 30 seconds) to create a counter value, then applying HMAC-SHA1 to the counter using the shared secret as the key. The resulting hash is truncated to produce a 6 or 8 digit code. Both the server and the authenticator app independently compute the same code because they share the same secret and use the same time reference. This is why clock synchronization between the device and server is critical for TOTP to function correctly.
Why is a 30-second time step the standard for most TOTP implementations?
The 30-second time step represents a carefully chosen balance between security and usability. Shorter intervals like 10 seconds would be more secure because each code is valid for less time, but users would frequently experience codes expiring while they are still typing them, leading to frustration and failed authentication attempts. Longer intervals like 60 seconds keep codes valid longer, increasing the window of vulnerability if a code is intercepted or observed. The 30-second standard gives users enough time to read the code from their authenticator app and enter it into the login form, while keeping the exposure window reasonably short. Some high-security implementations use 60-second windows with 8-digit codes for additional security.
How does the verification window affect TOTP security?
The verification window determines how many time steps before and after the current step the server will accept as valid codes. A window of 1 means the server accepts the current code plus one step before and one step after, effectively creating a 90-second valid window for a 30-second time step. This accommodation handles clock drift between the user device and server. Larger windows increase usability by tolerating more clock skew but reduce security by accepting more codes simultaneously. A window of 1 triples the brute-force odds compared to window 0, from 1 in 1,000,000 to 3 in 1,000,000 for 6-digit codes. Most implementations use window 1 as the default, with some enterprise systems using window 0 and requiring strict NTP synchronization on all devices.
What secret key length should I use for TOTP implementation?
RFC 4226 recommends a minimum secret length of 128 bits (16 bytes), but most modern implementations use 160 bits (20 bytes) as the standard because it matches the HMAC-SHA1 output size. Using 20 bytes provides 160 bits of entropy, making brute-force attacks against the secret computationally infeasible. Some implementations support longer secrets of 32 or 64 bytes for use with HMAC-SHA256 or HMAC-SHA512, though this adds minimal practical security for TOTP purposes since the token space (6 digits) remains the bottleneck. The secret is typically encoded in Base32 format for QR code provisioning, which produces a 32-character string for a 20-byte secret. Never use predictable or short secrets, as the entire security model depends on the secret remaining unknown to attackers.
How vulnerable is TOTP to brute-force attacks?
With standard 6-digit TOTP codes, an attacker has a 1 in 1,000,000 chance of guessing correctly for each attempt within a single time step. With a verification window of 1, this improves to 3 in 1,000,000 (0.0003%). Rate limiting is the primary defense, as most systems lock accounts after 3-5 failed attempts. Without rate limiting, an attacker making 3 attempts per 30-second window would need roughly 100,000 windows (about 35 days of continuous attempts) for a 50% chance of success. Eight-digit codes provide 100x more security at 1 in 100,000,000 odds per attempt. The real-world attack surface is more often phishing (tricking users into revealing current codes) or SIM swapping for SMS-based codes rather than brute force against proper TOTP implementations.
What happens when the server and client clocks are out of sync?
Clock desynchronization is the most common cause of TOTP authentication failures. If the server and client clocks differ by more than the verification window allows, valid codes will be rejected even though they were correctly generated. Most TOTP implementations handle this by accepting codes from adjacent time steps (the verification window). Advanced implementations track the observed clock offset for each user by recording which time step the last successful code corresponded to, then applying that offset for future verifications. NTP (Network Time Protocol) synchronization on the server side is essential, and most authenticator apps rely on the device operating system clock. Users experiencing persistent TOTP failures should check their device time settings and enable automatic time synchronization.
How does TOTP compare to other two-factor authentication methods?
TOTP offers strong security advantages over SMS-based codes because it is not vulnerable to SIM swapping, SS7 network attacks, or phone number porting fraud. Compared to push-based authentication like Duo or Microsoft Authenticator push notifications, TOTP is more resistant to prompt bombing attacks where attackers repeatedly send push requests hoping the user accidentally approves one. Hardware security keys like YubiKey using FIDO2/WebAuthn provide stronger phishing resistance because they verify the domain cryptographically, which TOTP cannot do. TOTP advantages include working offline without network connectivity, being supported by many free authenticator apps, and having no per-authentication costs. The main weakness of TOTP is phishing vulnerability, where an attacker in real-time relays the code to the target service.
What are the best practices for storing TOTP secrets on the server side?
TOTP secrets must be stored in a way that allows the server to read the plaintext value for verification, unlike passwords which can be stored as one-way hashes. This means encryption at rest is essential. Best practices include encrypting secrets with a key management service (KMS) like AWS KMS or HashiCorp Vault, using envelope encryption where each secret is encrypted with a unique data key that is itself encrypted by a master key. Access to the secrets table should be restricted to only the authentication service with audit logging enabled. Database-level encryption (TDE) provides an additional layer but is insufficient alone since it protects against disk theft but not application-level breaches. Regular key rotation of the master encryption key and secure backup procedures for the secrets database are also critical components of a robust implementation.
Can TOTP be used for purposes beyond user authentication?
While TOTP is primarily known for two-factor authentication, the underlying algorithm has broader applications in security and verification systems. Some services use TOTP-like codes for email verification, password reset confirmation, and transaction authorization in banking applications. API authentication systems sometimes use TOTP as a lightweight alternative to OAuth tokens for machine-to-machine communication where simplicity is valued. IoT devices may use TOTP for periodic authentication handshakes when persistent connections are impractical. The algorithm can also be adapted for generating time-limited access codes for physical security systems, temporary WiFi passwords, and guest access provisioning. However, for most of these use cases, there are often more purpose-built solutions that provide better security guarantees than repurposing TOTP.
What should organizations consider when rolling out TOTP-based two-factor authentication?
Organizations deploying TOTP should plan for several operational considerations beyond the technical implementation. Recovery procedures are essential since users will lose their authenticator devices, and backup codes (typically 8-10 single-use codes) should be generated during enrollment and securely stored by the user. Support volume will increase initially as users adapt to the new workflow, so training materials and clear enrollment instructions reduce friction. Consider supporting multiple authenticator apps rather than mandating a specific one, as this increases adoption rates. Migration planning for users switching devices requires either a re-enrollment flow or backup and restore capabilities. Monitoring should track authentication failure rates to detect both attacks and usability issues. Finally, consider offering a grace period where TOTP is encouraged but not required before making it mandatory, as abrupt enforcement often creates significant support burden.
References
Background & Theory
History
Reviewed for accuracy by Daniel Agrici, Founder & Lead Developer ยท Editorial policy
Related Calculators
๐งฎAI Token Calculator
Calculate ai token with inputs, formulas, and instant results.
๐งฎBandwidth Time Transfer Calculator
Calculate bandwidth time transfer with inputs, formulas, and instant results.
๐งฎDownload Time Calculator
Calculate download time with inputs, formulas, and instant results.
๐งฎThroughput Efficiency Calculator
Calculate throughput efficiency with inputs, formulas, and instant results.
๐งฎBase64encode Decode Calculator
Calculate base64encode decode with inputs, formulas, and instant results.
๐งฎHash Checksum Calculator
Calculate hash checksum with inputs, formulas, and instant results.
๐งฎUrlpercent Encoding Calculator
Calculate urlpercent encoding with inputs, formulas, and instant results.