The inputs
- K, the shared secret, agreed when you scanned the QR code.
- T, the current Unix time (seconds since 1 January 1970, UTC).
- X, the time step, almost always 30 seconds.
The calculation
- Compute the counter C = floor(T / X). It changes once per time step.
- Compute HMAC(K, C) with the agreed hash, SHA-1 by default (SHA-256 and SHA-512 are allowed by the RFC).
- Apply HOTP’s dynamic truncation (RFC 4226): the low 4 bits of the last byte choose an offset, and the 4 bytes at that offset form a 31-bit number.
- Take that number modulo 10digits, usually 106, and pad with leading zeros.
The server does the same and compares. TOTP is HOTP with the counter derived from the clock.
What it explains
- Clock drift. A phone a minute off is two steps away. Servers usually accept a small window (RFC 6238 recommends at most one step back for network delay), so set the clock to automatic.
- Settings mismatch. A different algorithm, digit count or period gives a different number from the same key.
- Offline operation. Nothing travels between the app and the server; both just know the time.
- Replay. Good servers reject a code that was already used in the same step.
You can check your own results against the published examples on the test vectors page, or run the calculation with our TOTP code generator.