---
title: 'Where a 6-digit login code comes from · Reverse Engineer Projects showcase'
url: https://reverseengineerprojects.com/showcase/authenticator/
---

[Home](https://reverseengineerprojects.com/) / [Showcases](https://reverseengineerprojects.com/showcase/) / Login codes

  Auth app · The algorithm

# How does an authenticator make a login code?

  Open any authenticator app's code and you land on the same short routine: the time, cut into 30-second steps, signed with your secret, cut down to six digits.

- 01
### Find the generator
Search the app for the 30-second period and the HMAC call.

- 02
### Read the calculation
Counter, HMAC, offset, four bytes, modulo.

- 03
### Rebuild and check
Same steps in a few lines, checked against the standard's test values.

## Ask your agent

    Pick an open-source authenticator you can download (an Android package, a desktop app or a browser extension):

    Your coding agentCopy

      ›Use REP on this authenticator app. Find where it turns the time and the secret into the six-digit code, show me each step, and rebuild it in a short script I can test.

    In an Android package REP decompiles the classes and narrows them by what they call: the class that reads the clock, divides by the period and calls the HMAC is the generator; the screen that shows the code calls it.

## Try the code generator

        Secret (base32)
        Clock  now

          1 · Time—
          2 · 30-second step—
          3 · Step as 8 bytes—
          4 · HMAC-SHA1 (secret, step)—
          5 · Offset—
          6 · Four bytes, top bit off—

      ——————

      Standard check (RFC 6238, appendix B, SHA-1, time 59 s, 8 digits): …

    The secret above is a well-known example value, not anyone's account. Never paste a real secret into a web page.

## Read the calculation

    the generator, rebuilt · JavaScript

```
const step = Math.floor(Date.now() / 1000 / 30);          // changes every 30 s
const h = hmacSha1(secret, uint64BigEndian(step));       // 20 bytes
const off = h[19] & 0x0f;                                 // low 4 bits of the last byte
const bin = ((h[off] & 0x7f) << 24) | (h[off+1] << 16) | (h[off+2] << 8) | h[off+3];
const code = String(bin % 1_000_000).padStart(6, '0');
```

    That is the whole secret of the six digits. The phone and the server hold the same key and the same clock, so they compute the same number without talking; the 30-second step is why the code changes on the half minute, and why a phone with a wrong clock gets codes the server rejects. Move the clock slider: each 30-second step gives a new counter, a new HMAC and a new code.

    The steps are fixed by two public standards, [RFC 4226](https://www.rfc-editor.org/rfc/rfc4226) (the HMAC and the truncation) and [RFC 6238](https://www.rfc-editor.org/rfc/rfc6238) (the clock), so finding them in an app is mostly confirming which options it uses: the period, the digits, the hash.

  On this page[Ask](#ask)[Try the generator](#try)[Read the calculation](#read)
