Skip to content

TOTP code generator and verifier

Generate the current time-based one-time code from a Base32 secret, and check a code against it, as an authenticator app would.

Runs in your browser. Nothing you paste is sent to us.

Secret and settings

The RFC 6238 test secret is filled in. Paste your own; it stays in your browser.

Codes

Codes depend on this device’s clock. If they never match, compare the clock with an internet time source; a minute off is enough to fail.

How to use it

  1. Paste the Base32 secret, or make a new random one.
  2. Set the algorithm, digits and period to match the system.
  3. Read the current code and the seconds left.
  4. Type a code to check it against the current, previous and next steps.

What to know

TOTP (RFC 6238) is HOTP (RFC 4226) with the counter set to the Unix time divided by the period. Both sides compute an HMAC of that counter with the shared secret and keep a few digits.

The code changes every period, so the device clock matters. Servers usually accept a step or so either side to allow for drift.

Keycloak’s default OTP policy is time-based, HmacSHA1, 6 digits and a 30-second period, with a look-around window of one step. A realm can change it under Authentication, in the policies.

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.

  1. Counter: T = floor((Unix time − T0) / period). T0 is 0 and the period is usually 30 seconds.
  2. MAC: compute HMAC-SHA1 (or SHA-256, SHA-512) of the counter as an 8-byte big-endian number, keyed with the shared secret.
  3. 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.
  4. Digits: the code is that number modulo 10^6 for six digits, or 10^8 for eight, padded with leading zeros.
An RFC 6238 test case worked through
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   = 07081804

The 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 timeSHA-1SHA-256SHA-512
59942870824611924690693936
1111111109070818046808477425091201
1234567890890059249181942493441116
2000000000692790379069882538618901
20000000000653531307773770647863826

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.

MismatchEffectFix
Phone or server clock off by more than a stepEvery code rejectedTurn on automatic time on the phone; run NTP on servers
Algorithm differs (SHA-1 vs SHA-256)Every code wrongMatch the server’s setting, or use SHA-1
Digits differ (6 vs 8)Every code wrongMatch the digits
Period differs (30 vs 60 seconds)Codes match only sometimesMatch the period
Secret mistypedEvery code wrongRe-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

  1. Get the secret the server stored for the user, or a test secret enrolled in the app. Prefer a test account.
  2. Paste it and set the algorithm, digits and period to the server’s settings.
  3. Compare the code here with the code in the app. If they match, the app is set up correctly.
  4. Type the app’s code into the check field. If it matches the previous or next step, a clock is off.
  5. 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 settingDefaultNotes
OTP typeTime basedCounter based (HOTP) is also available
OTP hash algorithmSHA1SHA256 and SHA512 are offered; many apps support only SHA1
Number of digits68 is offered
Look around window1Steps accepted before and after the current one
OTP token period30 secondsApplies 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.
Questions

Frequently asked questions

Why is my authenticator code not working?

Most often the phone’s clock is wrong; turn on automatic date and time. Otherwise the app and server disagree on the secret, algorithm, digits or period. Compare them with this tool.

How does a TOTP code work without internet?

The app and the server share a secret from setup. Both compute the code from that secret and the current time, so they agree without talking to each other.

What is the difference between TOTP and HOTP?

HOTP codes come from a counter that moves each time a code is used. TOTP codes come from the time, changing every period. TOTP is what most authenticator apps use.

How long is a TOTP code valid?

One period, usually 30 seconds. Servers often accept one step before and after to allow for clock drift and typing time, so a code may work for up to about 90 seconds.

Is SHA-1 safe for TOTP?

Yes. TOTP uses HMAC-SHA1, and SHA-1’s collision weakness does not give a practical attack on HMAC. SHA-1 remains the default because nearly every app supports it.

Can I see the TOTP secret for a Keycloak user?

Not through the admin console after enrollment; Keycloak stores the credential and does not display the secret again. To reset, delete the user’s OTP credential and have them enroll again.

Is it safe to paste my TOTP secret here?

The page sends nothing and stores nothing. Still, a secret is a credential, so use a test secret where you can, and re-enroll if a real one may have been exposed.

Does TOTP protect against phishing?

No. A phishing page can ask for the code and pass it to the real site within seconds. TOTP still stops attacks that use only a stolen password; passkeys stop phishing too.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.