TOTP Generator: How the 6-Digit Code Is Calculated

PublishedToolTOTP / 2FA Authenticator Code Tester

Contents
  1. When You Can't Tell Why the Code Won't Match
  2. The Standard Procedure for Computing the 6 Digits Yourself, No Tool Needed
  3. Common Points of Confusion
  4. The Short Version
  5. Frequently Asked Questions

The 6-digit number that shows up in an app like Google Authenticator or Authy isn't something a server sends you in real time. Your device works it out on its own, using nothing but one secret key and the current time. That's why codes still appear in airplane mode, and why they come out wrong when your device clock is off.

This article explains the order in which those 6 digits are built and what actually goes into a QR code, following the standards (RFC) exactly. It isn't about which authentication method you should choose — it's for readers who want to confirm how it works as verifiable fact.

Summary ① The Base32 secret key is the only secret ingredient that produces the code. The issuer and account are just name tags that appear in the app's list; they don't enter the 6-digit calculation at all. ② The 6 digits come from mixing "the secret key (the key) + the current UTC time divided by 30, as a counter (the message)" through HMAC-SHA1, then truncating the result and taking the remainder after dividing by 1,000,000. ③ Most mismatches come down to a device clock that's off, or characters that aren't in Base32 (0, 1, 8, 9, and so on) ending up in the secret key.

When You Can't Tell Why the Code Won't Match

Picture switching to a new phone and moving your authenticator app over. If you can't show the QR code again, you land on an "enter the setup key manually" screen, and the app asks for three fields: secret key, issuer, and account name.

When you don't know what goes where, or which of the three actually changes the code, you just fill them in by feel. Then registration finishes and "invalid code" appears, and you can't tell whether the secret key is wrong or the clock is off.

The problem is that the process behind those 6 digits is hidden inside the app. Once you've seen how it works for yourself, you can pinpoint which step went wrong.

The Standard Procedure for Computing the 6 Digits Yourself, No Tool Needed

TOTP is a published standard. The rules for generating the code are written out in the RFC documents, so if you want, you can reproduce the same value by hand or with a command.

What Each of the Three Inputs Actually Means

First you need to sort out what the three fields the app asks for are for.

  • Base32 secret key (Secret): The original ingredient shared by the server and your device. The only secret value that goes into the calculation.
  • Issuer: The service name shown in the app's list, like "Google" or "GitHub".
  • Account: The email or username that tells accounts apart when one service has several.

The key point is that the last two values play no part in the calculation at all. Change the name tags however you like, and the 6 digits stay the same.

The Five Steps That Produce the 6 Digits

This is the order set by RFC 4226 (HOTP) and RFC 6238 (TOTP).

  1. Base32 decoding: Turn the secret key back into bytes. Base32 (RFC 4648) uses only 32 characters, A–Z and 2–7, packed 5 bits at a time, so you unpack them back into 8-bit units.
  2. Time into a counter: The counter is the number of seconds since January 1, 1970 UTC (Unix time) divided by 30, keeping the quotient. RFC 6238's default interval is 30 seconds. Write that value as 8 bytes.
  3. HMAC-SHA1: Use the secret key as the key and the 8-byte counter as the message, compute HMAC-SHA1, and you get a 20-byte hash.
  4. Dynamic truncation: Take the low 4 bits of the last byte of the 20 bytes to pick a starting position (0–15), pull 4 bytes from there, drop the most significant bit, and you have a 31-bit integer.
  5. Cut to 6 digits: The remainder of that integer divided by 1,000,000 is your 6 digits. Pad with leading zeros if it comes up short.

Same secret key and same 30-second window means the same result no matter where in the world it's computed. That's why the server's number and your app's number match.

What's Inside a QR Code

A QR code isn't a picture — it holds a single line of URI. The format looks like this.

otpauth://totp/Issuer:Account?secret=Secret&issuer=Issuer

The only value here that produces the code is secret. The Issuer:Account prefix and the issuer parameter are labels the app uses to show a name in its list. So scanning the QR code and typing the secret key by hand give the same result.

Verifying It from the Command Line

To reproduce it yourself, the open-source command-line tool oathtool (oath-toolkit) does it in one line.

oathtool --totp -b JBSWY3DPEHPK3PXP

-b means the secret key is Base32, and the output is the 6 digits for this exact moment. Run it again 30 seconds later and the value changes. You can also code the same five steps in a few lines with Python's standard hmac and hashlib libraries.

Let's check it with our free tool, TOTP / 2FA Authenticator Code Tester — generate a Base32 secret, render a QR code for Google Authenticator, and watch live 6-digit codes.

TOTP / 2FA Authenticator Code Tester

Generate a Base32 secret and QR code and watch the 6-digit token recalculate every 30 seconds, right in your browser with no sign-up

Common Points of Confusion

"If I change the issuer or account, the code should change too." It won't. As shown above, the 6 digits are determined only by the secret key and the time. Here's the three inputs' roles again.

InputRoleAffects the 6 digits
Base32 secret keyThe only secret ingredient (the key) in the calculationYes
IssuerThe service name tag shown in the app's listNo
AccountThe label that tells accounts apartNo

"My codes keep coming out wrong." The most common cause is clock drift. The counter comes from dividing your device's UTC time by 30, so if your phone clock is tens of seconds or more off from the server, the two sides use different counters and produce different codes. Some verification servers accept a small window on either side, but a large gap still fails.

"I entered the secret key and got an error." You included a character Base32 doesn't have. Base32 uses only A–Z and 2–7, so a 0, 1, 8, 9, or special character blocks the decoding step. Case differences and spaces are usually cleaned up automatically, but characters that don't exist can't be recovered.

CategoryCharacters
Characters Base32 usesA–Z, 2–7
Characters it doesn't use (error if entered)0, 1, 8, 9, and special characters

"Why does it change every 30 seconds?" Because the counter is "time ÷ 30, quotient." The moment you cross into a new 30-second window, the quotient goes up by 1, and since the counter changed, the HMAC result and the 6 digits change completely. The gauge bar on screen shows the seconds left until the current window ends.

The Short Version

  • The 6 digits are calculated, not sent by a server. The only ingredients are the secret key and the current UTC time.
  • The standard procedure has five steps. Base32 decoding → time into a 30-second counter → HMAC-SHA1 → dynamic truncation → remainder after dividing by 1,000,000.
  • Only the secret key and the time change the code. The issuer and account are name tags, so they never enter the calculation.
  • A QR code is one line of otpauth:// URI, and the only value actually used inside it is secret.
  • Mismatches usually have two causes. A device clock that's off, or a character Base32 doesn't allow.

The tool below runs these five steps live in your browser and shows you the QR code, the 6 digits, and the time remaining the moment you enter a secret key.

TOTP / 2FA Authenticator Code Tester

Generate a Base32 secret and QR code and watch the 6-digit token recalculate every 30 seconds, right in your browser with no sign-up

Frequently Asked Questions

Can I check the 6 digits without registering in an app?

Yes. With a valid Base32 secret key, your browser runs the HMAC-SHA1 calculation right there and shows the 6 digits on a 30-second cycle. Your key is never sent to a server; the math happens on screen.

Why do the numbers match exactly across devices and servers?

Because TOTP uses only a fixed secret key and Coordinated Universal Time (UTC), which is shared worldwide. Same key and same 30-second window means the same result. That's also why codes drift apart when devices' clocks aren't synced.

Can the secret key have lowercase letters or spaces?

Usually that's fine. Standard processing turns lowercase letters into uppercase and strips spaces before decoding. But characters that Base32 doesn't have at all, like 0, 1, 8, and 9, will throw an error at that step.

Can I enter a code that appeared just before the 30 seconds ran out?

Once the window rolls over, the counter changes and so does the code. That said, many verification servers are built to accept the previous window's code for a short time, so a code can still go through if it expires while you're typing. How much leeway you get varies by server.

This article was adapted from our Korean original. Read the Korean original

Related articles

  1. Unicode Escape Conversion: \uXXXX, U+ Code Points & HTML EntitiesConvert Hangul, emoji, and symbols to \uXXXX, U+ code points, or HTML entities and back — plus why emoji break into surrogate pairs.
  2. URL Normalization: Remove utm Parameters and Get Canonical LinksSee exactly which tracking parameters get stripped (utm_*, fbclid, gclid, ref), how URLs are normalized, and how to read the applied changes list.
  3. URL Encoding Explained: Why Spaces Become %20Learn why URL encoding turns spaces into %20 and non-English characters into codes like %EA%B0%80, how UTF-8 bytes work, and which mistakes to avoid.
  4. Extract URL Path Segments and Convert Them to RoutesSee how a URL splits into protocol, host, path segments, and query, which pieces get indexes, and when a segment becomes an Express :param or Next.js [param].
  5. URL Encoding Explained: Percent Encoding and Why Text BreaksSee why URLs can't hold spaces or non-ASCII text, how percent encoding turns them into %XX bytes, and why encodeURI and encodeURIComponent give different results.
  6. User-Agent Header Explained: Parsing, Devices, and Bot CrawlersSee how the User-Agent header is built, how to read it with navigator.userAgent or regex, and how to test browser, device, and bot strings.

Related tools