opyeb totplab

How a TOTP code is actually built

The six digits in your authenticator are arithmetic, not magic. Once you know the order, you can also see why a code fails.

2026-11-29 · about 6 min

The four steps at a glance

A TOTP code is built by ① turning the current time into a time step, ② writing that step as an 8-byte counter, ③ computing an HMAC with the shared secret as the key, and ④ truncating the result into a six-digit number.

The server runs the same calculation with the same secret, so matching values mean the login passes. The secret itself never crosses the network.

The shared secret

The value the server hands you when you enrol an authenticator is the shared secret. It is usually 160 bits (20 bytes), written in Base32 so people can copy it by hand.

That value is the root of every rotating code. Anyone who knows it can compute the next code at any time, so treat it like a password.

Turning time into a step

TOTP divides Unix time by the period (usually 30 seconds) and uses the quotient as the counter: T = floor((now − T0) / X), with T0 = 0 and X = 30 by default.

For example, 1,111,111,109 seconds divided by 30 gives a quotient of 37,037,036 and a remainder of 29, so the code for that moment is the code of step 37,037,036.

Signing with HMAC

The counter is written as 8 big-endian bytes and signed with HMAC-SHA1 using the secret as the key. RFC 6238 also allows SHA-256 and SHA-512, and this tool supports all three.

HMAC produces a completely different result if a single bit of the key or message changes, which is why a code cannot be predicted without the secret.

Dynamic truncation

The low four bits of the last HMAC byte become an offset. Four bytes are read at that offset, the top bit is cleared, and the result is taken modulo 10 to the power of the digit count.

This is the dynamic truncation of RFC 4226. The same secret at a different moment picks a different slice of a different digest.

Six digits and eight digits

The digit count only changes the final modulus. Six digits divide by 10^6 and eight digits by 10^8, so the same HMAC yields different numbers.

If a service expects eight digits and you compute six, the code can never match. Check that the authenticator and the server agree on the digit count first.

What makes HOTP different

HOTP increments a counter — by the user or the server — for each code. It needs no clock, but client and server must agree on the counter, and reusing a counter allows a replay.

TOTP lets time manage the counter. The trade-off is that a wrong device clock produces a wrong code, so servers usually accept one or two neighbouring steps.

Three common misconceptions

First, the server does not store codes; it stores the secret and computes each code. Second, a code should be discarded once used. Third, deleting the authenticator app deletes the secret, so keep recovery codes or a backup.

You can confirm the whole model here by fixing the timestamp and recomputing the same step: the same moment always yields the same code.

Questions in this article

Where is the code stored?

Nowhere. The server and the app compute it fresh from the same secret. Only the secret and the last used counter (or time) are kept.

Is the code always the same at the same time?

Yes. Same secret, algorithm, digit count and step always give the same result. The fixed-time mode on this page lets you check that directly.

Why is SHA-1 still the default?

It is the oldest and every authenticator supports it. HMAC-SHA1 has no known practical attack, but a new service can choose SHA-256.

An example secret is already loaded. Replace it with yours and the code changes on the spot.

Open the workbench
← All guides