How a 2FA code is made
The six-digit codes of two-factor login, also called MFA or multi-factor authentication, come from two open standards. HOTP, in RFC 4226, computes an HMAC-SHA-1 of a shared secret K and a counter C, written as 8 bytes. It then takes four bytes from the result at a position given by the low four bits of the last byte, drops the top bit and keeps the last digits:
HOTP(K, C) = Truncate(HMAC-SHA-1(K, C)) mod 10^digits
TOTP, in RFC 6238, is the same function with the time in place of the counter. T is the number of whole time steps since the Unix epoch, January 1, 1970, 00:00 UTC, with a step of 30 seconds by default. TOTP may also use HMAC-SHA-256 or HMAC-SHA-512.
T = floor(Unix time ÷ 30), TOTP = HOTP(K, T)
Worked example from the RFC 6238 test vectors: the secret is the text 12345678901234567890, which is GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ in Base32. At Unix time 59 seconds, T = floor(59 ÷ 30) = 1, and the 8-digit SHA-1 code is 94287082. Its last six digits, 287082, are the 6-digit code, the same value RFC 4226 lists for counter 1. Press RFC test key, pick Counter and set the counter to 1 to see it.
The secret is written in Base32 (RFC 4648, section 6): the letters A to Z and the digits 2 to 7, each standing for 5 bits. That is why a secret never holds 0, 1, 8 or 9, and why the tool asks whether you meant O, I or B when it sees one.
The otpauth link inside the QR code
The QR code a service shows during 2FA setup holds a link in the key URI format, like otpauth://totp/Example%20App:alice@example.com?secret=JBSWY3DPEHPK3PXP&issuer=Example%20App. Paste such a link into the field and its settings are filled in.
| Part | Meaning | Default |
|---|---|---|
| totp or hotp | Time-based or counter-based | required |
| Label | Issuer and account, separated by a colon | required |
| secret | The key in Base32, without padding | required |
| issuer | Name of the service | strongly recommended |
| algorithm | SHA1, SHA256 or SHA512 | SHA1 |
| digits | Length of the code | 6 |
| period | Seconds per TOTP code | 30 |
| counter | Starting counter, HOTP only | required for HOTP |
The format description notes that the authenticator app it was written for ignores the algorithm and period parameters, and on some platforms the digits too. A secret set up with SHA-256 or a 60-second period can then show codes in the app that never match. For a service of your own, the defaults SHA-1, 6 digits and 30 seconds are the safe choice.
Setting up 2FA for your own service
- Create a new random secret for each user. New secret uses your browser's cryptographic random generator and makes the key as long as the HMAC output: 20 bytes (160 bits) for SHA-1, as RFC 6238 advises. RFC 4226 requires at least 128 bits and recommends 160.
- Show the QR code and the secret as text, for people who cannot scan.
- Ask for one code before switching 2FA on, so a failed scan never locks anyone out. The Check a code field does the same comparison here.
- When checking codes, RFC 6238 recommends allowing at most one time step of delay, which covers a code typed just as it changed. A larger window gives an attacker more time.
This page is for testing and for people who set up one service by hand. A server that checks codes for real users computes them in its own code; never send user secrets to a web page.
When the code is not accepted
- Clock: TOTP depends on the time. The tool shows the device time it uses in UTC; if your device clock is off by more than a few seconds, set it to update automatically.
- Settings: algorithm, digits and period have to match the service. An otpauth link carries them; a bare secret does not.
- Secret: one wrong character gives completely different codes. Compare the secret in groups of four.
- Counter: counter-based codes only work while your counter matches the server's. Each Next counter press uses up one code.
Need a strong password to go with the second factor? The Password Generator makes one on your device. To see what else a QR code holds, open it in the QR Code Scanner.
How to get a 2FA code from a secret key
- 1Paste the secret key or the otpauth link, or open a picture of the QR code.
- 2Check the type, algorithm, digits and period. A link sets them for you.
- 3Copy the current code before the ring runs out, or wait for the next one shown below it.
Sources
- RFC 4226, HOTP: An HMAC-Based One-Time Password Algorithm: the algorithm, dynamic truncation, secret length, test values in Appendix D
- RFC 6238, TOTP: Time-Based One-Time Password Algorithm: time steps, SHA-256 and SHA-512, key length (5.1), one step of delay (5.2), test vectors in Appendix B
- RFC 4648, section 6: Base 32 Encoding
- Key Uri Format, google-authenticator project wiki on GitHub: otpauth parameters, their defaults, and which of them the app it was written for ignores
- W3C Web Cryptography API: crypto.subtle for the HMAC and getRandomValues for new secrets, available on secure (https) pages
Guides on this topic
Frequently asked questions
Is it safe to paste my 2FA secret here?
The page computes the codes in your browser with its built-in Web Crypto API. The secret is not sent anywhere, not saved in the browser and never put into the page address, and it is gone when you close or reload the tab. Still, treat a 2FA secret like a password: anyone who has it can make your codes. Use this tool on your own device, for testing or for recovering your own account.
Why does my code not match the one the website expects?
Check three things. First the clock: time-based codes depend on your device time, and the tool shows the time it uses. Second the settings: algorithm, number of digits and period must match what the service uses, and an otpauth link fills them in for you. Third the secret itself: a single wrong character gives completely different codes. Counter-based codes also need the same counter value as the server.
What is the difference between TOTP and HOTP?
Both turn a shared secret into a short code with an HMAC. HOTP (RFC 4226) uses a counter that goes up by one each time a code is used. TOTP (RFC 6238) uses the time instead: the number of 30-second steps since January 1, 1970, so the code changes on its own every 30 seconds.
Can I use this page instead of an authenticator app?
Only for a moment. The page shows codes while it is open and forgets the secret afterwards on purpose, so it is no place to keep your accounts. It helps when you want to check a secret you saved as a backup, test a 2FA setup you are building, or see whether a code problem comes from the clock.
Which settings should I use for my own service?
SHA-1, 6 digits and 30 seconds. They are the defaults of the otpauth key URI format, and the format description notes that the authenticator app it was written for ignores the algorithm and period parameters, and on some platforms the digits, so other values can produce codes that never match. Use a random secret of 20 bytes (160 bits), the length RFC 4226 recommends.
Can I read the QR code a website shows me?
Yes, from a picture. Save or screenshot the QR code and open it with Open QR image, or paste the screenshot into the secret field. The code is read on your device; the camera is not used.