Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 3 min read

How TOTP Authenticator Apps Work?

Time-based One-Time Password A Time-based One-Time Password (TOTP) is a temporary passcode (a secret number) used to confirm a person's identity when logging into a computer system. It forms part of two-factor authenti

How TOTP Authenticator Apps Work?

Time-based One-Time Password

A Time-based One-Time Password (TOTP) is a temporary passcode (a secret number) used to confirm a person's identity when logging into a computer system. It forms part of two-factor authentication (a security check that asks for two different proofs of who you are, like a normal password plus a temporary number).

Unlike text-message codes, TOTP does not require a cellular network or an internet connection. The app on a user's device and the login server (the main computer running the website) calculate the exact same number at the exact same moment completely independently.

Operating Principle

The offline operation of TOTP relies on two shared pieces of information:

  1. A shared secret key (a private string of letters and numbers known only to the user's device and the server).
  2. The current time (agreed upon using standard worldwide clock time).

Because both the user's phone and the server possess the secret key and can read the time, they do not need to talk to each other to produce matching codes.

How TOTP Code Match?

Setup and Key Exchange

The shared secret is exchanged only once, during the initial setup:

  1. Generation: When a user enables two-factor authentication, the server creates a unique, random key.
  2. Transfer: The server displays this key as a QR code (a square barcode that a phone camera can scan).
  3. Storage: The user scans the QR code with an authenticator app. The app saves the secret key securely inside the phone's storage.

Once this initial exchange is complete, no further network communication is ever required between the authenticator app and the server.

Algorithm and Code Generation

TOTP calculates a code through three distinct steps:

1. Time Discretization (Splitting Time into Blocks)

The algorithm (a set of step-by-step mathematical instructions) takes the current Unix time (the total number of seconds that have passed since January 1, 1970) and divides it by an agreed interval, almost always 30 seconds:

Counter=⌊IntervalΒ (30)CurrentΒ UnixΒ Timeβ€‹βŒ‹

Because computers drop any remainder (fractions left over after division), this Counter value stays completely unchanged for the entire 30-second window, increasing by 1 only when the next 30-second block begins.

2. Cryptographic Hashing (Scrambling the Data)

The counter and the shared secret key are combined and run through an HMAC function (a special mathematical recipe that scrambles data into a fixed-length digital fingerprint that cannot be reversed or undone).

This produces a 160-bit hash (a long string of computer bits, which are basic 1s and 0s) that is impossible to predict without knowing both the secret key and the current time block.

3. Truncation (Shortening)

The long scrambled output is systematically chopped down into a human-readable number:

  • The last 4 bits of the hash determine an offset (a starting position or point of reference).
  • Starting at that position, 4 bytes (small packets of computer memory) are extracted.
  • The extracted number is reduced using the modulo operation (the math operation that finds the remainder after dividing by a million) to produce a 6-digit number, typically displayed as:
Passcode=ExtractedΒ Number(mod106)

Verification and Clock Drift

When the user enters the 6-digit code into a website, the server runs the exact same formula using its own clock and the user's saved secret key. If the server's output matches the user's input, access is granted.

To account for clock drift (small time differences where a phone's internal clock runs slightly faster or slower than the server's clock), servers generally calculate codes for the current time window, the window immediately before, and the window immediately after (a $\pm30$-second safety margin).

References

  1. M'Raihi, D.; Machani, S.; Pei, M.; Rydell, J. (May 2011). "TOTP: Time-Based One-Time Password Algorithm". Internet Engineering Task Force (IETF).
  2. M'Raihi, D.; Bellare, M.; Hoornaert, F.; Naccache, D.; Ranen, O. (December 2005). "HOTP: An HMAC-Based One-Time Password Algorithm". Internet Engineering Task Force (IETF).
πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.