What is TOTP?

In short
TOTP stands for Time-based One-Time Password. It is a method that creates a short-lived code from a shared secret key and the current time, so a login server and your phone can produce the same six-digit code without talking to each other. The algorithm is defined in RFC 6238.
How it works
TOTP is built on top of HOTP, an older one-time password method defined in RFC 4226. HOTP uses a counter that goes up each time a code is made. TOTP swaps that counter for time. Both sides count how many time steps have passed since a fixed starting point, and that number is fed into the same calculation.
The basic formula is simple. TOTP equals HOTP applied to a value T, where T is the number of time steps since a start time T0. The default time step is 30 seconds, and the default start is the Unix epoch, which is midnight UTC on January 1, 1970. Unix time is the number of seconds that have passed since that moment.
The calculation uses a secret key that both parties already share. The key is usually set up once, when you add an account to an authenticator app. After that, no network call is needed to make a new code. The app and the server each compute the code from the key and the current time step.
Why the codes are short-lived
Because the code changes every time step, a code that a thief copies is useless after a short while. This is the main security benefit of TOTP over a fixed password. The RFC notes that short-lived values are desirable for stronger security.
The server also has to decide how to handle timing gaps. A code made at the end of one step may arrive at the server during the next step. The specification leaves room for the verifier to accept nearby time steps to cope with network delay and clock drift. Developers should choose that window carefully, because a wider window gives an attacker more time.
A simple example
Imagine a user who scans a QR code on a website with an authenticator app. The site and the app now share a secret. When the user logs in, the site asks for the current code. The app computes it from the secret and the current time, and the user types it in. The server runs the same calculation and compares the two results.
In pseudocode, the flow looks like this:
- Work out T = floor((current Unix time - T0) / 30).
- Compute HMAC-SHA-1 of the secret key and T.
- Truncate the result to a short decimal code, usually six digits.
- Compare the code the user typed with the one the server computed.
The RFC also allows HMAC-SHA-256 or HMAC-SHA-512 in place of SHA-1, so some systems use stronger hashes.
Common mistakes
The first common mistake is clock drift. TOTP depends on both sides agreeing on the current time. If a phone clock is several minutes off, codes will fail. The specification says the prover and verifier must know the current Unix time, and it notes that clock synchronization matters for the precision of the time used.
The second mistake is weak keys. The RFC recommends that keys be random, and that they be at least as long as the HMAC output. Keys built from guessable values are a poor choice.
The third mistake is sending secrets over an insecure channel. The RFC recommends secure transport such as TLS for all communication, and recommends storing keys encrypted and exposing them only when needed.
Alternatives
HOTP is the counter-based version of the same idea. It works well when time is unreliable, but codes stay valid until used, so it is easier to misuse. Hardware tokens and push-based approval are other options. Each has trade-offs in cost, convenience and security. TOTP is popular because many apps and servers support it, which makes it easy to adopt for two-factor authentication.
FAQ
Is TOTP the same as HOTP?
No, but they are closely related. TOTP uses the current time as the moving factor, while HOTP uses an event counter that increases with each code.
How often does a TOTP code change?
The default time step in the specification is 30 seconds. Each new step produces a new code from the same secret key.
Why do TOTP codes stop working when my phone clock is wrong?
TOTP depends on both sides computing the same time step. If the phone clock is off, its step number differs from the server's, so the codes do not match.
Does TOTP need an internet connection to generate codes?
No. After the shared secret is set up, the app computes codes locally from the key and the current time.
Sources
This page is based on the official documentation below. Read it for full detail.
TOTP — official documentation ↗