What is the app actually doing?
It's doing math, not talking to the internet. The app and the service you're signing in to both hold the same secret value. Each one mixes that secret with the current time to produce a number.
The standard behind this is called TOTP, short for time-based one-time password. It's written up in RFC 6238, a public technical document. TOTP is the time-based variant of an older method called HOTP, which is built on a scrambling function named HMAC. In that older method, a counter is mixed with the shared secret. TOTP swaps the counter for a value taken from the clock.1
Because both sides run the same calculation, they land on the same answer. The service doesn't need to send you the number.
Why does the code change every 30 seconds?
The code belongs to a slice of time, and RFC 6238 recommends a default slice of 30 seconds. It calls that a balance between security and usability.1
A short slice matters because a code only helps a thief while it's still valid. The standard says a longer slice exposes a larger window to attack.1
You may see a code accepted a moment after the ring runs out. That's deliberate. Networks are slow, so the service may also compare against the previous slice. The standard recommends allowing at most one step for that delay.1
What does the service check?
When you type the code, the service works out what it expects for the current time slice, and perhaps the one before. If your number matches, you're through. If it doesn't, you're not.
The service has to keep its copy of the shared secret. RFC 6238 recommends storing those keys securely on the service's side. That's why a code from an app isn't useless when someone steals your password: they'd also need the secret held on your device.1
A code that's exposed before it's used is the worry. RFC 6238 says that when a code is generated and shown to a third party before it's consumed, that person can use it within the same time window. That's one reason the slice is short.1
Are app codes the strongest option?
No. They're a solid step up from a password alone, but agencies rank methods in tiers.
CISA lists application-based MFA among the popular forms. It also says not all methods give the same level of protection.2
CISA names FIDO/WebAuthn as the only widely available phishing-resistant type. Those are the security keys and passkeys covered elsewhere on this site.2
What about a lost phone?
The shared secret lives on your device, so a lost or replaced phone is the usual trouble. The lost-phone recovery page shows the way back in.
If you're an admin deciding whether to allow app codes, team setup covers the choices business apps give you.
Sources used
- RFC 6238: TOTP: Time-Based One-Time Password Algorithm, Internet Engineering Task Force (IETF)
- More than a Password, Cybersecurity and Infrastructure Security Agency (CISA)