khanhsec
VI
3 min readCryptographyAuthentication

Where six digits come from

Nothing is exchanged between phone and server, yet both agree. The whole algorithm, step by step.

Bản tiếng Việt

You type six digits and the server agrees. Between those two ends there is nothing but an ordinary HTTP request, and nothing at all is sent back to your phone. So how do the two sides agree?

The short answer is a keyed hash and a clock.

Exactly one shared secret

When you scan the QR code, a base32 string is handed to the authenticator app. That string is the whole secret. There is no session, no registration with Google's servers, nothing running on the network.

The server keeps one copy, the phone keeps another. From then on both need only compute the same function over the same input. No communication, no trust required.

The other input is time:

counter = floor(unix_time / 30)

Every 30 seconds the counter advances by exactly one. The code is not "refreshed" in any meaningful sense — it is the same formula evaluated with a different number.

The code is carved out of an HMAC

RFC 6238 describes this step, inheriting almost unchanged from HOTP (RFC 4226). This is the whole algorithm, not a sketch:

import { createHmac } from "node:crypto";
 
function totp(secret: Buffer, unixSeconds: number): string {
  const counter = Math.floor(unixSeconds / 30);
 
  // The counter is encoded as 8 big-endian bytes, exactly as HMAC expects.
  const counterBytes = Buffer.alloc(8);
  counterBytes.writeBigUInt64BE(BigInt(counter));
 
  const digest = createHmac("sha1", secret).update(counterBytes).digest();
 
  // "Dynamic truncation": the last byte picks where to take four bytes from.
  const offset = digest[digest.length - 1] & 0x0f;
  const value =
    ((digest[offset] & 0x7f) << 24) |
    (digest[offset + 1] << 16) |
    (digest[offset + 2] << 8) |
    digest[offset + 3];
 
  return String(value % 1_000_000).padStart(6, "0");
}

Three details there are worth pausing on:

  • & 0x0f limits the offset to 16 positions, so the four bytes taken always fit inside the 20-byte SHA-1 digest.
  • & 0x7f clears the sign bit. Without it the result can go negative in languages that treat integers as signed.
  • % 1_000_000 is the entire reason there are six digits. Nothing is special about the number six.

Six digits are not the weak point

A million possibilities sounds thin. But the code lives for 30 seconds, and every wrong guess is counted. With a limit of eight attempts in fifteen minutes, the chance of guessing inside one session is negligible.

What protects you is not the length of the code. It is the server refusing to answer quickly enough for anyone to try a million times.

Clock skew

Both sides must agree on time, but clocks never match exactly. Every implementation accepts a window: the current step's code, and usually the step before or after as well.

A wider window is friendlier to use, and easier to probe. One step is the usual trade-off — roughly 30 seconds of drift in each direction.

What is left is a keyed hash, a counter derived from time, and some careful slicing. The six digits hold no secret — they only prove you hold the same secret string as the server, at the same moment.

Comments

No comments yet.

Comments are moderated before they appear.

Get new posts by email

No ads, no tracking. Just new posts, unsubscribe any time.