---
title: "The code that changes every 30 seconds: verify TOTP correctly"
description: "A TOTP code comes from a shared secret and a clock. To protect your users, allow at most one time step of delay, reject a code that was already accepted, and store the secret in a protected place."
url: https://ihsanalapsi.dev/articles/totp-two-factor-codes
lang: en
date: 2026-11-27
updated: 2026-11-27
tags: ["App security", "Web development"]
author: Ihsan Alapsi (إحسان العبسي)
---
# The code that changes every 30 seconds: verify TOTP correctly

A short number on a phone screen, changing every half minute, is the last thing standing between an attacker with a stolen password and your user's account. That number doesn't come from the internet or from a message: it comes from a secret stored once, and a clock. By the end of this article you will know how the code is made, the three mistakes that turn it into decoration, and how to fix them in your verification code.

## What is a TOTP code?

A TOTP code is a one-time password made from a secret shared between the server and the user's app, plus the current time. There is no connection between the app and the server: both sides compute the same number because they hold the same secret and the same clock.

It is like a safe whose combination changes every half minute, with the manager and the employee holding the same schedule. [The RFC 6238 standard](https://datatracker.ietf.org/doc/html/rfc6238), published in May 2011, defines the counter `T` as the current Unix time minus `T0`, divided by `X`. The default value of `X` is 30 seconds, and `T0` defaults to 0, the Unix epoch.

```javascript
const X = 30;
const T = Math.floor(Date.now() / 1000 / X);
```

The same standard says the default algorithm is HMAC-SHA-1, and that implementations may use HMAC-SHA-256 or HMAC-SHA-512.

## Where does TOTP protection fail in apps?

TOTP protection fails in the code that verifies the code, not in the math. The code is strong as long as the server rejects what it should reject.

The most common mistake goes like this: a user complains that the code "didn't work," so the developer widens the window to several minutes to stop the complaints. Now every code from the last few minutes is accepted, and an attacker who saw a code on a screen or on a phishing page has time to use it. A second mistake: the server accepts the same code twice within the same window, so a code stolen at the moment of login also works for the attacker.

The fix is in the same standard: keep the window small, and never accept an accepted code again.

## How do you verify the code correctly?

You verify it correctly with three rules: a small fixed time window, rejecting any code that was already accepted, and a protected secret.

1. Shrink the window. The standard recommends allowing at most one time step as network delay, and if you want to cover clock drift, set a specific limit on the number of steps. Compute the code for the current step and the one before only, and reject the rest.
2. Block replays. The standard says the verifier must not accept the same code a second time after it was accepted. Store the last accepted step for each user, and reject any code from that step or older.
3. Protect the secret. The standard says keys should be randomly generated, and the key store must be in a secure area. Don't keep it in plain text next to the password in the same table, and don't print it in logs.

And add your own limit on attempts at the code screen: the code is short, and without a limit the attacker keeps guessing.

## When is TOTP not enough?

A TOTP code doesn't stop a phishing page that asks for the code and uses it right away, because the user types it by hand into the wrong page. It makes taking over an account with a password alone much harder, but it doesn't stop everything.

So keep the login itself protected: a limit on password attempts, an alert to the user on a new sign-in, and a recovery path that doesn't skip the second step easily.

## Where do you start now?

The lesson for us as developers: the code that changes every 30 seconds is as strong as the server rules that accept it, not as strong as the digits on the screen.

Open the function that verifies the code in your app now, and check how many steps it accepts and whether it stores the last accepted step. Then review [defending the login against stolen-password stuffing](/articles/credential-stuffing-login-defense), [storing passwords with Argon2id](/articles/password-hashing-argon2id), and [the password reset link](/articles/password-reset-token-flow), since it is the back door around two-factor login. And if you want someone to test the whole login of your platform, [Muhassan](/muhassan) does that. How many steps does your code accept today?

## FAQ

**If the user's phone clock is a little behind, is the code rejected?**

Not necessarily. The standard recommends allowing at most one time step for network delay, and describes a fixed limit on time steps for clock drift. Keep that limit small and fixed.

**Should I use SHA-256 instead of SHA-1?**

Not required. The default in the standard is HMAC-SHA-1, and it allows HMAC-SHA-256 or HMAC-SHA-512. What matters most is that the app and the server agree on the same algorithm.

**Does a TOTP code replace the password?**

No. It is a second factor on top of the password. If the password is stolen, the attacker still needs the code that comes from the secret stored on the user's phone.
