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.
- 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.
- 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.
- 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.
- 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
| Method | How it works | Worth knowing |
|---|---|---|
| Code by text message | The 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 key | Your 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.