TOTP

How TOTP Works Under the Hood: HMAC, Time Steps, and RFC 6238 Explained

A technical, step-by-step breakdown of how Time-Based One-Time Passwords are generated: the RFC 6238 standard, the HMAC-SHA1 algorithm, dynamic truncation, and why the whole system needs no network connection to work.

Nency
Published Sep 21, 2026
4 min read

RFC 6238: The Standard Behind Every Authenticator App

Every six-digit code you've ever typed from an authenticator app follows the same published specification: RFC 6238, "TOTP: Time-Based One-Time Password Algorithm," defined by the Internet Engineering Task Force. It's an open standard, not a proprietary trick owned by any one company — which is exactly why a browser-based tool like AuthBro can generate codes that are indistinguishable from a dedicated mobile app's output. Both are implementing the same math from the same document.

TOTP itself is a small extension of an earlier standard, HOTP (HMAC-based One-Time Password, RFC 4226), which used an incrementing counter instead of time. TOTP swaps that counter for the current timestamp, so both sides of the exchange — your account and your authenticator — always agree on "now" without needing to talk to each other.

The Core Time-Step Formula

The first step in generating a TOTP code is turning the current time into a counter value, referred to as T:

CODE
T = floor((Current Unix Time - T0) / X)

Here, T0 is the Unix epoch, effectively 0, and X is the time-step size — almost universally 30 seconds. This means T only changes once every 30 seconds, which is exactly why a code stays valid for that window and then rotates. If X were 1 second, codes would expire before most people could finish typing them; if it were 5 minutes, a stolen code would stay dangerous for far too long. Thirty seconds is a deliberate balance.

HMAC-SHA1: Turning a Secret and a Time Into a Number

Once T is known, it's combined with your account's secret key using an HMAC-SHA1 function — a cryptographic hashing operation that's effectively impossible to reverse. Feed it the same secret key and the same time counter, and it always produces the same 160-bit hash. Change either input even slightly, and the output is completely different and unpredictable, with no partial correlation to the original.

This property is what makes TOTP secure: someone watching a single valid code learn essentially nothing about the secret key that produced it, and nothing about what the next code will be.

Dynamic Truncation: From a Long Hash to a Short Code

A raw HMAC-SHA1 hash is far too long to type at a login screen, so RFC 6238 defines a process called dynamic truncation to shrink it down:

  1. The last 4 bits of the hash are used to pick an offset into the hash itself.
  2. Four bytes starting at that offset are extracted and interpreted as a 31-bit integer (the top bit is discarded to avoid sign-related issues).
  3. That number is taken modulo 106, leaving a value between 0 and 999,999.
  4. The result is padded with leading zeros to always display as six digits.

That six-digit number is the code you type. It looks arbitrary, but it's fully deterministic — anyone (or anything) with the same secret key and the same time will always compute the exact same result.

Why This Design Needs No Network Connection

Because every input to the algorithm — the secret key and the current time — is already known locally, TOTP generation never has to phone home. Your account and your authenticator each run the identical calculation independently and simply expect to land on the same answer. That's a meaningful security property in its own right: there's no code traveling across the internet at generation time for anyone to intercept, log, or replay. You can watch this happen in practice — AuthBro runs this exact RFC 6238 computation entirely inside your browser tab every 30 seconds. Open your browser's developer tools while it's running and you won't see a single network request carrying your secret key anywhere.

The strength of TOTP isn't that the math is exotic — HMAC-SHA1 and modular arithmetic are well-understood, decades-old tools. The strength is that the entire calculation happens locally, with nothing secret ever transmitted.

What This Means for Everyday Use

Understanding the mechanics explains a few things you may have noticed as a user:

  • Codes always expire in about 30 seconds — that's the time-step window, not an arbitrary UI choice.
  • Codes work even offline — there's no server round-trip involved in generating one.
  • A wrong device clock breaks logins — since T is derived from real time, a clock that has drifted enough will produce a code the server doesn't recognize (more on how this is handled in the next guide).
  • The same secret key always produces the same sequence of codes — which is exactly why a QR code (encoding that key) is all a new device needs to start generating identical codes to your original one.
Did you find this helpful?
198

Frequently Asked Questions

TOTP stands for Time-Based One-Time Password. It's a standardized algorithm, defined in RFC 6238, that generates a short numeric code from a shared secret key and the current time.
RFC 6238's dynamic truncation step takes the computed value modulo 1,000,000, which mathematically guarantees a result between 0 and 999,999 — always representable as six digits, padded with leading zeros when needed.
No. Both inputs to the algorithm — your secret key and the current time — are already available locally, so the six-digit code can be calculated entirely offline.
Yes. SHA-1's known weaknesses relate to finding hash collisions, which isn't how it's used in HMAC-TOTP. HMAC-SHA1 remains cryptographically sound for this specific keyed, time-limited application, which is why it's still the RFC 6238 default nearly two decades later.
N

About The Author

Nency

Author is a software developer and technical contributor at AuthBro. They specialize in building performance-oriented browser productivity systems and writing developer guidelines.

Related Guides

Explore additional resources on 2FA security and authentication.