๐Ÿงฐ Handy ยท Updated October 26, 2026 ยท 5 min read

What Is a 2FA Code and How Does TOTP Work?

Your password Plus a code ๐Ÿ”

A 2FA code is the short, six-digit number you enter after your password to prove it is really you. Most of these codes come from a system called TOTP, which generates a fresh number every thirty seconds from a shared secret and the current time. This explainer unpacks what a 2FA code is and exactly how TOTP produces it, without the jargon. Once you see how the pieces fit together, the rotating six digits stop feeling like magic and start making sense.

๐Ÿ” Try the 2FA Code Generator now โ€” freeOpen โ†’
Six-digit one-time code 187 689 with a green countdown bar, 23s left and the next code 113 190, calculated in the browser
The hash of secret plus time becomes the six-digit code you type into the site.
๐Ÿ’ก Why 2FA codes exist

A password alone is a single point of failure: if it leaks, anyone can log in. Two-factor authentication adds a second proof, something you have, so a stolen password is no longer enough. The 2FA code is that second factor. Because the code changes constantly and is generated on your own device, an attacker who knows your password still cannot get in without the current number. Understanding how the code is produced makes the whole system less mysterious and helps you use it confidently. Grasping the mechanism also helps you troubleshoot: most 2FA problems trace back to a clock out of sync or a code entered a beat too late, both of which the model explains.

What a 2FA code actually is

A 2FA code is a temporary, one-time password, usually six digits, that is valid for a short window and then replaced by a new one.

How TOTP generates the code

TOTP stands for time-based one-time password. It combines two ingredients you and the server both know to compute the same number independently.

When you set up 2FA, the service shares a secret key with your authenticator, usually via a QR code. From then on, your app takes that secret and the current time, runs them through a hashing function, and produces the six-digit code. The server does the same calculation, and if the numbers match, you are in.

Why the code changes every 30 seconds

The time element is what makes the code temporary. TOTP divides time into thirty-second windows and generates a new code for each one.

Why it works without the internet

A common surprise is that authenticator apps generate codes offline. Nothing is sent to you when you open the app.

Because both your device and the server already share the secret and both read the clock, each computes the code on its own. That is why 2FA codes work on a plane with no signal, and why they are more reliable than codes texted to a phone.

HOTP, TOTP and where the code comes from

Two closely related schemes generate one-time codes, and knowing the difference clarifies why most apps refresh on a timer.

What the QR code actually contains

The QR code you scan during setup is not the code itself; it is the shared secret that makes every future code possible.

Encoded inside it are the secret key, the account name and the issuer, so your app knows which service the codes belong to. Because that secret is the crown jewel, anyone who captures the QR code can generate your codes, which is why you should treat setup screens as sensitive.

When a 2FA code is rejected

A code that looks right but fails is almost always a timing or entry problem, not a broken account. Work through the usual suspects before requesting a reset.

Treat the second factor as one layer of a set. Pair it with a unique, long credential from the Password Generator so that neither factor carries the whole defence.

Where 2FA codes are used day to day

You meet these codes most often when signing in to email, banking, cloud storage and social accounts, and again when a service sees a new device or location. Some sites ask for the code on every login, others only when something looks unusual, so the prompt you see depends on how that service assesses risk.

Step-by-step

123456
1Enable 2FA on an account and scan the QR code.
2Your authenticator stores the shared secret from that code.
Authenticator setup key JBSW Y3DP EHPK 3PXP typed into the 2FA secret key field with 6 digits, 30 seconds and SHA-1 options
The shared secret from the setup QR code is the first ingredient, here typed as a Base32 key.
3The app combines the secret with the current time.
Period set to 30 seconds with the countdown ring and seconds left shown beside the generated code
The app combines that secret with the current 30-second time step.
4It hashes them to produce a six-digit code.
Six-digit one-time code 187 689 with a green countdown bar, 23s left and the next code 113 190, calculated in the browser
The hash of secret plus time becomes the six-digit code you type into the site.
5Enter the code, and the server runs the same math to verify it.
6Keep the setup QR code private, since it holds the shared secret.
Digits menu changed to 8 and algorithm to SHA-256 giving the longer 8-digit code 253 47425 for the same key
Digits, period and algorithm must match the service, or the server's math will not give the same code.

Common mistakes to avoid

โš ๏ธThinking the code is texted or emailed each time you log in.
โš ๏ธBelieving the code is random rather than computed from time.
โš ๏ธIgnoring a phone clock that has drifted out of sync.
โš ๏ธAssuming an old code still works after its window passes.
โš ๏ธSharing a screenshot of the 2FA setup QR code with others.

Pro tips

โœ“Keep your device clock set to automatic so codes stay valid.
โœ“Use an authenticator app rather than SMS where possible.
โœ“Enter the code promptly before the thirty-second window ends.
โœ“Save backup codes when you first enable 2FA.
โœ“Treat the setup QR code as a secret; anyone who has it can make your codes.

Frequently asked questions

What is a 2FA code?

It is a short one-time number, usually six digits, that you enter after your password as a second proof of identity.

How does TOTP create the code?

It combines a secret shared at setup with the current time, hashes them together, and outputs a six-digit code that the server independently recomputes.

Why does the code change every 30 seconds?

TOTP splits time into thirty-second windows and generates a new code for each, so an intercepted code expires almost immediately.

Do 2FA codes need an internet connection?

No. Authenticator apps compute codes offline from the shared secret and the clock, so they work with no signal.

What is the difference between HOTP and TOTP?

HOTP generates codes from an incrementing counter, while TOTP uses the current time; almost all authenticator apps use TOTP, which is why codes refresh on a timer.

๐Ÿ“Œ Bottom line

A 2FA code is a one-time number that TOTP computes offline from a shared secret and the clock, giving you a second factor that a stolen password cannot bypass.

Open the 2FA Code Generator โ†’

Related guides

Browse more: all all guides ยท the 2FA Code Generator