How Google Authenticator Works Without Internet


 Why your phone can show a fresh login code every 30 seconds, even in flight mode

Turn on flight mode, open Google Authenticator, and watch. The six-digit code is still there, and every 30 seconds it changes to a new one. No Wi-Fi, no mobile data, no text message. So where do the codes come from?

The short answer: nobody sends them. Your phone and the website's server each calculate the same code on their own. This post explains how that works, why it's secure, and what can go wrong.

The two ingredients

Every code is built from just two things: a shared secret and the current time.

1. A secret key, shared once

When you turn on two-factor authentication for an account, the site shows you a QR code. Scanning it hands your phone a secret key. This happens only once, at setup.

After that there are two copies of the secret: one stored on your phone and one stored on the server. From then on, they never need to talk to each other about it again.

2. The current time

Computers keep time as the number of seconds that have passed since January 1, 1970. Right now that number is somewhere around 1.79 billion. Authenticator divides it by 30, and the result is a counter that stays the same for a full 30-second window and then goes up by one.

Since your phone and the server both know the time, they both arrive at the same counter value, without exchanging a single message.

The trick: turning a secret and a counter into a code

The secret and the counter are fed into a function called HMAC. Here is what happens next:

  1. HMAC mixes the two inputs and produces a jumble of characters, a fixed-length hash.
  2. The process is one-way. Given the output, working backwards to find the secret is practically impossible.
  3. The algorithm selects a few bytes from that output and combines them into one large number.
  4. The last six digits of that number become the code you see on screen.

This is the standard method known as TOTP, or time-based one-time password. Because it's an open standard, many authenticator apps work the same way.

How the server checks your code

When you log in, the server runs the exact same calculation using its copy of the secret and the current counter. It gets a six-digit result. You type the code from your phone. If the two match, you're in.

The important detail is that the code itself was never sent between your phone and the server. Both sides produced it separately. That's why it keeps working when your phone has no connection at all.

Why codes are hard to steal and misuse

Every 30 seconds the counter increases by one. A different counter produces a completely different hash, and therefore a completely different code. So if someone sees or steals your code, it stops being useful within about half a minute.

The secret key is the real prize, which is why it should never be shared or photographed. The code is only a short-lived result of it.

The most common problem: a wrong clock

Since the whole system depends on both sides agreeing on the time, a phone with the wrong clock will calculate the wrong counter. The codes then won't match, and your login will fail even though the secret is correct.

Servers usually tolerate a small amount of drift, but not much. The fix takes seconds: open your phone's settings and switch Date and time to Automatic.

Backups and losing your phone

Since April 2023, Google Authenticator has offered backup to your Google account. If you lose your phone, you can sign in on a new one and your codes come back, which is a big improvement over the old days of being locked out of everything.

There is a trade-off, though. Your secrets are now stored in the cloud, so the security of that Google account matters more than before. Use a strong, unique password and protect it with its own second step. If you'd rather keep the secrets only on your device, the app also lets you use it without an account.

Authenticator app or SMS codes?

Both give you a one-time code, but they get there differently:

  • SMS codes are generated by the service and sent to your phone. They need mobile signal and can be intercepted or redirected in attacks such as SIM swapping.
  • Authenticator apps generate the code on your device from a stored secret, so nothing travels over the network at login time.

For most people, an authenticator app is the stronger choice, and any second step is still far better than a password alone.

Quick recap

  • The code is calculated, not delivered.
  • Two ingredients make it work: a secret shared once through the QR code, and synchronized time.
  • Time becomes a counter by dividing seconds since 1970 by 30.
  • HMAC combines the secret and counter into a one-way hash, and six digits of it become your code.
  • Codes change every 30 seconds and expire quickly.
  • If codes keep failing, set your phone's clock to automatic.
  • Cloud backup is convenient, but it makes your Google account security more important.

Which do you use, SMS codes or an authenticator app? Tell us in the comments.

Comments

Popular posts from this blog

Bootstrap 6: New Features, Major Changes, and What Developers Need to Know

Why Reflections Disappear in Games When You Tilt the Camera

How Games Fake Underwater Worlds: Fog, Color Absorption and Caustics