Pikkola TOTP, tool No. 80

The code, and the arithmetic behind it.

A six-digit code is the end of a short chain: a base32 secret, a counter built from the clock, an HMAC and a truncation. This page shows the code and every step that made it, which is what makes it useful when a code is rejected and both sides are sure the secret is right.

3
hash functions
6-8
digits
0
uploads

The bench

A clock, a secret and five steps

The secret stays in this tab and the counter comes from your own clock, so a code that disagrees with an authenticator usually means a clock that disagrees first.

------
next ------
Counter
-
Key bytes
-
HMAC digest
-
Offset
-
Truncated
-
Code
-

Each row is the value the next step reads.

Base32, as every authenticator stores it.

Hash

Five steps, no secret in the middle

TOTP is HOTP with the counter taken from the clock. Every step is deterministic, which is why a disagreement can be found rather than guessed at.

01

Base32, then bytes

An authenticator stores the secret as text because a person has to type it. Base32 uses thirty-two characters and no letter that a hand confuses with a number, and the decoder here ignores the spaces and hyphens that printed secrets carry.

02

The counter is time divided

Unix seconds divided by the period, floored, written as eight big-endian bytes. At thirty seconds the value changes twice a minute, which is the whole of what a period means, and a clock thirty seconds out of step produces the code from the step before.

03

HMAC, then four bytes

The counter is signed with the key and the chosen hash, and the last byte's low nibble says where to cut four bytes out of the twenty. Those four bytes, with the top bit cleared, are the number that becomes the code. Every intermediate value is on the page.

What this page will and will not do

A tool that shows a live secret has to be clear about where that secret goes. Here it goes nowhere, and the list is the whole of the promise.

Part of the work What this page does What it will not do
The secret Keeps it in the tab only, and forgets it when you reload Read from the box, held in memory, never written
The code Copied only when you press copy, and nowhere else Computed with the browser's own HMAC
The clock Fetch a time from a server: a code made with a corrected clock would not be yours Uses this device's clock, and offers no correction
The window Accept a code from any step at all: that is a brute-force surface, not a convenience Checks the step and one either side if you allow it
SHOULD RFC 6238 SHA-1, SHA-256 and SHA-512, six to eight digits Produce a QR code: the URI is text, and any QR encoder draws it
Standards Checks against the RFC's published vectors Claim to be an authenticator: nothing is stored, so nothing survives a reload

A secret pasted here is live. Use this page to understand a code, and an authenticator app to hold the accounts you actually protect.

Field guide

Reading a six-digit code

Two-factor authentication is the part of a login that a password leak cannot satisfy, and the six digits on the screen are the smallest part of it. This is the chain from a secret in a text box to a code a server accepts, and the places it usually breaks.

RFC 4226 first, RFC 6238 after it

HOTP is the older half: a counter is incremented on each use and both sides keep it in step. TOTP replaces the counter with the Unix time divided by a period, which removes the need to remember how many codes have been used and introduces the need for both clocks to agree. Everything else in the two documents is identical, which is why a page can show one and support the other with one extra field.

The truncation is the part people get wrong

The HMAC output is twenty bytes for SHA-1, thirty-two for SHA-256 and sixty-four for SHA-512, and only four of them become the code. The last byte's low nibble is read as an offset from zero to fifteen, four bytes are taken from there, and the top bit of the first of those is cleared so the result is positive in every language's integer type. A code that is right for one implementation and wrong for another is almost always a truncation implemented on the wrong bytes, or a modulo applied to the whole digest.

StepValue at one momentWhat it means
Time / period58812331The step number, the same everywhere
Counter bytes00 00 00 00 03 81 4c 2bThat number, eight bytes, big-endian
HMAC-SHA-11f 86 9a …Twenty bytes
Offset0eThe low nibble of the last byte
Code418 902The truncated number, modulo ten to the digits

Clock drift is the usual failure

Most servers accept the current step and the one before it, which is about thirty seconds of tolerance. A phone whose clock is set by the network is nearly always inside that, and a phone whose clock was set by hand months ago is not. When a code is rejected and the secret is certainly right, comparing the counter on both sides is the fastest diagnosis: the same secret and the same period with a counter one apart produce two entirely different codes, and neither side looks wrong until the numbers are put next to each other.

A base32 secret is not a password

The secret is a key, thirty-two characters of base32 carrying twenty bytes of entropy in the common case, and it is as sensitive as the account's password because it can generate every future code. It is stored in the clear by design in most authenticators, protected by the phone's own encryption, and that is why a screenshot of a QR code is a credential. This page holds one only in memory: a reload forgets it, and nothing is written to storage.

What a code does not protect

A TOTP code that is typed into a page the attacker controls is a code the attacker can use within its period; the protection is against a stolen password, not against a convincing login page. It also does not protect a seed that was copied when the account was set up, and it does not survive a server that accepts a code twice. Passkeys remove the shared secret entirely, which is why they are the better answer where they are offered, and TOTP remains the answer everywhere else.

The other half of a login is a token rather than a code: Pikkola JWT decodes and checks one. For the secret that protects the account itself, Pikkola Pass generates a passphrase worth remembering.

Questions

Is my secret sent anywhere?

No. It is read from the box into memory, decoded and used as an HMAC key by the browser's own crypto, in this tab. It is not written to storage or a cookie, so a reload forgets it, and the only request the page makes is the cookieless page-view count.

Why does my code differ from my phone's?

In order of likelihood: the secret has a character wrong, the period or digits differ from what the site uses, the hash is SHA-256 rather than SHA-1, or the clocks differ. The detail panel shows the counter, so comparing it with the same value on the other side turns a guess into a measurement.

What does allowing one step either side mean?

The check tries the current step, the one before and the one after, which absorbs about thirty seconds of clock drift in either direction. It is what most servers do with a TOTP code. Widening it further is how a six-digit secret is found by trying, so a server should not allow many steps and should also limit attempts.

Can this page replace my authenticator?

No, and it is not built to. Nothing is stored, so a reload loses the secret and the page cannot hand you a code tomorrow. It is a bench for checking that a secret, an algorithm and a clock agree, and for reading what a code is made of.

Why SHA-1, when it is broken?

Because TOTP with SHA-1 is what the ecosystem uses and the choice is written into the QR code every site issues. HMAC-SHA-1 is not affected by the collision attacks that broke plain SHA-1 in signatures; a shared key and a counter are a different problem. New schemes choose SHA-256, and both are supported here because both are in the wild.