2FA code generator (TOTP)
Everything runs in your browser; nothing is sent or stored.
- Paste a test key or an otpauth://totp/ URI into the "Base32 secret key or otpauth:// URI" field, and the settings update to match the URI.
- In the "Algorithm", "Code length", and "Code validity" lists, choose the settings of the service you are testing.
- The "Codes" section shows the current code, the seconds left until it changes, and the previous and next codes. The code is calculated from your computer's clock, and if the clock is not accurate, the code will not match the service's code.
- Enter a code in the "Check a code" field, for example from an authenticator app, and the result shows whether it matches one of the three codes.
- Clicking "Create a new key" replaces the value in the key field with a new random key. This tool is designed for testing and development, and for testing, we recommend using a test key created with the tool.
- In the "otpauth URI" section, enter a service name and an account name, and copy the full URI by clicking "Copy URI".
How a TOTP code is generated
When you turn on two-factor authentication, the service and the authenticator app store the same secret key. A TOTP (Time-based One-Time Password) code is calculated from the key and the current time, as defined in RFC 6238: time is divided into fixed steps, usually 30 seconds long, and the number of the current step goes through an HMAC calculation together with the key. A fixed number of digits, usually 6, is taken from the result, and each code is valid only during the step in which it was calculated. When you log in, the service calculates the code the same way and compares it with the code you entered, so the key itself is never sent when you log in.
The secret key and the otpauth URI
The secret key is written in Base32 encoding, which uses only the letters A to Z and the digits 2 to 7, without digits that look like letters, such as 0 and 1, so the key is easy to type by hand when a QR code cannot be scanned. The QR code shown when you set up two-factor authentication contains a URI in the otpauth://totp/ format, with the service name, the account name, the key, and parameters for the algorithm, the code length, and the validity period. When these parameters are missing from the URI, the defaults are the SHA1 algorithm, a 6-digit code, and a 30-second validity period. Some authenticator apps support only the default values, so many services do not change them.
Accurate clocks and adjacent codes
The code depends on the time, so both the clock on the device running the authenticator app and the clock on the service's server need to be accurate. A clock difference of a few dozen seconds may cause the app to show the code of a different time step than the one the service expects. Many services also accept the previous and the next code, to make up for a small clock difference and for the time it takes to enter the code, which is why the tool shows three codes. If a code from the app is rejected again and again, make sure the automatic time setting on the device is turned on.
Parts of an otpauth://totp/ URI
Example:dana@example.comthe label in the URI path: the service name and the account name, separated by a colon.secret=JBSWY3DPEHPK3PXPthe Base32 secret key, the only parameter the URI must include.issuer=Examplethe service name, shown in the authenticator app next to the account name.algorithm=SHA1the HMAC algorithm. The tool also accepts SHA256 and SHA512.digits=6the number of digits in the code. The tool also accepts the value 8.period=30how long each code is valid, in seconds. The tool also accepts the value 60.
2FA code generator (TOTP) FAQ
Why is the code in my authenticator app different from the code shown in the tool?
The code depends on the key, the settings, and the time, and a change in any of them changes the code. Make sure the algorithm, code length, and validity period in the tool match the settings in the otpauth URI, and that your computer's clock is as accurate as the clock on the device running the app.
What is the difference between TOTP and HOTP?
With HOTP, defined in RFC 4226, the code is calculated from a counter that goes up by one each time a code is generated, and with TOTP the counter is the number of the current time step. The tool supports only TOTP codes, and it shows an error message for a URI that starts with otpauth://hotp/.
Why does a message say the key is shorter than 16 bytes?
RFC 4226 requires a key of at least 128 bits (16 bytes) and recommends a key of 160 bits (20 bytes). The tool also generates codes from a shorter key, but a short key is easier to guess. A key created by clicking "Create a new key" is 20 bytes long, which is 32 characters in Base32.
How do I add a test key to an authenticator app?
You can enter the key in the app manually, or copy the full URI and create a QR code from it with the "QR code and Wi-Fi QR generator" tool, using the "Text or address" content type. The authenticator app scans the code and adds the account with the settings in the URI.
What is the maximum length of the secret key?
The field accepts up to 2,048 characters, and the key can be up to 128 bytes long. You can enter the key in uppercase or lowercase letters, with or without spaces or hyphens, and with or without = signs at the end.