What is a TOTP / OTP code?
A one-time password (OTP) is a short numeric code that works once. The ones in Google Authenticator, Authy, 1Password and similar apps are TOTP codes, time-based one-time passwords defined in RFC 6238. The app and the server share a secret key. Every 30 seconds each side hashes that secret together with the current time using HMAC and cuts the result down to 6 digits. If both sides compute the same digits, the user has the device. The older sibling, HOTP (RFC 4226), uses a counter instead of the clock: the code changes only when the counter moves.
That secret is usually shown as a base32 string such as JBSWY3DPEHPK3PXP (the alphabet is in RFC 4648: letters A to Z and digits 2 to 7) or as a QR code that wraps it in an otpauth:// link. This OTP generator and validator turns a secret into live codes, shows the previous, current and next code with a countdown, builds the key URI and QR code to scan into an authenticator app, and checks a code against the secret.
It is a developer's tool for testing two-factor authentication: what is the right code right now, or why does the server reject this one? It runs in your browser, so the secret is not uploaded. The secret is kept only if you tick the option to remember it on this device. For the random bytes behind secrets, see the token generator.
How to generate and check an OTP code
- Open the page: it creates a random secret for you. To use your own, paste it into Secret. Spaces and hyphens, as authenticator apps show them, are accepted.
- Choose the Type: time based (TOTP) or counter based (HOTP). Most services use TOTP.
- Adjust Algorithm (SHA1 by default, SHA256 or SHA512), Digits (6, 7 or 8) and Period in seconds (30 by default) to match your server. Authenticator apps usually assume SHA1, 6 digits and 30 seconds, so change these only when your service really differs.
- Read Previous, Current code and Next, with the time left until the next one. For HOTP, set the Counter and use Next code (+1) to advance it.
- To test a login, open Check a code, type the code the user sent and pick the tolerance in Accept: the exact step only, or one, two or five steps either side. The answer tells you whether it is valid and whether the device clock is early or late.
- To load an existing account, use Import an account: paste an
otpauth://link, drop a QR picture, or paste a Google Authenticator export link. The picture is read in your browser. - To add the secret to a phone, scan the QR code or copy the Key URI.
Examples
The test vectors from RFC 4226
With the secret GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ (the ASCII text 12345678901234567890), SHA1 and 6 digits, an HOTP counter of 0, 1, 2, 3 yields:
755224
287082
359152
969429If your server returns these for the same secret and counters, your HOTP is correct.
The test vector from RFC 6238
For TOTP with the same secret, SHA1, 8 digits and a 30-second period, at Unix time 59 the code is:
94287082With 6 digits the same moment gives 287082, the last six digits. At Unix time 1111111109 the 8-digit code is 07081804, which shows why the code must be treated as text: the leading zero matters.
A code that fails because of the clock
The check says "Valid, but it is the code from 30 s ago: the device clock is about 30 s slow". The code is right, but the phone is one step behind the server. Widen the tolerance to one step on the server, or fix the phone clock.
The key URI
A key URI has this shape and is what the QR code holds:
otpauth://totp/stdlib.tools:demo-user?issuer=stdlib.tools&secret=JBSWY3DPEHPK3PXP&algorithm=SHA1&digits=6&period=30Common errors and how to fix them
- "Enter a secret. It is a base32 string, such as JBSWY3DPEHPK3PXP.": the secret box is empty.
- "The secret must be base32: only the letters A to Z and the digits 2 to 7.": remove other characters. The digits 0, 1, 8 and 9 and the symbols are not part of base32.
- "Not valid now. Check the secret, the algorithm, the digits and the period, and that the clock of the device is right.": those four settings and the clock are the whole list of causes.
- "A code has 6 digits (this tool is set to 6). You typed 5.": a code is missing a digit, often the leading zero.
- "This is not an otpauth:// key URI.": the imported text is not a link like
otpauth://totp/Issuer:account?secret=.... - "No QR code was found in the picture.": use a sharper, larger picture of only the code.
Frequently asked questions
How does Google Authenticator generate its codes?
It implements TOTP: HMAC-SHA1 over the number of 30-second steps since 1970, truncated to 6 digits. That is why the same secret in any compatible app shows the same code at the same time.
Why is my OTP code invalid?
Most often the device clock is off, the algorithm, digits or period differ (some apps ignore them in the link and use SHA1, 6 digits, 30 seconds), or the secret was copied with a wrong character. The checker on this page points to the likely cause.
Is it safe to paste my real secret here?
The page runs in your browser and does not send the secret anywhere. Even so, use real production secrets only if you trust the device, and prefer test accounts for debugging. The share link option includes the secret, so anyone with that link can generate your codes.
Generate a TOTP in code
JavaScript
import { createHmac } from 'node:crypto';
function base32Decode(text) {
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
let bits = '';
for (const char of text.replace(/[\s=-]/g, '').toUpperCase()) bits += alphabet.indexOf(char).toString(2).padStart(5, '0');
return Buffer.from(bits.match(/.{8}/g).map(byte => Number.parseInt(byte, 2)));
}
function hotp(secret, counter, digits = 6) {
const message = Buffer.alloc(8);
message.writeBigUInt64BE(BigInt(counter));
const mac = createHmac('sha1', base32Decode(secret)).update(message).digest();
const offset = mac[mac.length - 1] & 0x0f;
return String((mac.readUInt32BE(offset) & 0x7fffffff) % 10 ** digits).padStart(digits, '0');
}
const totp = (secret, period = 30, digits = 6) => hotp(secret, Math.floor(Date.now() / 1000 / period), digits);Python
import base64, hashlib, hmac, struct, time
def hotp(secret_b32, counter, digits=6):
key = base64.b32decode(secret_b32.replace(' ', '').upper())
mac = hmac.new(key, struct.pack('>Q', counter), hashlib.sha1).digest()
offset = mac[-1] & 0x0F
code = (struct.unpack('>I', mac[offset:offset + 4])[0] & 0x7FFFFFFF) % 10 ** digits
return str(code).zfill(digits)
def totp(secret_b32, period=30, digits=6):
return hotp(secret_b32, int(time.time() // period), digits)Go
// import "crypto/hmac", "crypto/sha1", "encoding/base32", "encoding/binary", "fmt"
func hotp(secret string, counter uint64, digits int) string {
key, _ := base32.StdEncoding.DecodeString(secret)
msg := make([]byte, 8)
binary.BigEndian.PutUint64(msg, counter)
mac := hmac.New(sha1.New, key)
mac.Write(msg)
sum := mac.Sum(nil)
offset := sum[len(sum)-1] & 0x0f
code := binary.BigEndian.Uint32(sum[offset:offset+4]) & 0x7fffffff
mod := uint32(1)
for i := 0; i < digits; i++ {
mod *= 10
}
return fmt.Sprintf("%0*d", digits, code%mod)
}Bash
oathtool --totp -b JBSWY3DPEHPK3PXP