TOTP Explained: How One-Time Codes Keep Accounts Safe

TOTP explained in plain terms means understanding the six-digit codes that change every thirty seconds and prove you are who you claim to be. The central question is how a code can be checked without the server ever sending it to you, and the answer is that both sides share a secret and a clock, and each one works out the same code independently.

TOTP, short for Time-based One-Time Password, is the standard behind the codes shown by authenticator apps such as Google Authenticator, Microsoft Authenticator, and Authy. It is the second factor in two-factor authentication for millions of accounts, from email and banking to cloud services and code repositories.

The system is elegant because it needs no network connection at the moment of use: the code is generated on the device, and the server generates the same code, and if they match, the login proceeds. This article explains what TOTP is, how the codes are made, and why it is safer than the alternatives.

Key takeaways

  • TOTP codes are six digits that change every thirty seconds.
  • Both sides share a secret and use the current time to make the code.
  • No network connection is needed to generate a code.
  • The secret is set up once, usually by scanning a QR code.
  • TOTP is safer than SMS codes, which can be intercepted.

What TOTP is

TOTP is a method for generating one-time passwords that are valid only for a short window of time, usually thirty seconds. The method is defined in RFC 6238, published in 2011, and it builds on an earlier standard for counter-based codes, replacing the counter with the current time so that both sides always agree on which code is valid.

The system has two parts: a shared secret, a random string of bytes that is given to the user once, when two-factor authentication is set up, and a clock, which both the user’s device and the server read. The secret is usually delivered as a QR code that the authenticator app scans, or as a string of letters that the user types in, and after that moment the secret is never sent again.

Each code is derived from the secret and the current time, so anyone who knows the secret and the time can compute the code, and anyone who knows neither cannot. That is the whole security of the system: the secret must stay secret, and the clocks must stay roughly in step. Our guide to two-factor authentication explains the wider practice that TOTP is part of, and our guide to cryptography explains the mathematics that makes the codes unforgeable.

How TOTP works

The process starts with the current time, measured as the number of seconds since the first of January 1970, the standard epoch used across computing. That number is divided by the time step, almost always thirty seconds, and the result is a counter that changes once per step. At any moment, the user’s device and the server should compute the same counter, as long as their clocks agree within a step or two.

The counter is then combined with the shared secret using HMAC, a keyed hash function, usually based on SHA-1, though SHA-256 and SHA-512 are also allowed. The result is a twenty-byte value that looks random, and a fixed procedure extracts a six-digit number from it, by taking four bytes at a position chosen by the last byte and reducing the result modulo one million.

The six digits are what the user sees and types in. The server performs the same calculation with its copy of the secret and the current counter, and if the numbers match, the login is accepted. Because the counter changes every thirty seconds, a code that is stolen is useless within a minute, and because the code is derived from the secret rather than sent by the server, there is nothing to intercept in transit.

Servers allow for clock drift by accepting codes from neighbouring time steps as well. A server will usually accept the code for the current step, the one before, and the one after, which covers clocks that are up to a minute out of step. A code that has already been used is often rejected, so that a captured code cannot be replayed even within its valid window.

Setting up TOTP is a one-time exchange. The service shows a QR code that encodes the secret, the account name, and the issuer, and the authenticator app scans it and stores the secret on the device. From then on, the app generates codes without any connection to the service, which is why it works on a plane, in a basement, or anywhere else without a signal.

A phone on a dark desk with its screen showing a glowing green six-digit code and a countdown ring
A phone showing a glowing green six-digit code with a countdown ring shows how TOTP works: a code made from a shared secret and the current time.

Comparing TOTP with other second factors

TOTP is one of several ways to prove identity beyond a password. SMS codes arrive by text message, hardware keys use a physical device, and push notifications ask the user to approve a login, but each suits a different need.

The table below sets out the main contrasts between the leading approaches.

Aspect TOTP SMS codes Hardware key
Needs a network No, works offline Yes, mobile signal No, works offline
Can be intercepted No, nothing is sent Yes, texts can be read No, nothing is sent
Phishing resistant No, codes can be tricked out No Yes, checks the site
Cost Free, any phone Per message, for the service Price of the key
Best for Everyday accounts Users without an app High-value accounts

The practical result is that TOTP is the best balance of security and convenience for most accounts, SMS is the fallback for users who cannot use an app, and hardware keys are the strongest choice for accounts that must resist phishing.

Where TOTP is used

Email and cloud accounts are the largest use. Google, Microsoft, Apple, Amazon, and almost every major service offer TOTP as a second factor, and turning it on is the single most effective step a user can take to protect an account, since a stolen password alone is no longer enough to log in.

Banking and finance use TOTP widely, often through their own authenticator apps or through a standard app. A bank that once sent codes by text message increasingly offers app-based codes instead, because text messages can be intercepted through SIM swapping, where an attacker convinces a mobile provider to move a number to their own SIM card.

Code repositories and developer tools rely on TOTP to protect the software supply chain. GitHub, GitLab, and package registries require or strongly encourage a second factor, because a compromised developer account can inject malicious code into software used by millions.

Remote access and VPNs use TOTP as well. An employee connecting to a company network enters their password and a code from their authenticator app, so that a stolen password cannot be used from an attacker’s machine, and the company can be confident that the person connecting is who they claim to be.

Cryptocurrency exchanges and password managers use TOTP to guard accounts whose compromise would be immediately costly. A password manager protected only by a password is a single point of failure, so most of them require a second factor, and TOTP is the most common choice.

Strengths and limits

The greatest strength of TOTP is that it works offline and costs nothing. The codes are generated on the device, with no message to send, no call to make, and no hardware to buy, which means it works for anyone with a phone, anywhere in the world, and it cannot be intercepted in transit because nothing is transmitted.

It is also a strength that TOTP is an open standard. Any authenticator app works with any service that supports it, so users are not locked into one vendor, and the algorithm has been public and reviewed for over a decade, which is more than can be said for proprietary code systems.

The limits are real, though. TOTP does not resist phishing: a fake login page can ask for a code just as it asks for a password, and a code entered within its thirty-second window can be used immediately by the attacker. The secret is also a single point of failure: anyone who copies it can generate codes forever, which is why backup codes and recovery procedures must be kept safe, and why hardware keys are recommended for the most valuable accounts.

TOTP checklist

  • Turn on TOTP for email first, since email unlocks password resets elsewhere.
  • Store backup codes somewhere safe, separate from the device.
  • Keep your phone’s clock accurate, since codes depend on the time.
  • Do not share a screenshot of the QR code, since it contains the secret.
  • Use a reputable authenticator app, and back up its data if it offers the option.
  • Never enter a code on a site you reached through a suspicious link.
  • Replace SMS codes with TOTP wherever the service allows it.
  • Revoke and re-enrol TOTP if you suspect the secret has been exposed.
  • Consider a hardware key for accounts that must resist phishing.

Frequently asked questions

What is TOTP in simple terms?

TOTP is the system behind the six-digit codes in authenticator apps. Your phone and the service share a secret, and both use the current time to work out the same code, which changes every thirty seconds.

Why do TOTP codes change every thirty seconds?

So that a stolen code is useless within a minute. The time step makes each code valid only briefly, which limits the damage if someone sees or intercepts it.

What happens if my phone’s clock is wrong?

The codes will not match, because the server uses its own clock. Servers accept neighbouring time steps, which covers small differences, but a phone that is minutes out will fail until its clock is corrected.

Is TOTP safer than SMS?

Yes. SMS codes travel over the mobile network and can be intercepted, notably through SIM swapping, while TOTP codes are generated on the device and never sent anywhere.

What if I lose my phone?

Use the backup codes the service gave you when you set up TOTP, or its account recovery process. This is why saving those codes somewhere safe matters as much as turning TOTP on.

Can TOTP be phished?

Yes. A fake login page can ask for a code and use it immediately, since the code is valid for thirty seconds. Hardware security keys resist this, because they check the site’s identity before answering.

The bottom line

TOTP explained in one line is this: a shared secret and a clock, used by both sides to generate the same short-lived code, with nothing ever sent between them.

It is the most practical second factor available, free, offline, and supported almost everywhere, and it stops the most common account attacks dead. Its weakness is phishing, which is why the most valuable accounts deserve a hardware key as well, but for everyday accounts, TOTP is the single best upgrade a user can make.

This article is general information about how TOTP works, not advice about securing a particular account.

Latest articles

Scroll to Top