What is TOTP? A code from a secret every 30 seconds
What TOTP is, how RFC 6238 turns a shared secret and the clock into a code every 30 seconds, with a worked example and the 18 test vectors checked.

Davide G. · Founder, Scalebrowser
Published Nov 27, 2026 · 5 min read
Short answer

Every authenticator app shows the same thing: a six-digit number and a small clock running down. Behind it is one shared secret, stored once when you scanned the QR code, and a fixed piece of arithmetic that turns the current time into digits. The app and the server never talk to each other, and they still agree, because both do the same arithmetic on the same secret.
What is TOTP?
TOTP is a one-time password computed from a shared secret and the current time, defined in RFC 6238, published in May 2011. It is a variant of HOTP from RFC 4226, published in December 2005, in which "a value T, derived from a time reference and a time step, replaces the counter C". The two differ only in what moves.
| Property | HOTP | TOTP |
|---|---|---|
| Moving factor | a counter raised on every use | the number of time steps since 1 January 1970 |
| Standard | RFC 4226, December 2005 | RFC 6238, May 2011 |
| The code changes | when the counter moves | every time step, 30 seconds by default |
| Digits | at least 6 required | inherited from HOTP; the RFC's examples use 8 |
| Hash | HMAC-SHA-1 | HMAC-SHA-1, SHA-256 or SHA-512 |
The time step is written in the RFC as "default value X = 30 seconds". A server and an app that share the secret and both know the time produce the same code without any message between them, which is why TOTP works offline on a phone.
How is a TOTP code calculated from a secret?
A TOTP code is calculated in four steps: count the time steps, hash the count with the secret, pick 4 bytes out of the hash, and reduce them to the wanted number of digits. The RFC's own test secret, the ASCII string "12345678901234567890", at Unix time 59 shows every step.
- Count the steps. 59 divided by 30, rounded down, is 1. The RFC gives the same example: "T = 1 if the current Unix time is 59 seconds, and T = 2 if the current Unix time is 60 seconds."
- Hash. HMAC-SHA-1 of the step, written as 8 bytes, keyed with the secret, gives the 20 bytes
75a48a19d4cbe100644e8ac1397eea747a2d33ab. - Truncate. The last byte ends in hex
b, so the offset is 11. The 4 bytes from offset 11 arec1397eea, and with the top bit cleared they are the number 1,094,287,082. - Reduce. That number modulo 10^8 is 94287082, the value in the RFC's table; modulo 10^6 it is 287082, the six-digit code an app would show.
The whole calculation fits in a few lines of Python's standard library.
import hmac, hashlib, struct
def totp(secret: bytes, now: int, digits: int = 6, step: int = 30) -> str:
counter = now // step
mac = hmac.new(secret, struct.pack(">Q", counter), hashlib.sha1).digest()
offset = mac[-1] & 0x0F
number = struct.unpack(">I", mac[offset:offset + 4])[0] & 0x7FFFFFFF
return str(number % 10**digits).zfill(digits)
print(totp(b"12345678901234567890", 59, digits=8)) # 94287082RFC 4226 explains the cleared top bit: masking it avoids "confusion about signed vs. unsigned modulo computations" across processors.
Which test vectors prove an implementation?
The 18 test vectors in Appendix B of RFC 6238 prove an implementation, because they fix the output for six timestamps under all three hash functions. We ran the function above, extended to SHA-256 and SHA-512 with the RFC's longer test keys, against every row.
| Unix time | SHA-1 | SHA-256 | SHA-512 |
|---|---|---|---|
| 59 | 94287082 | 46119246 | 90693936 |
| 1111111109 | 07081804 | 68084774 | 25091201 |
| 1111111111 | 14050471 | 67062674 | 99943326 |
| 1234567890 | 89005924 | 91819424 | 93441116 |
| 2000000000 | 69279037 | 90698825 | 38618901 |
| 20000000000 | 65353130 | 77737706 | 47863826 |
All 18 values match the RFC's table. The last row lies in the year 2603 and is there on purpose: the RFC says an implementation "MUST support a time value T larger than a 32-bit integer when it is beyond the year 2038".
How long is a TOTP code valid?
A TOTP code is valid for one time step, 30 seconds by default, plus whatever window of delay the server allows. RFC 6238 recommends "that at most one time step is allowed as the network delay", so a code usually passes for 30 to 60 seconds depending on when in its step it was shown. For clocks that drift, the RFC describes accepting "two time steps backward", which covers a drift of "around 89 seconds".
| Parameter | RFC 6238 | Google key URI default |
|---|---|---|
| Time step | 30 seconds by default | 30 seconds |
| Digits | not set; 6 minimum from RFC 4226 | 6, or 8 |
| Hash | SHA-1, SHA-256 or SHA-512 allowed | SHA-1 |
| Reuse | "MUST NOT accept the second attempt" | not covered |
Google's key URI format, the text inside most setup QR codes, lists these parameters and warns that Google Authenticator ignores several of them: "the algorithm parameter is ignored by the Google Authenticator implementations", and so is the period. A site that sets SHA-256 or a 60-second period in its QR code can therefore produce codes that app never shows.
Where does an agent keep the secret?
An agent keeps the TOTP secret in its profile, next to the login it belongs to, and never in the model's context. RFC 4226 requires the shared secret to be "at least 128 bits" and recommends 160; in the QR code it travels as Base32 text, the "setup key" a site offers to type in by hand. Stored once, it lets the profile compute the current code at the moment a sign-in needs it, while the agent only names the code field. TOTP is the most computable of the second factors an agent meets, since it needs no message to arrive, and the credentials page of the documentation shows how the setup key is stored against a profile.
What is the difference between TOTP and OTP?
OTP is the general term for any password valid once, and TOTP is the kind whose moving factor is the clock. HOTP counts uses instead, and codes sent by SMS or email are one-time passwords generated on the server and delivered, not computed on your side.