How One-Time Passwords Are Generated: HOTP, TOTP and SMS Codes Compared
Most people meet a one-time password as a six digit number on a screen, but behind that number sit three genuinely different mechanisms. Understanding how OTPs are generated matters whether you are building a login flow, debugging a clock skew problem or choosing between an authenticator app and SMS delivery. Each method trades something away: shared secrets, time synchronization, or the cost of sending a message through a carrier. This guide compares HOTP, TOTP and SMS codes with the practical detail a developer needs.
Direct answer: HOTP generates a code from a shared secret plus an incrementing counter, TOTP generates a code from a shared secret plus the current time window, and SMS codes are random values generated on the server and delivered over the mobile network. All three produce short numeric codes, but only the first two run entirely on the device.
HOTP: Counter-Based One-Time Passwords
HOTP is defined in RFC 4226. The server and the client share a secret key. Each time a code is needed, both sides run an HMAC over a counter value, then truncate the result into a short decimal code. After a successful verification, the counter increments.
- Strength: no clock dependency, so it works offline and on devices with unreliable time.
- Weakness: the counter must stay in sync. A user who generates codes without submitting them, or submits one that fails, drifts out of step.
- Typical use: hardware tokens and printed code lists.
Resynchronization is the fiddly part. Most implementations accept a small window of counter values ahead of the expected one to tolerate drift.
TOTP: Time-Based One-Time Passwords
TOTP, defined in RFC 6238, replaces the counter with time. Both sides compute the code from the shared secret and the number of time steps elapsed since an epoch, usually in thirty second windows. Because the input changes with the clock, the code changes automatically every window.
Clock skew is the operational risk. Servers generally accept the previous, current and next window to absorb small differences. Push that tolerance too wide and you widen the window an attacker can exploit; keep it too narrow and users with a slow device complain that codes are always rejected.
- User enrolls and the server stores the shared secret.
- At login, the server derives the expected code for the current time step.
- The user submits a code and the server checks the current step plus one neighbour on each side.
- On a match, the server records the time step to block replay of the same code.
SMS Codes: Server-Generated and Delivered by Carrier
An SMS code is usually not derived from a shared secret at all. The server generates a random numeric value, stores a hash of it with an expiry, and sends the plain value through a messaging provider to the phone number on file. When the user types it back, the server compares hashes.
This is the model used by virtual number platforms and verification APIs. You request a number, a service sends a message to it, and your integration reads the code out of the message body. The security property is different: the code lives briefly in a message that travels over the mobile network, so interception risk exists but is bounded by the short expiry.
HOTP, TOTP and SMS Codes: Comparison
| Aspect | HOTP | TOTP | SMS code |
|---|---|---|---|
| Input to generation | Counter | Time step | Random value |
| Needs a network | No | No | Yes, for delivery |
| Main failure mode | Counter drift | Clock skew | Delivery delay or wrong number |
| Replay defence | Counter increment | Time step tracking | One-time use with expiry |
| Recovery when it fails | Resync | Time check | Resend the message |
How Are Codes Actually Generated on the Server?
For SMS, good implementations use a cryptographically secure random number generator and never a predictable function like a timestamp or an incrementing id. Six digits give one million combinations, which is fine only if you also limit attempts and expiry. Four digits give ten thousand, which is fine only with a very short window and a hard attempt cap.
Store a hash, not the code. Use a fast hash appropriate for short-lived low-entropy values plus a per-record salt, and compare in constant time. Once verified, mark the record as consumed so a replayed submission fails even inside the expiry window.
Which Method Should You Choose?
For account security on a device that is already enrolled, TOTP in an authenticator app is strong and free to operate. For onboarding a new user whose phone number you are trying to confirm, an SMS code is the pragmatic choice because it verifies possession of the number itself. Many products run both: SMS to confirm the number at signup, TOTP for later logins.
When you need to test an SMS-based flow, a virtual number keeps your personal line out of the equation and lets you repeat the flow as often as needed. You can provision one from the SMS Feel registration page and pull the incoming verification code through the API documentation.
Frequently Asked Questions
Are SMS OTPs less secure than TOTP?
They defend against different threats. SMS proves control of a phone number and is vulnerable to SIM swap and interception; TOTP is vulnerable to phishing of the code itself. Neither is universally stronger.
Can an attacker guess a six digit code?
Not if you limit attempts. With a hard cap of three to five tries and a short expiry, brute force becomes impractical. Without an attempt cap, six digits is not enough.
Why do TOTP codes sometimes fail immediately?
Clock skew between the device and the server is the usual cause. Accept a small window of neighbouring time steps and ask users to enable automatic time sync.
How long should an SMS code stay valid?
A few minutes is standard. Long enough to survive delivery delays, short enough that an intercepted message is usually useless by the time it is read.
Is it safe to log generated codes for debugging?
No. Log the request identifier and the outcome, never the code itself. Debugging rarely requires the secret value.
Conclusion
Knowing how OTPs are generated makes the trade-offs obvious: counters need sync, clocks need tolerance, and messages need delivery. Pick the mechanism that matches the threat you are defending against, then enforce expiry, attempt limits and one-time use regardless of which one you choose. To test an SMS flow end to end, get a virtual number from SMS Feel and read the code through the API.
Related reading: more verification engineering notes on the SMS Feel blog.