Skip to content
MailSlurp logo

What Is TOTP? How Time-Based One-Time Passwords Work

TOTP is the six-digit code an authenticator app shows and changes every 30 seconds. See how it's calculated from a shared secret and the time, how it differs from SMS codes, and how to test TOTP sign-in automatically.

By Jack MahoneyPublished

Use with your tools

You turn on two-factor sign-in, scan a QR code with an authenticator app, and from then on the app shows a six-digit code that changes every 30 seconds. That code is a TOTP: a time-based one-time password. Nothing is sent to your phone to produce it. The app and the website each calculate the same code from a secret they share and the current time.

This guide explains what TOTP is, how the code is calculated, how it compares with codes sent by SMS or email, and how to test a TOTP sign-in in an automated test, where no one is holding a phone.

What TOTP means

TOTP stands for Time-based One-Time Password. It is defined in RFC 6238 as an extension of HOTP, the counter-based one-time password in RFC 4226. Instead of a counter that goes up each time a code is used, TOTP uses the number of time steps that have passed since a fixed start time.

Authenticator apps such as Google Authenticator, Microsoft Authenticator and password managers with a built-in authenticator all generate TOTP codes, which is why one QR code works in any of them.

How a TOTP code is calculated

1. Enrollment shares a secret

When you turn on two-factor sign-in, the website creates a random secret key and shows it as a QR code. The QR code holds an otpauth:// URI with the secret, usually written in Base32, plus the issuer, account name and settings. Scanning it gives the authenticator app a copy of the secret. From then on, the website and the app both hold the same key.

2. The current time becomes a counter

Both sides divide the current Unix time by the time step and drop the remainder. RFC 6238 recommends a 30-second step counted from the Unix epoch, so every 30 seconds the counter moves on by one.

3. The counter and the secret produce the code

Each side computes an HMAC of the counter using the secret, with HMAC-SHA-1 by default (the standard also allows SHA-256 and SHA-512), then truncates the result to a short number, usually six digits. Because both sides use the same secret, the same time step and the same algorithm, they arrive at the same code without talking to each other.

4. The server checks the code

When you type the code, the server calculates its own value for the current time step and compares them. Clocks and typing take time, so servers usually also accept the code from the neighbouring step. The standard recommends allowing at most one time step of delay.

TOTP compared with SMS and email codes

  • Where the code comes from. A TOTP code is calculated on the device from the shared secret and the time. An SMS or email code is generated by the server and sent as a message.
  • Whether it needs a network. An authenticator app works offline. An SMS or email code has to arrive.
  • How long it lasts. A TOTP code lasts one time step, usually 30 seconds. An SMS or email code lasts as long as the application allows, often several minutes.
  • What a test has to do. A test of TOTP sign-in calculates the same code from the enrollment secret. A test of an SMS or email code receives the message and reads the code from it.

That last difference matters when you test sign-in. An SMS or email code can be tested by giving the test a real phone number or inbox and waiting for the message. A TOTP code can't be waited for, because nothing is sent.

To see the code for a secret by hand, paste the secret or its otpauth:// link into the free TOTP generator. It calculates the current code in your browser and shows when it changes.

How to test TOTP sign-in automatically

An automated test or an AI agent can't scan a QR code into a phone app, but it doesn't need to. It needs the same secret the app would get, and something that calculates codes from it.

MailSlurp virtual TOTP devices do that:

  1. Start MFA enrollment for a test user and read the otpauth:// URL or Base32 secret your application shows.
  2. Create a MailSlurp TOTP device paired with that URL or secret.
  3. Request a fresh code from the device and submit it to finish enrollment.
  4. Sign out, start a new sign-in, request another code and confirm the user gets in.

Enrollment and the later sign-in are separate checks. A test that only submits the first enrollment code hasn't yet shown that a returning user can sign in with MFA.

Agents work the same way. With scoped agent access, an AI agent can list the TOTP devices it's allowed to use and generate a code for an authorized sign-in step, alongside the inboxes and phone numbers assigned to it. Treat enrollment secrets and generated codes as credentials, and keep them out of shared logs.

Frequently asked questions

What does TOTP stand for?

Time-based One-Time Password. It's the standard behind the six-digit codes in authenticator apps, defined in RFC 6238.

How long is a TOTP code valid?

One time step, which is 30 seconds by default. Many servers also accept the code from the step just before or after, to allow for clock differences and typing time.

What is the difference between TOTP and HOTP?

HOTP uses a counter that advances each time a code is generated. TOTP replaces the counter with the number of time steps since the Unix epoch, so codes expire on their own as time passes.

Is TOTP the same as 2FA?

No. Two-factor authentication means signing in with two different kinds of proof. A TOTP code from an authenticator app is one common second factor; SMS codes, email codes and hardware security keys are others.

Why is my TOTP code not working?

The most common causes are a phone clock that's wrong, a code typed after its time step ended, or an app paired with an old secret after the account's MFA was reset. Set the phone's time automatically, use a fresh code, and re-enroll if the secret changed.