Yoshith

How Google Authenticator Works Without Internet

system-designsecurityauthenticationcryptographybackend

we may assume authenticator apps(example: google auth) receive OTPs from servers over the internet.

That is not what actually happens.

Apps like Google Authenticator generate codes completely offline using shared secrets and synchronized time.

The interesting part is not the 6-digit number itself. The interesting part is how both the server and your phone independently generate the same value without communicating.

Shared secrets are the foundation

When two-factor authentication is enabled, the server generates a secret key and shows it as a QR code.

JBSWY3DPEHPK3PXP

The authenticator app scans the QR code and stores the secret key locally.

Now both:

  • the server
  • our phone

have the same secret.

That secret becomes the foundation for every future OTP generated in fixed time periods.

OTPs are generated using time

Authenticator apps mainly use a protocol called TOTP (Time-Based One-Time Password).

The generated code depends on:

  • the shared secret
  • the current time

A simplified version looks like this:

TOTP = HMAC(secret_key, current_time_block)

The time block usually changes every 30 seconds.

That is why authenticator codes refresh continuously.

What is Unix time

To make time consistent across all systems, TOTP does not use:

  • local clocks
  • AM/PM
  • dates
  • time zones

Instead, it uses something called Unix time.

Unix time means:

the number of seconds elapsed since January 1, 1970 UTC

Example:

1715518200

That number continuously increases every second.

It is one of the most important concepts in operating systems and backend engineering.

Why Unix time is used

Normal human-readable time is messy.

Different countries use:

  • different time zones
  • daylight savings
  • different formats

But Unix time is universal.

Every machine can calculate the same timestamp regardless of location.

That makes synchronization possible.

Without Unix time, TOTP would become unreliable across devices.

How the 30-second window works

TOTP does not use the raw Unix timestamp directly.

Instead, it divides time into blocks.

Example:

unix_time / 30

If Unix time is:

1715518200

then:

1715518200 / 30 = 57183940

That value becomes the current time block.

As long as:

  • the server
  • your phone

are inside the same 30-second window, both generate the same OTP.

No internet is required

The authenticator app does not contact Google servers whenever a new code appears.

It already has:

  • the secret key
  • the current Unix time

So the OTP can be generated locally using pure computation.

The server performs the exact same calculation during login verification.

If both outputs match, authentication succeeds.

Time synchronization matters

Since TOTP depends heavily on Unix time, clock drift can break authentication.

Even if the secret key is correct, a device with incorrect time may generate invalid OTPs.

That is why some authenticator apps include a:

"Sync time for codes"

feature.

The system depends heavily on both sides staying time-synchronized.

The server usually does not store OTPs

A common misconception is that servers store generated OTP values.

Most systems do not.

The backend typically stores only:

  • the user's secret key

During authentication, the server recomputes the expected OTP dynamically.

This avoids:

  • OTP databases
  • synchronization overhead
  • unnecessary storage writes

The architecture stays lightweight and scalable.

TOTP is stronger than SMS OTP

SMS OTP relies on:

  • telecom infrastructure
  • SIM ownership
  • message delivery

Authenticator apps rely on:

  • local cryptographic computation
  • shared secrets

That removes many SMS-related attack surfaces like:

  • SIM swapping
  • SMS interception
  • carrier-level attacks

TOTP is not perfect, but it is significantly stronger than traditional SMS verification.

The setup phase is the weakest point

If an attacker steals:

  • the QR code OR
  • the shared secret

they can generate valid OTPs forever.

The security of the system depends heavily on protecting the initial setup process.

The protocol itself is strong.

The onboarding flow is often the vulnerable part.

A small Python example

OTP generation can be reproduced easily.

totp-demo.py
import pyotp
import time
 
totp = pyotp.TOTP("JBSWY3DPEHPK3PXP")
 
while True:
    print("Current OTP:", totp.now())
    time.sleep(30)

The generated value changes every 30 seconds, exactly like Google Authenticator.

The non-obvious engineering insight

The genius of TOTP is not cryptography alone.

It is synchronization without communication.

The server and phone stay synchronized using only:

  • shared secrets
  • Unix time

No constant messaging.

No OTP database replication.

No real-time coordination.

Just deterministic computation.

That is elegant systems design.

OTP validation usually checks multiple time blocks(its like +1 and -1 blocks generally)

Most people assume the server validates only the current 30-second OTP window.

Real systems usually do something smarter.

They commonly verify:

  • previous block
  • current block
  • next block

instead of checking only one exact timestamp.

Why this is necessary

Clocks are never perfectly synchronized.

Suppose:

Server time:

10:00:29

Phone time:

10:00:31

Now:

  • the server may still be in the old block
  • the phone may already be in the next block

Even though the user entered the correct OTP, strict validation would fail.

That would create terrible user experience.

How validation windows work

If the current time block is:

57183940

the server may validate:

57183939
57183940
57183941

Meaning:

  • previous 30-second window
  • current window
  • next window

If any generated OTP matches, authentication succeeds.

This is commonly called a:

time window validation

strategy.

Why this design matters

The validation window compensates for:

  • clock drift
  • slow typing
  • small network delays
  • imperfect device synchronization

Without this tolerance, TOTP systems would reject many valid users.

The tradeoff

Larger validation windows improve usability.

But they also reduce security.

Because multiple OTPs become valid simultaneously.

That increases the attack surface slightly.

Most production systems use:

WindowMeaning
±0very strict
±1common production setup
±2more tolerant
large windowsweaker security

Most authenticator systems stay around:

  • ±1 time block

because it balances:

  • usability
  • reliability
  • security

quite well.

The non-obvious engineering insight

TOTP systems are not truly exact-time systems.

They are tolerance-based synchronization systems.

That tiny implementation detail is one of the main reasons authenticator apps feel smooth and reliable in real-world usage.

Final thoughts

Google Authenticator looks simple on the surface, but internally it combines:

  • cryptography
  • hashing
  • synchronized computation
  • Unix time
  • backend verification
  • protocol design

into a system that works reliably even without internet access.

The real engineering idea is not the OTP itself.

It is synchronization using shared time.