How Do One-Time Passwords (OTPs) Work? (Explained Clearly) - TOTP & HOTP
How do one-time passwords work? Learn how TOTP, HOTP, and SMS codes verify logins, why codes expire, and how phishing and backup codes affect security.
👉 Master your Security+ skills with Pluralsight here: Pluralsight
You’ll learn how one-time passwords (OTPs), TOTP, HOTP, and authenticator apps work to secure your accounts with multifactor authentication (MFA). This beginner-friendly CompTIA Security+ guide explains why codes expire, how phishing can steal them, and how backup codes help prevent lockouts.
Chapters: 0:00 Intro 0:11 What makes a one-time password different? 0:54 How does an authenticator app generate matching codes? 2:12 Why do OTP codes expire or get rejected? 3:20 Can attackers steal OTPs, and what if you lose your phone? 4:21 Outro
Affiliate notice: Some links may earn a commission. Thanks for your support.
Key Takeaways
OTPs add MFA when paired with a different factor, such as a password plus an authenticator app. An OTP alone is not automatically MFA.
Authenticator apps calculate codes locally using a shared secret and a changing input, so they work offline. Protect the setup QR code and seed: anyone who copies them may reproduce future codes.
HOTP uses a counter; TOTP uses time, commonly in 30-second steps. Unlike TOTP, HOTP has no inherent fixed-time expiration.
Expiration is not the same as single-use protection. Services must also reject reused codes, limit guesses, and restrict acceptance windows.
If TOTP codes keep failing, check the account entry and enable automatic date and time. If a code is nearly expired, wait for a fresh one.
Manually entered OTPs are not phishing-resistant, and SMS adds risks such as SIM swapping. Prefer passkeys or FIDO/WebAuthn security keys where available, as recommended by CISA.
Store single-use backup codes securely outside the phone they protect so losing that device does not also eliminate your recovery option.
👉 Turn understanding into hands-on skills with Pluralsight, an optional next step offering guided security labs, feedback, assessments, and certification practice.
A one-time password (OTP) is a short authentication code intended to be accepted once under specific conditions. Unlike your regular password, it is not a standing credential you can reuse for every login.
So, how do one-time passwords work? There are two main approaches: an authenticator and a server independently calculate matching codes, or a server generates a code and sends it to you. Authenticator apps commonly use a shared secret combined with time or a counter. Text-message verification typically uses a server-generated code delivered to your phone.
Understanding the difference explains why authenticator apps work offline, why codes expire, and why even a valid OTP cannot stop every attack.
How OTPs Add Security to Your Accounts
A stolen password may give an attacker repeated access to an account. Requiring an additional code from an authenticator means that knowing the password alone is no longer enough.
Some links may earn a commission. Thanks for your support.
Watch in the video playerOpen the dedicated player for this video.
A password and an authenticator-generated OTP typically represent two different authentication factors:
Authentication factor
Category
Example
Something you know
Knowledge
Account password
Something you have
Possession
Authenticator app or OTP token
Something you are
Inherence
Fingerprint
Combining different categories creates multifactor authentication (MFA). When exactly two factors are involved, it is also called two-factor authentication, or 2FA.
An OTP by itself is not automatically MFA. Likewise, entering a password and answering a security question does not become MFA merely because there are two steps: both are knowledge checks.
How Authenticator Apps Generate Matching Codes
Your authenticator app usually does not receive a new code from the website every 30 seconds. It calculates the code locally.
That process begins when you enroll the authenticator.
Setup establishes a shared secret
When you enable authenticator-based verification, the service typically displays a QR code. Scanning it gives your authenticator a secret key, often called a seed, along with configuration details.
The service retains the same secret. Both sides can then calculate matching codes using:
The shared secret.
A changing input, such as a counter or time-step number.
The same cryptographic algorithm.
The setup QR code is not the temporary login code. It carries the information needed to generate future codes.
Protect the setup QR code and seed. Someone who copies the secret may be able to reproduce your authenticator’s codes. That is a more persistent problem than exposing one temporary code.
The calculation produces a short code
HOTP and TOTP use a keyed cryptographic calculation called HMAC, short for Hash-based Message Authentication Code.
The full calculation produces a larger result, which is reduced to a short value suitable for typing.
The changing input determines whether the system is counter-based or time-based.
HOTP vs. TOTP: What Is the Difference?
HOTP uses a counter; TOTP uses time. Both depend on a shared secret.
Feature
HOTP
TOTP
Full name
HMAC-based One-Time Password
Time-based One-Time Password
Changing input
Event counter
Time-step number
What produces a new value?
Advancing the counter
Entering a new time step
Main synchronization issue
Counter mismatch
Clock mismatch
Fixed time-based expiration
Not inherent to the algorithm
Based on the accepted time window
Hardware and software are separate distinctions. A physical token can use TOTP, and an application can implement an OTP algorithm. The device’s appearance does not tell you which calculation it uses.
How HOTP works
With HOTP, the authenticator combines its secret with a counter. When it generates another code, the counter advances.
The server tracks corresponding counter state. After successful verification, it advances its accepted state so the same counter-based credential cannot simply be reused.
Generating many unused codes can move the authenticator ahead of the server. Some implementations check a limited range of upcoming counter values to accommodate this.
Unlike TOTP, HOTP does not inherently expire after a fixed number of seconds. Its changing input is the counter, not the clock.
You still need connectivity to access the remote website or application. Offline code generation does not make the website itself available offline.
Why OTP Codes Expire, and Why Valid Codes Sometimes Fail
A TOTP code changes because its time-step input changes. The server accepts only codes within its configured time window.
Some servers allow neighboring time steps to accommodate small clock differences or the time needed to enter a code. Therefore, the app’s 30-second countdown is not necessarily the complete server acceptance window.
A secure verifier must also do more than compare numbers:
Reject reuse: An already accepted OTP must not be accepted again while it remains valid.
Limit guesses: Short numeric codes require controls against repeated guessing.
Restrict acceptance: Excessively broad counter or time windows weaken protection.
Expiration and single-use enforcement solve different problems. Expiration limits how long a credential remains acceptable. Single-use enforcement prevents reuse after successful authentication.
Troubleshooting rejected codes
These are possible explanations, not definitive diagnoses. If problems continue, follow the service’s official recovery process.
How SMS and Other Delivered Codes Work
A texted verification code follows a different model from local authenticator generation.
Typically:
You initiate a login or verification request.
The service generates a code.
It associates the code with that request.
It sends the code through a delivery channel.
You submit the code.
The service verifies it and consumes it after successful use.
The code should also expire, and failed attempts should be limited.
SMS describes delivery, not the OTP algorithm. Receiving six digits by text does not mean the service uses TOTP.
Expiration and resend rules vary by provider. Requesting another message does not universally mean a different code will be generated.
Can Attackers Steal One-Time Passwords?
Yes. OTPs improve security, but manually entered OTPs are not phishing-resistant.
A fake login page can collect your password and current code, then immediately relay both to the real website. The attacker does not need to predict a future code if they can use the current one first.
Single-use enforcement does not prevent this race to first use.
Texted codes depend on the security of the telephone network and your phone number. In a SIM-swapping attack, criminals take control of the number and may receive verification messages intended for you.
Authenticator-generated codes avoid SMS delivery risks, but they can still be captured through phishing.
To reduce exposure:
Check the website before entering credentials or codes.
Do not share OTPs or backup codes with callers.
Protect authenticator setup secrets.
Prefer phishing-resistant passkeys or FIDO/WebAuthn security keys where available.
A hardware device that displays a numeric OTP is not automatically a phishing-resistant security key. The authentication protocol matters, not just the physical device.
How Backup Codes Help Prevent Account Lockouts
Backup codes are pre-issued, single-use secrets that provide an alternative when your normal authentication method is unavailable, for example, after losing your phone.
They differ from TOTP codes:
They are saved in advance rather than continually recalculated.
They do not rotate every 30 seconds.
Each code is invalidated after successful use.
Generating a replacement set may invalidate the previous set.
Depending on the service, a backup code replaces the usual second step, not the account password. Google’s backup-code documentation provides an example.
Store backup codes securely somewhere you can access without the phone they are meant to replace. Keeping your only copy on that phone defeats their recovery purpose.
Treat them as account-access credentials: do not share them, and replace exposed codes using the service’s official settings.
Build Practical Authentication Skills
Understanding OTPs is a foundation for configuring MFA, diagnosing rejected codes, and planning account recovery. Hands-on practice helps turn that understanding into a repeatable process.
Pluralsight is an optional next step with guided security labs, feedback, assessments, and practice exams. Use it to apply foundational concepts, identify weak spots, and focus your certification preparation.
Affiliate notice: Some links may earn a commission. Thanks for your support.
How Does Biometric Authentication Work? (Explained Clearly) - Fingerprints and Face UnlockNext
FAQ
Quick answers to the questions people usually ask about this topic.
How do I keep access to my authenticator codes when switching phones?
Before replacing or erasing your old phone, follow your authenticator app’s official transfer instructions or re-enroll your accounts using each service’s security settings. Verify that codes work on the new phone, and keep backup codes accessible independently of either device. If your old phone is lost, use the service’s official recovery process.
Can two phones generate the same one-time password?
Yes. Two authenticator apps configured with the same shared secret, algorithm, and time-step settings can generate matching TOTP codes when their clocks agree. This is why protecting the setup QR code matters: copying it can allow another device to generate your future codes.
Can someone guess a six-digit OTP?
A six-digit numeric code has one million possible values, so a correct guess is possible. Security depends on the service limiting failed attempts, restricting its acceptance window, and rejecting codes already used successfully. Short codes are not safe simply because they change frequently.
What should I do if I receive an OTP I did not request?
Do not share the code or enter it into a page linked from an unexpected message. It could result from a mistaken entry or an attempted account action. Open the service through its official app or a known address, review recent activity, and change your password if you find signs of compromise. An unsolicited code alone does not prove someone knows your password.
What should I do if someone sees my authenticator setup QR code?
Treat the shared secret as potentially compromised. Through the service’s official security settings, replace the authenticator enrollment so it uses a new secret and the old enrollment is revoked. Merely waiting for the displayed OTP to change does not fix an exposed seed: someone with that seed may generate future codes.
Does an OTP replace my account password?
Not necessarily. In a typical password-plus-OTP login, the code is an additional authentication factor, not a replacement for the password. Some services use delivered codes for passwordless sign-in, but an OTP by itself does not automatically make that process multifactor authentication. The service’s login design determines which credentials are required.
Should I use an authenticator app instead of SMS verification?
When both are available, authenticator-generated codes avoid SMS-specific risks such as SIM swapping and message-delivery problems. However, both manually entered authenticator codes and SMS codes can be stolen through phishing. For stronger protection against phishing, prefer passkeys or FIDO/WebAuthn security keys where supported.
How can I turn my understanding of OTPs into practical cybersecurity skills?
Practice applying authentication concepts, evaluating security controls, and identifying gaps in your knowledge. If you want a structured next step, explore Pluralsight for guided security labs, feedback, assessments, and practice exams that support hands-on learning and focused certification preparation.
Affiliate link — I may earn a commission at no additional cost to you.
Jay's Recommendation
Turn understanding into hands-on confidence
You've built the foundations. Pluralsight adds opportunities to practice in guided security labs, get feedback, and check your knowledge with assessments and practice exams, so your next step is more than another video.
🛠Put concepts to workBuild practical skills with guided security labs and feedback.
🎯Find your weak spotsUse assessments to focus on what needs more work.
📝Check your understandingUse practice exams to guide your certification preparation.
🧭Give study sessions directionOrganize further learning with curated paths and progress tracking.