How 2FA authenticator codes work: TOTP and HOTP explained step by step

How a shared secret and the clock make a new 6-digit code every 30 seconds, why phone and server agree without talking, and why codes get rejected.

Open the 2FA code tester Free, in your browser, nothing uploaded.

The six digits in an authenticator app look random, but your phone and the website work them out separately, from the same secret and the same clock, and arrive at the same number without ever contacting each other. This guide walks through that recipe one step at a time, for developers adding two-factor login, support staff answering “my code doesn’t work”, and anyone curious about what the app really does. The heart of it is an HMAC, which you can try on its own with HMAC and bcrypt; strong backup passwords come from password generator; and the setup QR code a site shows can be read with QR and barcode reader (only ever for a test account).

What it is

When you turn on two-factor login, the site makes a random secret, usually 20 bytes, and shows it as a QR code or as Base32 text: the letters A to Z and digits 2 to 7, five bits per character. Your app stores it. From then on, both sides run the same steps.

  1. Counter. For TOTP (time-based one-time password), the counter is the number of 30-second steps since 1 January 1970 UTC: counter = ⌊Unix time ÷ 30⌋. For HOTP (counter-based), it is a number that goes up by one each time a code is used.
  2. HMAC. The counter is written as 8 bytes and signed with the secret: HMAC-SHA-1(secret, counter), giving 20 bytes. An HMAC is a keyed hash: without the secret, the output cannot be predicted.
  3. Dynamic truncation. The last 4 bits of the last byte give an offset from 0 to 15. Four bytes are read from that offset, the top bit is cleared, and the result is a number up to about 2.1 billion.
  4. Digits. That number mod 1,000,000 gives the 6-digit code, padded with leading zeros.

Since both sides know the secret and the time, both get the same code. To allow for clocks a few seconds apart, servers usually also accept the code from one step before and one step after.

How to use it

  1. Open the 2FA code tester and paste a test secret in Base32, or a whole otpauth:// link, which fills in the settings.
  2. Choose Time-based (TOTP) or Counter-based (HOTP), and set the hash, digits and step (or the counter for HOTP) to match the service.
  3. Read the current code, the time bar and the next code.
  4. For a repeatable result, type a Unix time in the fixed-time box.
  5. Type a code into Check a code you were given to see whether it matches now, one step before or one step after.

A worked example

We made a test secret from the 20 characters fibub-demo-key-2026!. In Base32 it is MZUWE5LCFVSGK3LPFVVWK6JNGIYDENRB (32 characters, since 20 bytes × 8 bits ÷ 5 = 32).

Take the moment 1 January 2026, 00:00:00 UTC, which is Unix time 1,767,225,600.

  • Counter: 1,767,225,600 ÷ 30 = 58,907,520 exactly.
  • HMAC-SHA-1 of that counter with the secret begins 85f8053c0610c76f… and ends in byte a4.
  • The last 4 bits of a4 are 4, so read bytes 4 to 7: 06 10 c7 6f, which is 101,762,927.
  • 101,762,927 mod 1,000,000 = 762927.

That code holds from second 0 to second 29 of the step. Thirty seconds earlier the code was 181985; thirty seconds later it is 936336. A server with a window of one step accepts all three at midnight exactly. With SHA-256 instead of SHA-1 the code would be 441676, which is why the hash setting must match the app. As HOTP with counter 0 and 1, the same secret gives 560424 and 724158.

Checked with the tools

The codes were worked out with the tool’s own functions (src/lib/sec/otp.ts) in Node: base32Encode() for the secret, totpCounter() and totp() for the times, and hotp() for the counters. The 762927 code was confirmed a second way with Node’s own HMAC, which gave the same bytes and offset. The same code also reproduces the RFC 6238 test value 94287082 at time 59 seconds (8 digits, SHA-1).

Where it is used

  • Building a login. Developers check that their server and an authenticator app agree before going live.
  • Support desks. “Code rejected” is nearly always a clock or settings mismatch, which the tester shows quickly.
  • Hardware tokens. Some key-fob tokens use HOTP, where the counter moves on each button press.
  • Security lessons. The four steps are a clear example of hashing, keys and time used together.

Common mistakes

  • A wrong clock. A server or PC a minute off fails every code. Turn on automatic time.
  • Mismatched settings. SHA-256, 8 digits or a 60-second step in the server but defaults in the app gives different codes.
  • Confusing 0 and O, or 1 and I. Base32 has no 0, 1, 8 or 9; a typed secret with those is wrong.
  • Sharing real secrets. Anyone with the secret can make your codes forever. Test with a throwaway account.

Questions people ask

Does the app need the internet? No. It needs only the secret and a correct clock.

Why 30 seconds? It is the default in RFC 6238: short enough that a stolen code soon expires, long enough to type.

Can a code be reused? Within its step it is the same number, so careful servers remember the last code accepted and refuse it twice.

Sources

  • TOTP: RFC 6238 (IETF, 2011), including its published test vectors. HOTP: RFC 4226 (IETF, 2005). The otpauth:// link layout follows the Google Authenticator key URI format. Checked 2026-12-05.
  • HMAC: RFC 2104. Base32: RFC 4648.

If you are an author or publisher and want a credit or a passage changed or removed, tell us and we will act quickly.

Tools in this article

Read next