Scalebrowser

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

TOTP, the time-based one-time password, derives a short code from a shared secret and the current time. Under RFC 6238 the time is counted in steps of 30 seconds by default, each step is hashed with the secret and the result is cut down to a number of digits; RFC 4226, on which TOTP builds, requires at least 6. The TOTP RFC itself sets no digit count, and its own test vectors use 8. App and server need only the same secret and clocks that agree within a step. We recomputed all 18 of the RFC's test vectors on 27 September 2026, and every one matched.
30 s, a code from a secret, beside an app window showing a six-digit code

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.

PropertyHOTPTOTP
Moving factora counter raised on every usethe number of time steps since 1 January 1970
StandardRFC 4226, December 2005RFC 6238, May 2011
The code changeswhen the counter movesevery time step, 30 seconds by default
Digitsat least 6 requiredinherited from HOTP; the RFC's examples use 8
HashHMAC-SHA-1HMAC-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.

  1. 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."
  2. Hash. HMAC-SHA-1 of the step, written as 8 bytes, keyed with the secret, gives the 20 bytes 75a48a19d4cbe100644e8ac1397eea747a2d33ab.
  3. Truncate. The last byte ends in hex b, so the offset is 11. The 4 bytes from offset 11 are c1397eea, and with the top bit cleared they are the number 1,094,287,082.
  4. 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.

python
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))  # 94287082

RFC 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 timeSHA-1SHA-256SHA-512
59942870824611924690693936
1111111109070818046808477425091201
1111111111140504716706267499943326
1234567890890059249181942493441116
2000000000692790379069882538618901
20000000000653531307773770647863826
Source: RFC 6238 Appendix B recomputed with Python's hmac module, 18 vectors, 27 September 2026

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".

ParameterRFC 6238Google key URI default
Time step30 seconds by default30 seconds
Digitsnot set; 6 minimum from RFC 42266, or 8
HashSHA-1, SHA-256 or SHA-512 allowedSHA-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.

Filed in Sign in, one of 8 articles

Real browser profiles for your AI agent, on your PC.

Each profile keeps its own identity, logins and proxy. Your agent drives it over MCP and signs in without seeing a password.

Download free for Windows

Free plan with 3 profiles · No credit card · Runs on your PC

Written by

Davide G.

Founder, Scalebrowser

Builds Scalebrowser, browser automation for the agentic era that runs on your own machine. Measures every change a web page could observe against a real browser before it ships, and writes up the ones that turned out wrong.

All articles by Davide G.

More in Sign in

All 8 articles in Sign in