What Is TOTP and Why Do 2FA Codes Expire in 30 Seconds?

Security ยท 7 November 2026 ยท 4 min read ยท Iftikhar Ahmad

The six-digit code on your phone is not random โ€” it is calculated from the time and a secret. Here is how TOTP works, why it expires, and how to use a code generator safely.

Two-factor authentication has become normal: you type your password, then a six-digit code from your phone. The code changes every thirty seconds, and if you are slow it stops working. Why so fast? And why can a website with no internet connection still generate the right number?

The answer is TOTP, short for Time-based One-Time Password. It is simpler than it sounds.

The idea in one sentence

Both your phone and the website share a secret code, and both know the current time. They combine the two with a maths formula. If the results match, you are in.

What is actually shared

When you turn on two-factor authentication, the website shows you a secret โ€” usually a string like JBSWY3DPEHPK3PXP โ€” or a QR code that contains the same string. Your authenticator app stores that secret. From then on, no internet connection is needed, because nothing new is sent. The app just does the maths.

That is why the code works on a plane, in a basement, or with mobile data turned off.

Why thirty seconds?

Every code is valid for a short window, usually 30 seconds. The window exists to limit damage. If someone sees your code over your shoulder, or reads it from a screenshot, it becomes useless very quickly.

Services usually accept the previous and next window as well, which is why a code sometimes still works a few seconds late. If you are more than about a minute late, you will need a fresh one.

What goes into the calculation

  • The secret โ€” a long random value created when you enabled 2FA.
  • The time step โ€” the current time divided into 30-second blocks.
  • An algorithm โ€” most services use SHA-1, some use SHA-256 or SHA-512.
  • Digits โ€” almost always six, sometimes eight.

Change any one of those and the code changes. That is also why a code from a different service will never work on another one.

Why the code looks random but is not

The formula produces a large number, and the app keeps the last few digits. To a human it looks random. To the server, which has the same secret and the same clock, it is perfectly predictable. Only someone without the secret cannot guess it.

When a code does not work

  1. Your clock is wrong. This is the most common cause. If your phone is a minute behind, the codes will never match. Turn on automatic time in your phone settings.
  2. You are using an old code. Wait for the next one and type it quickly.
  3. You scanned the wrong QR code. Some services show a setup code and a recovery code. Only the setup secret generates codes.
  4. The algorithm or digit count is different. Rare, but some services use SHA-256 or eight digits. Choose the right option in your app.

Using a browser-based generator

You do not always have your phone at hand, for example when you are working on a desktop with your phone charging in another room. A browser tool can generate the same code from the same secret.

Our two-factor authentication tool accepts a Base32 secret or a full otpauth:// link, supports SHA-1, SHA-256, and SHA-512, and calculates everything inside your browser. Click the large code to copy it, then paste it into the login form.

Safety rule: only use a generator you trust, and only paste your secret into a page you control. Never enter a two-factor secret on a website that a stranger sent you, because that secret is the second half of your account security.

Is 2FA really worth it?

Yes. Passwords leak constantly, and the same password is often reused on several sites. Two-factor authentication blocks almost every password-only attack, including the ones that use a password stolen from another website.

If a service offers SMS codes and app codes, prefer the app. SMS can be intercepted or SIM-swapped; an app secret stays on your device.

The recovery codes you should save

When you enable 2FA, the service gives you a list of one-time recovery codes. Save them somewhere offline โ€” a printed page in a drawer is better than a screenshot in your gallery. Without them, a lost phone can lock you out of your own account.

A quick checklist

  • Turn on 2FA on email, banking, and social accounts first.
  • Use an app rather than SMS where possible.
  • Keep automatic time enabled on your phone.
  • Store recovery codes offline.
  • Never share a code with anyone, even someone claiming to be support.

For protecting the account itself, pair 2FA with a long unique password from our password generator.

Need a code right now? Open the free 2FA code generator, paste your secret, and copy the current six digits. Your secret never leaves your browser.