Security Tools

How the 6-digit code in your authenticator app works (TOTP explained)

Your phone shows a six-digit code that changes every 30 seconds and the website somehow knows it, with no internet connection between the two. The trick is a shared secret and a shared clock. This guide walks through exactly what happens, how safe it is, and where it still fails.

By the Toolcloak team8 October 20266 min read
In this article
  1. The two ingredients
  2. From time to a counter
  3. From counter and secret to six digits
  4. How safe is a six-digit code?
  5. TOTP, SMS codes and passkeys
  6. Practical habits

When you turn on two-factor authentication, a site shows you a QR code. You scan it once with an authenticator app, and from then on the app displays a six-digit number that changes twice a minute. You type it in, and you are let through. The app never talks to the site. So how do both sides arrive at the same number? The answer is a small, elegant recipe called TOTP.

The two ingredients

TOTP, defined in an open standard (RFC 6238), needs only two things that both sides already have: a secret key and the clock. The secret is a random string chosen by the server when you set up 2FA. The QR code you scan is just that secret, plus a label for the account, written in a way the app can read. The app stores it and the server stores it. That is the only moment the secret travels.

The clock is the second ingredient. Phones and servers keep time closely synchronised through the internet, so both can agree on “what time is it now” to within a second or two without talking to each other.

What each side holds
Your phoneSecret + clock
  • Stored the secret when you scanned the QR
  • Reads the current time from the phone
  • Works with no signal at all
The websiteSecret + clock
  • Stored the same secret for your account
  • Reads the current time from the server
  • Computes the code it expects and compares

Same inputs on both sides give the same output. That is why the app works in airplane mode.

Paste a secret and see the live code and the seconds left. Use test secrets only, never your real account secret on a site you do not trust.Open TOTP Code Generator

From time to a counter

The first step is to turn the time into a whole number that changes only every 30 seconds. Take the Unix time (the seconds since 1 January 1970; see Unix time explained) and divide by 30, dropping the fraction. The result is the counter.

Three moments and the counter they produce
Unix timeCounterSame code as the first?
1,700,000,01056,666,667Yes (start)
1,700,000,03956,666,667Yes (29 s later)
1,700,000,04056,666,668No, a new code

The ringed cell is the first second of the next step. Every moment inside a step gives the same counter, so the code stays steady for 30 seconds.

One 30-second step
  1. 0 s
    A new code appearsThe counter just ticked up, so the app computes a fresh code.
  2. 15 s
    HalfwayThe same code is shown. You have comfortable time to type it.
  3. 25 s
    Time is shortMany apps show the ring turning red. Wait for the next code if you are slow.
  4. 30 s
    Next stepThe counter becomes N + 1 and the old code stops being the current one.

From counter and secret to six digits

Now the counter and the secret are mixed using HMAC, a keyed hash (the idea is explained in what a hash is). A hash scrambles its input so thoroughly that even a tiny change gives a completely different output, and HMAC adds the secret so that only someone who knows it can produce the same result. The output is a long block of bytes, much too long to type.

So the standard shrinks it with a step called dynamic truncation: look at the last byte, use its low four bits as a starting position, take four bytes from that position, drop the top bit, and read them as a number. Finally take that number modulo 1,000,000 (the remainder after dividing) to get exactly six digits, padding with zeros at the front if needed.

The whole recipe
  1. 1Read the clockUnix time, for example 1,700,000,010.
  2. 2Make the counterDivide by 30 and drop the fraction: 56,666,667.
  3. 3Mix with the secretRun HMAC over the counter using the shared secret (SHA-1 by default in most apps).
  4. 4TruncatePick four bytes from a position chosen by the last byte of the result.
  5. 5Keep six digitsTake the number modulo 1,000,000 and pad with leading zeros.

You never need to do this by hand. The point is that every step is public and repeatable, and only the secret is private.

How safe is a six-digit code?

There are only 1,000,000 possible six-digit codes, so a blind guess has a 1 in a million chance. That sounds weak, and by itself it would be, but the code is never the only barrier. It works together with a password, and servers limit attempts. Because clocks drift slightly, servers usually accept the previous, current and next step, so three codes are valid at once: the odds of a lucky guess become about 3 in a million.

Odds of guessing blindly
1 in 1,000,000One try, one valid codeSix digits, 10⁶ possibilities
1 in 333,333One try, three valid codesWith a ±1 step grace window
1 in 66,667Five tries in a rowWithout a lockout, 5 × 3 in a million

Real services lock the account or slow down after a handful of wrong codes, so an attacker cannot simply try hundreds of thousands of codes.

What TOTP protects against, and what it does not
Stolen passwordA leaked password alone is no longer enough. The attacker also needs the code.
Reused password in a breachOther sites’ leaks cannot unlock this account without your phone.
Real-time phishingA fake site can ask for your code and pass it on within 30 seconds. TOTP does not stop this.
Lost or wiped phoneWithout a backup, you may be locked out yourself. Save the recovery codes.

TOTP is a strong everyday layer, not a perfect one. Hardware keys and passkeys, which are tied to the real site address, resist phishing better.

TOTP, SMS codes and passkeys

Text-message codes are easy to use but depend on your phone number, which can be hijacked through SIM-swap fraud or intercepted, and on the network being available. TOTP codes are generated on your device and need no signal. Passkeys and hardware security keys go further by cryptographically tying the login to the site, so a copied page cannot trick you.

Rough comparison of second factors
MethodWorks offlineHard to phishEasy to set up
SMS codeNoNoVery easy
Authenticator app (TOTP)YesNoEasy
Passkey or hardware keyYesYesModerate

Darker means the stronger or easier result in that column. This is a simplified overview: individual services implement each method differently.

None of these is useless. Any second factor is far better than none, because most account takeovers rely on a stolen or guessed password. If you can choose, use an authenticator app over SMS and a passkey over an app. For the password side of the equation, see password entropy.

Practical habits

  1. Save the recovery codes when you set up 2FA, and keep them somewhere other than your phone.
  2. Back up the secret if your app offers encrypted backup, or note the setup key in a password manager.
  3. Keep your phone’s clock automatic. If the codes are rejected, a wrong clock is the first thing to check.
  4. Never share the code, even with someone who says they are from support. A valid code is a key to your account for 30 seconds.
  5. Check the address bar before typing a code into any site.

The cleverness of TOTP is how little it needs: one secret, shared once, and the clock that is already on every device. Try the generator above with a test secret and watch the code change at the 30-second mark. It is the same maths protecting accounts all over the internet.

Questions people ask

What does TOTP stand for?

Time-based One-Time Password. It generates a short code from a shared secret and the current time, defined in RFC 6238.

How does the app know the code without internet?

The app and the server share a secret and both use the clock. They each compute the same code independently, so no connection is needed at login time.

Why does the code change every 30 seconds?

The time is divided into 30-second steps and the code is derived from the step number. When the step number changes, so does the code.

Why does my code say it is invalid?

The most common cause is a wrong phone clock. Turn on automatic date and time. Also check that you typed the current code, not the one that just expired.

Is an authenticator app safer than SMS?

Generally yes. App codes are generated on your device and cannot be intercepted through your phone number. Neither one stops a real-time phishing page.

What happens if I lose my phone?

You need your recovery codes or a backup of the secret to get back in. Save these when you enable 2FA, and keep them away from the phone.