How two-factor codes work, six digits at a time

What is inside the QR code, how an authenticator turns a secret and the time into six digits, why it works offline, and what two-factor codes cannot stop.

Updated 16 November 2026 6 min read

You scan a square barcode once, and from then on an app shows you a new six-digit number every thirty seconds that a website somehow agrees with. There is no magic in it and very little network traffic. These numbers are called time-based one-time passwords, TOTP for short, and the whole scheme fits in a few paragraphs. Knowing how it works also makes it clear what it protects you from and where it stops.

What is inside the QR code

When you turn on two-factor authentication, the site creates a random secret for your account and shows it to you as a QR code. The site keeps a copy. Your authenticator app stores the other copy when you scan.

The QR code is a short piece of text in a standard format. It starts with otpauth://totp/ and carries:

  • the secret, written in letters and digits (an encoding called Base32),
  • the name of the service and your account, so the app can label the entry,
  • optionally the number of digits, the length of a time step and the hash function to use. Nearly every site uses six digits and thirty seconds.

Most sites also show the secret as plain text, labelled something like "setup key" or "can't scan?". That text and the QR code are the same thing. Scanning is typing it without the typos.

How the secret and the time become six digits

The method is a public standard, RFC 6238. Both sides run the same four steps.

  1. Count the time steps. Take the current time as a number of seconds since 1 January 1970 and divide by 30, dropping the remainder. Everyone with a correct clock gets the same whole number, and it goes up by one every half minute.
  2. Mix it with the secret. Feed that number and the secret into a keyed hash function called HMAC. The output is a long string of bytes that looks random and cannot be worked backward to reveal the secret.
  3. Cut out a piece. Use the last few bits of the output to choose a position, and read four bytes from there as one large number.
  4. Keep the last six digits. That is the code on your screen.

The website does the same calculation with its copy of the secret and compares. If the numbers match, you have proved that you hold the secret, without ever sending the secret itself.

Why the codes work offline

Look at the inputs: a secret that is already on your device, and the time. Neither needs a connection. Your authenticator works in airplane mode, in a tunnel, or on a device that has never been online since the day you scanned the code.

The one thing that must be right is the clock. If your device is minutes off, it is working on a different time step than the server and the codes will be refused. Sites usually accept the code just before or after the current one to allow for small differences, but not more. If codes suddenly stop working everywhere, check that your device sets its time automatically.

What two-factor codes protect you from

A password is something that can be copied. It can leak from a site that was broken into, be guessed, or be reused from another account. A code adds something that changes every thirty seconds and is calculated from a secret the attacker does not have.

  • Leaked and reused passwords. A password from a data breach is not enough to sign in on its own.
  • Guessing. Getting the password right still leaves a six-digit number that expires almost at once, and sites limit how many tries you get.
  • Someone reading over your shoulder. A code they saw is useless a minute later.

What they do not protect you from

Codes are a strong second lock, not a guarantee.

  • Phishing in real time. A fake sign-in page can ask for your password and your code, and pass both to the real site within the thirty seconds. The code does not know which site you typed it into. Check the address before you type, and let a password manager fill in logins, because it will not offer them on the wrong address.
  • A stolen secret. If someone copies the QR code, the setup key or an unprotected backup of your authenticator, they have your codes.
  • Malware on your device. Software that controls your device can read what is on it.

Codes, text messages, passkeys and backup codes

MethodHow it worksWorth knowing
Code by text messageThe site sends a number to your phone.It depends on your phone number, which can be taken over. Better than no second step.
Authenticator code (TOTP)Your device calculates the number from a shared secret.Works offline and does not depend on your phone number. Can still be phished.
Passkey or security keyYour device proves itself with a private key that never leaves it.Tied to the real site's address, so a fake page gets nothing useful.

If a site offers passkeys, they are the stronger choice. Authenticator codes are the next best, and they are supported almost everywhere.

Backup codes are the spare key. During setup the site gives you a short list of one-time codes for the day your device is lost or broken. Save them somewhere that is not the same device, such as on paper.

How Vela handles two-factor codes

Vela's password manager has a 2FA Codes tab next to your passwords. You add a code from the site's QR code: scan it with the camera, choose an image of it, or let Vela find it on the page you have open. You can also paste the setup key or an otpauth:// link. Vela accepts six to eight digits and the SHA-1, SHA-256 and SHA-512 variants, and calculates the codes on your device the way the standard describes.

A code can be attached to a saved login. When the site then asks for a verification code, Vela offers to fill it in, so you do not switch apps and retype six digits against a countdown. You can also click or tap a code to copy it. Codes are stored in the same encrypted vault as your passwords and are kept with the logins of a profile. If you turn on iCloud Sync in Vela, they sync encrypted between your devices.

The steps are in the guide to two-factor codes in Vela, and the rest of the vault is covered in the password manager guide.

Questions, answered.

Why does my code stop working after a few seconds?

Each code is tied to a 30-second slice of time. When the slice ends, your app and the website both move on to the next code, so type the one that is showing now.

What happens if I lose the device with my authenticator?

You sign in with one of the backup codes the site gave you during setup, then set up two-factor authentication again. Without backup codes you have to go through the site's account recovery, which can be slow.

Can I use the same QR code on two devices?

Yes. The QR code holds the secret, and any app that has the secret produces the same codes. That is also why you should treat a screenshot of the QR code like a password.

Are authenticator codes safer than codes sent by text message?

Generally yes. A code from an app is calculated on your device and never travels over the phone network, so it cannot be redirected by someone who takes over your phone number.

Does changing my password change my two-factor codes?

No. The codes come from a separate secret that stays the same until you turn two-factor authentication off and set it up again.

Keep reading.

All articles
Passwords & security

How to keep your two-factor codes in Vela

Scan a site's 2FA QR code or paste its key, then copy codes with a click or let Vela fill them in when you sign in.

7 minMac · iPhone · iPad
Passwords & security

How to save and autofill passwords with Vela

Save logins as you sign in, let Vela fill them next time, and keep them locked, synced and separated by profile.

5 minMac · iPhone · iPad
How the web works

Profiles, private windows or a second browser: which one?

Four ways to stay signed in to two accounts on one site, what each one really separates, and when to use which.

6 minBlog