What TOTP is
TOTP (Time-based One-Time Password) is the six-digit code an authenticator app shows and changes every 30 seconds. It is defined in RFC 6238 and built on HOTP (HMAC-based One-Time Password), RFC 4226.
The server and the app share a secret when you set up the app, usually by scanning a QR code. From then on, both compute the same code from the secret and the current time. No network connection is needed: the phone can be offline and still produce valid codes.
This tool computes TOTP codes in your browser from a Base32 secret, and checks a code against it, so you can debug authenticator setups and test your own implementation.
How a TOTP code is calculated
TOTP turns the time into a counter, then runs HOTP on that counter.
- Counter: T = floor((Unix time − T0) / period). T0 is 0 and the period is usually 30 seconds.
- MAC: compute HMAC-SHA1 (or SHA-256, SHA-512) of the counter as an 8-byte big-endian number, keyed with the shared secret.
- Dynamic truncation: take the low 4 bits of the last byte of the MAC as an offset, read 4 bytes from that offset, and clear the top bit to get a 31-bit number.
- Digits: the code is that number modulo 10^6 for six digits, or 10^8 for eight, padded with leading zeros.
Unix time = 1111111109
period = 30
counter T = floor(1111111109 / 30) = 37037036 (0x00000000023523EC)
secret = "12345678901234567890" (ASCII)
code = truncate(HMAC-SHA1(secret, T)) mod 10^8
8 digits = 07081804The RFC 6238 test vectors
RFC 6238 publishes expected 8-digit codes for fixed times, with ASCII secrets of 20, 32 and 64 bytes for SHA-1, SHA-256 and SHA-512. This tool’s code passes all of them.
| Unix time | SHA-1 | SHA-256 | SHA-512 |
|---|---|---|---|
| 59 | 94287082 | 46119246 | 90693936 |
| 1111111109 | 07081804 | 68084774 | 25091201 |
| 1234567890 | 89005924 | 91819424 | 93441116 |
| 2000000000 | 69279037 | 90698825 | 38618901 |
| 20000000000 | 65353130 | 77737706 | 47863826 |
The SHA-1 secret in Base32 is GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ, which is what this page loads with. It is a published test value, not anyone’s real secret.
Why codes fail: clocks, steps and settings
Because both sides derive the code from the time, a clock difference produces a different counter and a different code. A phone 40 seconds off is already one or two steps away.
Servers allow for this with a window: they also accept the codes for one or more steps before and after the current one. RFC 6238 recommends allowing at most one step of network delay. Wider windows make codes easier to guess.
| Mismatch | Effect | Fix |
|---|---|---|
| Phone or server clock off by more than a step | Every code rejected | Turn on automatic time on the phone; run NTP on servers |
| Algorithm differs (SHA-1 vs SHA-256) | Every code wrong | Match the server’s setting, or use SHA-1 |
| Digits differ (6 vs 8) | Every code wrong | Match the digits |
| Period differs (30 vs 60 seconds) | Codes match only sometimes | Match the period |
| Secret mistyped | Every code wrong | Re-enroll by QR code |
What this tool does and does not do
- It computes the current, previous and next TOTP codes from a Base32 secret, updating every second, with the seconds left in the step.
- It supports SHA-1, SHA-256 and SHA-512, six or eight digits, and 30 or 60 second periods.
- It checks a code you type against the current step and one step either side, and says which step matched.
- It can make a new random 160-bit secret.
- It uses your device’s clock. If that clock is wrong, its codes are wrong too, just like an authenticator app’s.
- It does not send the secret anywhere, and it does not store it.
How to debug an authenticator
- Get the secret the server stored for the user, or a test secret enrolled in the app. Prefer a test account.
- Paste it and set the algorithm, digits and period to the server’s settings.
- Compare the code here with the code in the app. If they match, the app is set up correctly.
- Type the app’s code into the check field. If it matches the previous or next step, a clock is off.
- If nothing matches, the secret or settings differ. Re-enroll the app.
One-time passwords in Keycloak
Keycloak supports TOTP and HOTP as a second factor. Its settings live in the realm’s OTP policy, under Authentication, Policies.
| OTP policy setting | Default | Notes |
|---|---|---|
| OTP type | Time based | Counter based (HOTP) is also available |
| OTP hash algorithm | SHA1 | SHA256 and SHA512 are offered; many apps support only SHA1 |
| Number of digits | 6 | 8 is offered |
| Look around window | 1 | Steps accepted before and after the current one |
| OTP token period | 30 seconds | Applies to time-based OTP |
Users enroll through the Configure OTP required action. Keycloak shows a QR code and the secret, and asks for a code to confirm. Admins can make OTP mandatory for everyone through the browser flow, or ask specific users to set it up by adding the required action.
The policy applies to new enrollments. If you change the algorithm, digits or period, existing users’ apps keep the old settings, so plan changes before rolling OTP out widely.
Keep the defaults (SHA1, 6 digits, 30 seconds) unless every app your users have supports something else. They are what nearly all authenticator apps expect.
Security notes
- The shared secret is a credential. Anyone with it can make valid codes forever, until the user re-enrolls. Store it encrypted on the server and never log it.
- RFC 4226 requires secrets of at least 128 bits and recommends 160. This tool’s random secrets are 160 bits.
- RFC 6238 says a verifier must not accept the same code twice after it has been used successfully. That stops a code seen over someone’s shoulder from being replayed within its step.
- Rate-limit code attempts. A six-digit code has one million values; without limits, guessing becomes feasible. In Keycloak, turn on the realm’s brute force detection.
- TOTP does not stop phishing. A fake sign-in page can ask for the code and use it at once. Passkeys and WebAuthn resist phishing; TOTP does not.
Best practices
- Use SHA-1, six digits and a 30-second period for broad app support.
- Keep the window at one step.
- Give users a recovery path, such as recovery codes, that does not depend on the phone.
- Keep server clocks synchronized with NTP.
- Offer WebAuthn or passkeys alongside TOTP for users who can use them.
Implementing TOTP yourself
Most frameworks have a tested TOTP library, and using one is the right choice. If you write or review an implementation, check these points against the RFCs.
- Decode the Base32 secret to bytes before using it as the HMAC key. Using the Base32 text itself as the key is a common bug that produces consistent but wrong codes.
- Write the counter as 8 bytes, big-endian, even though it fits in fewer.
- Mask the top bit after dynamic truncation, so the number is 31 bits.
- Pad the result with leading zeros to the full number of digits; about one code in ten starts with zero.
- Compare codes in constant time, accept a window of at most one step either side, and refuse a code already used.
- Test against the RFC 6238 vectors above for all three hashes.