What Is an OTP in Computer Security?
In this article
An OTP (one-time password) is a single-use authentication code that is valid for only one login session or transaction. It is a standard security measure used to add a second layer of verification beyond a static password, typically as part of two-factor authentication (2FA). The core idea is that even if an attacker steals your regular password, they cannot reuse the OTP because it expires immediately after use or after a short time window.
Why OTPs Exist: The Problem They Solve
Static passwords are a weak link in security. People reuse them across sites, choose predictable ones, and fall for phishing emails that trick them into typing credentials into fake pages. If a password leaks in a data breach, attackers can use it repeatedly until the account owner changes it. OTPs solve this by making each login require a fresh code that an attacker cannot predict or reuse.
The term "one-time password" is standard in the security industry. It appears in academic papers, vendor documentation, and standards like RFC 6238 (TOTP) and RFC 4226 (HOTP). It is not slang or informal; it is the official name for this class of authentication mechanism.
How OTPs Work: The Two Main Types
There are two common ways OTPs are generated:
- Time-based OTP (TOTP) – The code is derived from the current time plus a shared secret. It changes every 30 seconds (some systems use 60). The server and the authenticator app both compute the same code from the same clock. If the clocks drift, the code fails.
- Hash-based OTP (HOTP) – The code is derived from a counter that increments with each use. The server and the device share a counter value; after each successful login, the counter advances. HOTP does not expire on a timer, but it is invalidated after one use.
In practice, TOTP is far more common because it does not require the server to keep track of the last counter value. Apps like Google Authenticator, Authy, and Microsoft Authenticator implement TOTP.
How OTPs Are Delivered: SMS, Apps, and Hardware
You usually receive an OTP through one of these channels:
- SMS text message – The server sends a 6-digit code to your phone. This is the most widely used method but also the least secure because SMS can be intercepted via SIM-swapping attacks or SS7 protocol vulnerabilities.
- Authenticator app – The app generates codes locally on your device using a secret that was shared when you set up 2FA. This is more secure than SMS because the code never travels over the network.
- Hardware token – A small device (like a YubiKey or a key fob) that generates codes or acts as a USB security key. Hardware tokens are the most phishing-resistant because the code is tied to the physical device.
- Email – Some services send the code to your email inbox. This is less secure than an app because email accounts are often protected by the same weak password you are trying to protect.
How to Set Up OTP for Your Accounts
Most major services (Google, Microsoft, Facebook, banking apps) allow you to enable OTP-based 2FA. The general steps are:
- Open your account's security settings (look for "Security," "Two-Step Verification," or "2FA").
- Choose the method: SMS, authenticator app, or hardware key.
- If using an authenticator app, scan the QR code displayed on the screen with the app. The app will then show a 6-digit code.
- Enter the code from the app into the website to confirm the setup.
- Save the backup codes (or recovery codes) that the service provides – these are one-time codes you can use if you lose your phone.
Labels and menu names differ slightly by service and version, but the flow is consistent. Always store backup codes in a safe place, because if you lose your phone and have no backup codes, you can be locked out permanently.
Common Problems and Misconceptions
One frequent misconception is that OTPs are unbreakable. They are not. A sophisticated attacker can use a "phishing kit" that relays the OTP in real time to the real site (a "man-in-the-middle" attack). That is why hardware keys like YubiKey are recommended for high-value accounts – they use a different protocol that does not rely on a code you type.
Another issue is clock drift. If your phone's clock is wrong by more than a few seconds, TOTP codes will fail. Most authenticator apps sync with network time, but if you manually set your clock, you may see errors. The fix is to enable automatic time sync.
Also, SMS OTPs are not secure against SIM swapping. If an attacker convinces your carrier to port your number to a new SIM, they can receive your OTPs. For this reason, security experts recommend using an authenticator app instead of SMS whenever possible.
OTP vs. Other Authentication Methods
OTP is just one form of 2FA. Here is how it compares to others:
- Push notification – Instead of typing a code, you tap "Approve" on your phone. This is more convenient but can be abused by "push bombing" (spamming the user until they approve accidentally).
- Hardware security key (FIDO2/WebAuthn) – This is a physical key that uses public-key cryptography. It is more secure than OTP because it does not rely on a shared secret and is immune to phishing.
- Biometrics – Fingerprint or face recognition is convenient but can be spoofed in some cases and is not a true second factor if the device itself is compromised.
OTP sits in the middle: more secure than a static password alone, but less secure than a hardware key. For most consumers, an authenticator app is the best balance of security and convenience.
Safety and Best Practices
To use OTPs safely:
- Prefer an authenticator app over SMS. If a service only offers SMS, consider using a separate phone number for that account.
- Never share your OTP with anyone. Legitimate companies will never ask you for a code over the phone or via chat.
- Be wary of phishing emails that ask you to "verify" your account by entering a code on a fake site. Always type the URL yourself.
- Store backup codes in a password manager or a safe physical location.
- If you lose your phone, use your backup codes or the recovery process offered by the service. Do not attempt to bypass 2FA – that would defeat its purpose.
Frequently Asked Questions
Is an OTP the same as a password?
No. A password is something you choose and reuse; an OTP is generated by an algorithm and is valid only once. They are used together in 2FA.
Can OTPs be hacked?
Yes, OTPs can be intercepted in phishing attacks or via SMS interception. However, they are much harder to steal than static passwords because they expire quickly and are unpredictable.
Why do OTPs expire after 30 seconds?
That is the standard time window for TOTP. It reduces the window of opportunity for an attacker to use a stolen code. The server and app both compute the same code based on the current time, so they stay in sync.
What should I do if my OTP code is not working?
First, check that your device's time is set to automatic. If it still fails, try re-syncing the app (some apps have a "sync" or "refresh" option). If that does not help, use a backup code or contact the service's support.
Is SMS OTP safe?
It is better than no 2FA, but it is vulnerable to SIM swapping and SS7 attacks. Security experts recommend using an authenticator app instead.