What Is TOTP?

How time-based one-time passwords work: the shared secret, the 30-second window, clock drift, and how TOTP differs from HOTP.

Last updated: Oct 7, 2026 14:03:22

TOTP stands for Time-based One-Time Password: a short code derived from a shared secret and the current time, defined in RFC 6238. It is what authenticator apps show you, and it works with no network connection because both sides compute the same number from the same two inputs.

This page is about why the design looks the way it does — why 30 seconds, why six digits, why the secret is Base32, and what the scheme does and does not protect against. If you want the arithmetic, the TOTP generator exposes every parameter and shows a worked example; this page is the reasoning behind it.

The idea in one paragraph

At enrolment, a server generates a random secret and gives it to your device once. From then on, neither side needs to talk to the other: each computes HMAC over the current time step using that secret, truncates the result to a handful of digits, and compares. If the digits match, you have proved you hold the secret — without ever transmitting it.

The elegance is that the secret is the only long-lived value. Everything else — the code, the counter, the epoch — is public and derivable. That is also the weakness: whoever learns the secret can generate valid codes indefinitely, which is why the secret deserves better protection than the codes it produces.

The three inputs

Input Who decides it Effect
Shared secret The server, at enrolment The key. Different secret, completely different codes.
Time step Public: the Unix clock divided by the period Changes every 30 seconds by default. No sync protocol — both sides just need accurate clocks.
Hash function Usually SHA-1, sometimes SHA-256/512 Changes every digit. SHA-1 is not a weakness here; HMAC-SHA-1 remains sound for this use.

Two optional parameters ride along: digit count (6 by default, 8 in some enterprise systems) and period (30 seconds by default, 60 on a few older platforms). Both are carried in the otpauth:// enrolment link, which is why a QR code tells an app everything it needs.

Why 30 seconds, and why that is a compromise

The period is a trade-off between two failure modes:

Thirty seconds is the point where a careful user can read and type six digits comfortably, while an intercepted code decays fast. Services usually accept one step either side (±30 seconds) to absorb clock skew and slow typing; a few accept only the current step, which makes the experience feel brittle even when nothing is wrong.

This is why “the code doesn’t work” is so often a timing problem: with a ±1 window, a device that is 40 seconds behind produces codes that are always exactly one step away from correct.

Why six digits are enough

One million possible values. An attacker submitting guesses online gets one in a million per attempt, and every real service rate-limits or locks after a handful of failures. Combined with a password that must also be correct, six digits put online guessing out of reach.

What six digits do not protect is the secret. If an attacker has the secret, no number of digits helps; the code is a proof of possession, not a defence against a stolen key. Enterprise systems that move to eight digits are usually optimising for policy uniformity, not for a meaningfully different threat model.

TOTP vs HOTP: the counter is the whole difference

TOTP (RFC 6238) HOTP (RFC 4226)
Counter source The clock A number stored on both sides, incremented per use
Failure mode Clock drift Counter desynchronisation (a lost code advances the server’s counter)
Recovery Fix the clock Resync by trying a window of counters
Typical use Apps, consumer accounts Hardware tokens, some legacy VPNs, printed scratch lists

TOTP won the consumer market because it needs no state: there is nothing to desynchronise and nothing to resync. The cost is an absolute dependence on the clock. The HOTP generator covers the counter-based variant if you are dealing with a device that predates apps.

What TOTP protects against, and what it does not

Protects against: password reuse after a breach, credential stuffing, a stolen password used later, and opportunistic account takeover. In all of these the attacker has the password and not the secret.

Does not protect against:

That last category is why phishing-resistant factors exist: passkeys and security keys bind the credential to the site’s origin, so a proxy gets a signature for the wrong domain. TOTP is a large improvement over a password alone, and a step short of phishing-resistant.

Where you will meet it

Google, Microsoft, Apple’s iCloud web login (not the Apple ID factor itself), GitHub, most password managers, Cloudflare and a long tail of SaaS products all offer TOTP. Banks vary: many still default to SMS, some offer an app with TOTP inside, and a few use proprietary challenge systems that are not TOTP at all.

The tell is the enrolment screen: a QR code plus a “Can’t scan it?” link means TOTP. A screen that says “we will send you a code” means SMS. A screen that insists on a specific app and offers no manual key is usually proprietary — see the compatibility notes.

Five things people believe about TOTP

  1. “The codes are random.” They are deterministic: same secret, same second, same six digits, forever. Reinstall the app, restore the secret, and the sequence resumes exactly — which is why a leaked secret is permanent.
  2. “It needs internet.” It needs a clock. The app computes offline; the server is not consulted until you submit.
  3. “It uses SMS as a backup.” Different mechanism entirely — SMS is generated by the server and sent to you, TOTP is computed locally from a shared secret.
  4. “SHA-1 makes it weak.” HMAC-SHA-1 is not the same as plain SHA-1 collisions. No practical attack on TOTP comes from the hash choice; the weaknesses are phishing, secret theft and clock drift.
  5. “Eight digits means safer.” Marginally less guessable, meaningfully more annoying. The threat model that eight digits addresses is largely theoretical for a rate-limited login.

The half-steps between TOTP and passkeys

TOTP is often described as “good enough”, which is accurate but vague. Placed on a ladder:

Step Survives a stolen password Survives real-time phishing
Password only No No
Password + SMS Yes, unless SIM swap No
Password + TOTP Yes No — the code can be relayed
Password + push with number matching Yes No, but harder to proxy convincingly
Passkey / security key Yes Yes — bound to the domain

The jump from TOTP to passkeys is the only one that changes the answer to “can a phishing site replay this?”. Everything below that line raises the cost; the last row changes the property.

The two RFCs worth knowing

RFC 4226 defines HOTP: HMAC-SHA-1, dynamic truncation, a counter, at least 128 bits of secret. RFC 6238 extends it to TOTP by replacing the counter with time. Both are short and readable, and both specify the test vectors you should check an implementation against — the first HOTP values for the standard test secret are 755224, 287082, 359152.

One detail that trips up implementers: the secret is Base32, not Base64. Base32 exists so a key survives being read aloud or typed on a phone — no 0/O or 1/l ambiguity.

Where to go next

If you need a code, the TOTP generator runs entirely in your browser; for the plain six-digit case, the 2FA code generator is simpler. If a code is failing, work through 2FA code not working before changing anything, and if you are deciding whether TOTP is the right factor for an account, what is MFA compares the options.

Frequently asked questions

What does TOTP stand for?

Time-based One-Time Password. It is defined in RFC 6238 and used by almost every authenticator app.

Why do the codes change every 30 seconds?

The code is derived from the current time step. Thirty seconds is a compromise between a window wide enough to type comfortably and narrow enough that a leaked code decays quickly.

Does TOTP need internet access?

No. Both sides compute the same value from the shared secret and their clocks; nothing is transmitted until you submit the code.

Is TOTP the same as 2FA?

No. TOTP is a mechanism; 2FA is the requirement for two factors. TOTP is commonly used as the second factor, but it is not the requirement itself.

What happens if my clock is wrong?

Codes are one step out and every login fails. Anything beyond about 30 seconds of error makes the codes unusable until the clock is corrected.

Can TOTP codes be phished?

Yes. A proxy site can forward the code to the real service within the window. Only domain-bound credentials such as passkeys resist that.