Skip to content
OTP Generator

Guide

How TOTP Works: Time-Based One-Time Passwords Explained

How do authenticator apps make 6-digit codes without internet? A plain-English look at TOTP: shared secrets, Base32, 30-second steps and HMAC-SHA1 (RFC 6238).

7 min readUpdated November 12, 2026Fact-checked against RFC 6238

Quick answer

Does my authenticator app need internet to generate codes?

No. TOTP codes are calculated from the secret stored on your device and the device clock. No network connection is needed, which is why codes still work in airplane mode. Internet is only used if the app offers optional cloud backup or sync.

More answers
On this page

The magic trick that is not magic

Your authenticator app shows a new six-digit code every 30 seconds. It works on a plane, in airplane mode, with no signal at all. Yet the website you are logging into somehow knows exactly which code your phone is showing right now. How?

The answer is TOTP, short for Time-based One-Time Password, defined in a public standard called RFC 6238. Your phone and the website never talk to each other when a code is generated. Instead, they both run the same calculation on the same two inputs and arrive at the same answer independently.

Those two inputs are a shared secret and the current time.

Ingredient one: the shared secret

When you turn on 2FA, the website generates a random secret key and shows it to you, usually as a QR code. Scanning that QR code copies the secret into your authenticator app. From that moment, two places know the secret: the website's server and your app.

The secret is typically written in Base32, an encoding that uses only the capital letters A to Z and the digits 2 to 7. Base32 deliberately avoids 0, 1, 8 and 9 so that people typing a key by hand do not confuse them with the letters O, I and B. A typical key looks like JBSWY3DPEHPK3PXP, often longer, sometimes split into groups of four for readability.

The QR code itself is just a link in the otpauth://totp/ format that bundles the secret with a label, the issuer name, and optional settings. You can see this for yourself with our QR code to secret key tool, or build one with the 2FA QR code generator.

This secret is the whole ballgame. Anyone who has it can generate your codes, which is why you should treat it like a password.

Ingredient two: time, cut into 30-second slices

Computers measure time as the number of seconds since 1 January 1970 (UTC), known as Unix time. TOTP divides that number by 30 and throws away the remainder. The result is a time step counter that goes up by one every 30 seconds.

So at any given moment, your phone and the server, if their clocks agree, compute the same counter. When the 30-second window rolls over, the counter goes up by one and both sides produce a new code at the same instant. That is why the countdown ring in your app resets in sync with the website's expectations.

This also explains the most common TOTP problem: if your phone's clock is off by a minute or two, it is calculating codes for the wrong time slice. Our troubleshooting guide covers how to fix that.

Mixing the ingredients: HMAC

Now the app needs to combine the secret and the counter into a code in a way that cannot be reversed. It uses HMAC, a standard way of creating a fingerprint of a message using a secret key. By default TOTP uses HMAC with the SHA-1 hash function.

Think of HMAC as a blender with a lock. Put in the counter, lock it with the secret, and out comes a jumble of 20 bytes. Change the counter by one and the output looks completely different. Without the secret, nobody can predict the output, and seeing past codes does not reveal the secret.

Finally, the app shrinks that 20-byte jumble into a short number humans can type. That step is called dynamic truncation, and it produces the familiar six digits.

A note on SHA-1: it is considered broken for some uses like digital certificates, but those weaknesses do not meaningfully affect HMAC-SHA1 as used in TOTP. Some services choose SHA-256 or SHA-512 anyway, which the standard allows.

The technical version, step by step

For the curious, here is what RFC 6238 (building on HOTP from RFC 4226) actually specifies:

  1. Decode the Base32 secret into raw bytes, K.
  2. Compute the counter T = floor((unixTime - T0) / X), where T0 = 0 and the period X = 30 seconds by default.
  3. Write T as an 8-byte big-endian integer.
  4. Compute HMAC-SHA1(K, T), giving 20 bytes.
  5. Take the low 4 bits of the last byte as an offset. Read 4 bytes starting at that offset and clear the top bit, giving a 31-bit number.
  6. Take that number modulo 10 to the power of the digit count (1,000,000 for 6 digits) and pad with leading zeros.

Defaults are 6 digits, a 30-second period, and SHA-1. Allowed variations include 8 digits, other periods, and SHA-256 or SHA-512, but both sides must agree on every setting. Servers commonly accept a code from one step before or after the current one to tolerate small clock differences and typing delays.

HOTP, the older sibling, uses a counter that increments each time you press a button instead of a clock. Same HMAC and truncation, different counter.

Seeing it in action

The easiest way to understand TOTP is to watch it. Paste a test secret such as JBSWY3DPEHPK3PXP into our TOTP generator and add the same key to your phone's authenticator. The two codes will match and change together. The generator runs entirely in your browser using the Web Crypto API, so nothing is sent to a server.

Our how it works page describes exactly what the site computes locally. For real accounts, keep using a trusted authenticator app or password manager as your main code source, and use browser tools for testing, learning, or emergencies with keys you already control.

Frequently asked questions

Does my authenticator app need internet to generate codes?

No. TOTP codes are calculated from the secret stored on your device and the device clock. No network connection is needed, which is why codes still work in airplane mode. Internet is only used if the app offers optional cloud backup or sync.

Why do TOTP codes change every 30 seconds?

Thirty seconds is the default period in RFC 6238. It is short enough that a stolen code quickly becomes useless, yet long enough for a person to read and type it. Services can choose a different period, but both the app and server must use the same value.

Can someone work out my secret from a code?

Not in any practical way. HMAC is designed so that outputs do not reveal the key, and each code only gives away about 20 bits of information. An attacker would need to guess the entire secret, which for a typical 160-bit key is not feasible.

What is the difference between TOTP and HOTP?

Both use the same HMAC and truncation steps. TOTP uses the current time divided into 30-second steps as its counter, while HOTP uses a counter that increases each time a code is generated. TOTP is far more common for website logins today.

Need a code right now?

Paste your key and get the current 6-digit code in seconds. Free, private and nothing to install.