How two-factor codes (TOTP) work, and what the secret key is
The six-digit code in your authenticator app is not stored anywhere or sent from the website. Your phone and the website each work it out independently, from the same secret and the current time.
The shared secret
When you turn on two-factor authentication, the website shows a QR code. Inside it is a secret key, written in Base32, which uses the letters A to Z and the digits 2 to 7. Your app saves the key, and so does the website.
From that moment, both sides hold the same secret. The QR code is just a convenient way to copy it. If you can type the secret by hand, the result is identical.
How the code is made
The method is standardised as TOTP, short for Time-based One-Time Password, in RFC 6238. It builds on HOTP, the counter-based version in RFC 4226.
- Take the current Unix time in seconds and divide it by 30, dropping the fraction. This is the time step counter.
- Run an HMAC, usually with SHA-1, using the secret as the key and the counter as the message.
- Pick four bytes out of the result, starting at a position given by its last digit. This is called dynamic truncation.
- Take that number modulo 1,000,000 to get six digits, adding leading zeros if needed.
A real test vector
RFC 6238 publishes test values so that software can be checked. With the secret being the ASCII text 12345678901234567890, which in Base32 is GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ, at Unix time 59 the 8-digit code is 94287082. Reducing the same value to six digits gives 287082.
secret GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ
time 59 -> counter 1
8 digits 94287082
6 digits 287082Why the codes change every 30 seconds
Each 30-second window has its own counter, so the code changes with it. A stolen code is useful for only a short time. Websites usually accept the codes from the previous and next window too, to allow for small clock differences.
Common problems
- Wrong codes are usually caused by a phone clock that is off by more than a minute. Turn on automatic time.
- The digits, the period or the algorithm must match what the website expects. Almost every service uses 6 digits, 30 seconds and SHA-1.
- Typing the secret with spaces or lower case letters is fine in most apps, because Base32 is not case sensitive.
Keeping it safe
- Treat the secret like a password. Anyone who has it can create valid codes forever.
- Save the backup codes the website gives you, and store them somewhere separate from your phone.
- Do not paste a real secret into websites you do not trust. A tool that works in your browser without sending data is safer, but a test secret is better still.
- TOTP does not stop a live phishing site that asks for your code and passes it on straight away. Passkeys are designed to resist that.
Generate and check codes
The TOTP generator turns a Base32 secret into the current code, with a countdown, and supports 6 or 8 digits, other periods and SHA-256 or SHA-512. It is useful for developers testing a login flow. Use test secrets, not the secret of a real account.