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.
| Step | Value at one moment | What it means |
| Time / period | 58812331 | The step number, the same everywhere |
| Counter bytes | 00 00 00 00 03 81 4c 2b | That number, eight bytes, big-endian |
| HMAC-SHA-1 | 1f 86 9a … | Twenty bytes |
| Offset | 0e | The low nibble of the last byte |
| Code | 418 902 | The 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.