Techies Journal
AboutContact

Techies Journal

A collection of technical essays on systems, backend architecture, and the craft of engineering.

Sections

  • Technology
  • System Design
  • Backend Engineering
  • Cloud & DevOps
  • AI & ML

Connect

  • About Me
  • Contact

Presence

© 2026 Techies Journal. Handcrafted for readability.

PrivacyTerms
System Architecture
2026-01-27

How Google & Microsoft Authenticator Generate 6-Digit Codes Every 30 Seconds — Even Offline

RU
Rishabh UpadhyaySoftware Engineer & Designer

How Authenticator Apps Work Without Internet

Most people use authenticator apps every day. Very few understand why they work even without internet.

Google Authenticator and Microsoft Authenticator don't talk to servers every time they generate a code.

They don't need to.

Here's the simple but powerful idea behind them.


The Core Idea

When you enable Two-Factor Authentication (2FA), something important happens only once:

** Your device and the server share a secret key.**

After that, both sides independently generate the same 6-digit code, at the same time, using:

  • The shared secret
  • The current time
  • A cryptographic algorithm called TOTP (Time-Based One-Time Password)

No SMS No API calls No internet

Just math + time synchronization


What Is TOTP?

TOTP is defined in RFC 6238 and is built on top of HMAC-based cryptography.

At a high level:

Code
TOTP = HMAC-SHA1(secret_key, current_time_window) % 1,000,000

Where:

  • secret_key → Shared during setup (QR code scan)
  • current_time_window → Changes every 30 seconds
  • Output → Truncated to a 6-digit number

The 30-Second Time Window

Time is divided into fixed intervals (usually 30 seconds).

Code
Time (seconds since Unix epoch)

 0 – 29 → Code A (123456)
 30 – 59 → Code B (789012)
 60 – 89 → Code C (345678)
 ...

Both the server and your phone:

  • Know the current time
  • Use the same interval size (30s)
  • Run the same algorithm

That's why the codes match — even offline.


End-to-End Flow (Visual)

Code
     
 Your Phone       Server  
     
           
   Shared Secret (setup once)   
  
           
   Current Time (synced roughly)  
           
   TOTP(secret + time window)   
           
  6-digit code 

No communication is required during verification.


Why Internet Is Not Needed

The system works because:

  • The secret never changes
  • Time is predictable
  • The algorithm is deterministic

As long as:

  • Your phone's clock is reasonably accurate
  • The server allows small clock drift (±1 window)

…the codes will match.


Why This Design Is Elegant

This approach solves multiple problems at once:

Works Offline

No dependency on SMS, push notifications, or APIs.

Codes Expire Automatically

Every 30 seconds, the old code becomes useless.

Prevents Replay Attacks

Even if someone sees your code, it's valid for only seconds.

Massive Scalability

Servers don't need to store or generate codes per user. They only store the secret.


What the Server Actually Does

When you enter a code:

  1. Server fetches your stored secret
  2. Server computes TOTP for:
  • Current time window
  • Previous window (grace period)
  • Next window (clock drift tolerance)
  1. If any match → Authentication succeeds

Minimal state for replay protection: While the secret itself is long-lived, secure implementations track the last successful authentication time-step per account (RFC 6238 §5.2) to prevent code reuse within the same window.


Why SMS OTP Is Inferior

Compared to TOTP, SMS OTP:

| Feature | TOTP | SMS OTP | |---------|------|---------| | Requires Network | No | Yes | | SIM Swap Vulnerable | No | Yes | | Latency | Instant | Seconds to minutes | | Scalability | Infinite | Costs per SMS |

TOTP avoids all of this with local computation.


Common Misconception

"Authenticator apps generate random codes."

They don't.

The codes are:

  • Deterministic (not random)
  • Predictable only if you have the secret
  • Cryptographically secure

Without the secret key, guessing is computationally infeasible.


Where This Pattern Shows Up Elsewhere

TOTP-style authentication is used in:

  • Banking MFA
  • VPN authentication
  • Cloud provider consoles (AWS, Azure, GCP)
  • Internal enterprise systems
  • Authenticator apps across platforms

Note: Hardware security keys (YubiKey, Titan) primarily use FIDO2/U2F/WebAuthn (challenge-response), not TOTP. Some models support TOTP as an optional feature.

Anywhere you need strong auth without constant connectivity.


The Bigger System Design Lesson

This is a perfect example of:

Simple primitives + strong guarantees = great systems

No real-time sync No chatty protocols No fragile dependencies

Just:

  • Shared state
  • Time
  • Cryptography

Final Takeaway

Google and Microsoft Authenticator don't work because they're "smart".

They work because they're boringly correct.

Sometimes the most widely used systems are also the most beautifully designed.


** Have you ever implemented or debugged MFA / TOTP in real systems?** If yes, you already know how subtle — and powerful — this design is.

Continue Reading
2026-01-26

How Instagram Tells You a Username Is Taken in Under 100ms

A system design deep dive into how Instagram checks username availability instantly at massive scale using caching, normalization, and database constraints.

2026-01-28

When You Send 'Hi' in a WhatsApp Group of 256 People — Does the Database Get Hit 256 Times?

Ever wondered what really happens under the hood when you send a message in a large WhatsApp group? This post breaks down how modern chat systems avoid database overload and scale to billions of messages.

2026-02-11

How Food Delivery Apps Show Live Driver Location Without Refresh

A system-design deep dive into real-time location tracking in apps like Zomato—from driver GPS capture and pub/sub to WebSockets, interpolation, reliability, and privacy.

On This Page

  • How Authenticator Apps Work Without Internet
  • The Core Idea
  • What Is TOTP?
  • The 30-Second Time Window
  • End-to-End Flow (Visual)
  • Why Internet Is Not Needed
  • Why This Design Is Elegant
  • Works Offline
  • Codes Expire Automatically
  • Prevents Replay Attacks
  • Massive Scalability
  • What the Server Actually Does
  • Why SMS OTP Is Inferior
  • Common Misconception
  • Where This Pattern Shows Up Elsewhere
  • The Bigger System Design Lesson
  • Final Takeaway

Share Perspective

TwitterLinkedInInstagram