TOTP Code Generator

The Complete TOTP Code Generator Guide

How time-based one-time passwords are computed, what each control does, and how to check the output against the published RFC 6238 test values.

Open TOTP Code Generator

What a TOTP Code Is

A time-based one-time password (TOTP) is the six- or eight-digit code many services ask for after your password. RFC 6238 defines it and builds on the HMAC-based one-time password scheme in RFC 4226. Both sides share a secret key once, during setup. After that, each side computes the current code without talking to the other.

The code depends on three inputs: the shared secret, the current time and a fixed period, usually 30 seconds. The time is turned into a counter by dividing the Unix time by the period and rounding down. The counter, not the clock itself, is what gets hashed. That is why a code stays the same for the whole period and changes at each boundary.

This page runs the same steps in your browser. Use it to check a code, learn how the scheme works or test a service you are building. It does not replace an authenticator app for your own everyday logins.

The secret is the sensitive part. Anyone who holds it can produce valid codes for the account. While the page is open, the secret stays only in the input field and in the page's memory. It is not written to storage and is not sent over the network, and you should only use secrets for accounts that you own.

Step-by-Step: Every Control Explained

  1. Base32 secret. Paste the key a setup screen shows, usually a block of capital letters and digits. Spaces are removed automatically and lowercase letters are accepted. Only the characters A to Z and 2 to 7 are allowed. A secret under 16 characters is refused, and a length that cannot form a complete Base32 value is flagged as possibly cut off.
  2. Code length. Choose 6 or 8 digits. Use whatever the service requests, because the server has to use the same length.
  3. Period. The number of seconds each code lasts, from 10 to 120. Most services use 30. A different period moves the boundaries, so the code changes at other moments.
  4. Algorithm. SHA-1 is the default and the most widely deployed. SHA-256 and SHA-512 are available for services that ask for them. The algorithm selects the hash used inside the HMAC.
  5. Advanced: fixed Unix time. Enter a time in seconds to compute the code for that moment. The countdown pauses while this is set. Use it to reproduce test vectors or to look into a clock difference.
  6. Countdown bar. Shows the seconds left in the current period. The bar turns amber during the final five seconds.
  7. Copy code and Clear. Copy places the displayed digits on the clipboard. Clear empties the secret and the fixed time, and the page returns to its idle state.

Worked Example

Setting up the RFC test. The SHA-1 test secret is the ASCII text 12345678901234567890, whose Base32 form is GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ. Enter it, set the length to 8, keep the period at 30 and the algorithm at SHA-1. Open the advanced panel and type 59 as the Unix time.

Working it through. The counter is 59 divided by 30, rounded down, which is 1. HMAC-SHA-1 is computed over the eight bytes of that counter. The hash is truncated to a four-byte value and reduced modulo 10 to the power of 8. The result is 94287082, which matches the published vector.

Same moment, 6 digits. The same hash reduced modulo 10 to the power of 6 gives 287082, the last six digits of that value.

Other vectors. At Unix time 1111111109 with SHA-1 and 8 digits the code is 07081804. For SHA-256 the secret is 32 bytes, which in Base32 is GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZA, and time 59 gives 46119246. SHA-512 uses a 64-byte secret, and time 59 gives 90693936.

Tips and Common Mistakes

  • Check your device clock. A phone or computer several periods out of step produces codes the server rejects. Many services accept a neighbouring period, but a large error still fails.
  • Use the secret, not a recovery code. Some setup screens show recovery codes beside the secret key. Only the secret key feeds the calculation.
  • Match digits, period and algorithm. If any one of them differs from the server, every code is wrong even though the secret is correct.
  • Expect different codes for different hashes. A SHA-256 code is not the same as a SHA-1 code for the same secret and time.
  • Keep secrets out of untrusted pages. The privacy note on this page describes what happens here, but another site may behave differently.
  • Store secrets carefully. A secret is effectively the second factor itself, so keep it in a password manager or authenticator app rather than in a note beside your password.

Frequently Asked Questions

Why do the server and my device show the same code at the same moment?

Both use the same formula for the counter, so the code changes exactly when the counter changes. Small clock differences can shift that moment, which is why servers usually accept codes from neighbouring periods.

Can I use this tool for someone else's account?

Only for accounts you own, and only with a secret you are entitled to hold. A TOTP secret works like a password for the second factor, so using one without permission is not acceptable.

What happens if I enter a Unix time in the past?

The code is computed for that moment, which is useful for test vectors. Real logins need the current code, so a past time only helps you check how the algorithm behaves.

Will the codes match my authenticator app?

Standards-based apps should show the same code for the same secret, algorithm, digit count and period at the same time. This page is a calculator, so it does not store accounts or sync anything between devices.

Open TOTP Code Generator