A one-time passcode is a short code, worked out from a shared secret, that works only once.
This guide is for anyone who uses an authenticator app or code device and wonders how the numbers appear. You do not need maths. It explains the two main recipes, HOTP and TOTP, in plain words.
In short
- Your app and the service share a shared secret, usually set up by scanning a QR code once.
- HOTP mixes that secret with a counter that goes up each time you ask for a code.
- TOTP mixes it with the current time instead, so the code changes about every 30 seconds.
- Both sides do the same sum, so the codes match without the secret ever being sent again.
What you will learn
- Explain how a code is worked out from a secret and a moving number.
- Describe the difference between counter codes and time codes.
- Spot common problems, such as clock drift and shared secrets left in screenshots.
- Know where one-time codes fall short against phishing.
- Shared secret (set-up key)
- the hidden value your app and the service both keep.
- Counter code (HOTP)
- a code that changes each time you ask for one.
- Time code (TOTP)
- a code that changes every few seconds with the clock.
What are HOTP and TOTP?
Think of two friends with the same secret recipe book. Each day they turn to the page for that date and read the same word. Nobody needs to send the word, because both books give the same answer.
One-time passcodes work like this. Your app and the service both hold the same secret. Each mixes it with a moving number, then cuts the result down to a short code, often six digits.
The moving number is what makes the two recipes differ. In HOTP it is a counter that goes up one step each time a code is made. In TOTP it is the current time, cut into short steps.
Where the comparison stops working: anyone who copies the recipe book can read every future word. The same is true of the shared secret, so it must be guarded closely.
If you are new to authenticator apps, start with Authenticator Apps for Beginners. This page looks inside the code-making step.
Why does it matter?
One-time codes are one of the most common second checks at sign-in. Knowing how they work helps you set them up well and fix problems when codes stop matching.
If you use authenticator apps
What could happen: a screenshot of your set-up QR code is synced to a cloud photo album and copied.
Check this: delete any saved images of set-up codes, and store backup codes somewhere safe.
If you run a help desk
What could happen: a user's codes keep failing because the phone clock is wrong.
Check this: ask the user to set the phone clock to update automatically.
If you build sign-in systems
What could happen: attackers try many codes, or reuse one that has just worked.
Check this: limit failed tries and refuse any code that has already been accepted.
Next: D3-OTP One-time Password
What does it look like?
Here is a made-up set-up record. It shows the details hidden inside a set-up QR code.
Example only. All values are made up.
Account: EXAMPLE Shop ([email protected]) (1) Type: time-based (TOTP) (2) Secret: EXAMPLE-0000-FAKE (3) Digits: 6 (4) Period: 30 seconds (5) Code now: 000000 (FAKE) (6)
- Which account the codes are for.
- Whether codes follow the clock (TOTP) or a counter (HOTP).
- In a real file, this line would contain the shared secret, which must be kept private.
- How long each code is.
- How often a new time code appears.
- The code your app shows. Both sides work it out on their own.
Why this is risky: anyone who sees the secret can make the same codes as you. That lasts until the secret is reset.
The safer way: scan the QR code once and never save a picture of it. Reset the secret if you think it was seen.
How does it work?
- Share the secret once. The service creates a secret and shows it as a QR code. Your app saves it.
- Pick the moving number. HOTP uses a counter. TOTP uses the current time divided into steps.
- Mix and cut. The app mixes the secret and the moving number with a keyed hash, then cuts the result down to a short code.
- The service does the same. It uses its copy of the secret and its own counter or clock.
- Compare. If the two codes match, the check passes. The secret itself is never sent again.

HOTP was written up in an internet document in 2005. It mixes the secret and the counter using a keyed hash called HMAC, then cuts the result to at least six digits [1]. It asks for a shared secret of at least 128 bits [1].
A counter can get out of step. If you press the button on a code device without signing in, its counter moves on but the server's does not. So the server checks a small window of next counter values, which the document says should be kept as low as possible [1].
TOTP, written up in 2011, swaps the counter for time. It counts how many time steps have passed, with a default step of 30 seconds [2]. Clocks drift, so services usually accept a code from a step either side. The document recommends allowing at most one step for network delay [2].
Two more rules matter. The service must not accept the same time code a second time once it has worked [2]. And it should limit how many wrong guesses it allows [1].
A worked example
This story is made up to show the idea.
Reza Iyer works on the help desk at a small accounting firm. On Monday morning, his colleague Dario Rosen calls. His authenticator codes have stopped working, though he has not changed anything.
Reza asks Dario to read out the time shown on his phone. It is almost three minutes behind the office clock. Dario had switched off automatic time while travelling.
Reza explains that time codes depend on both sides agreeing on the time. A three-minute gap is several 30-second steps, so the service never sees a matching code.
Dario turns automatic time back on. His next code works first time.
While on the call, Reza asks one more question. How did Dario move his codes to his new phone last month? Dario admits he took a photo of the set-up QR code, just in case.
Reza walks him through resetting his sign-in check. The service issues a new secret, Dario scans it once, and he deletes the old photo from his phone and his cloud album.
Reza adds two lines to the help desk notes. Check the phone clock first. Ask about saved QR codes when anyone sets up a new phone.
Myths and mistakes
| What people think | What is true |
|---|---|
| The code is sent to my app from the service. | No code is sent. Your app works it out from the shared secret and a counter or the clock. |
| A time code needs an internet connection. | The app only needs the secret and a correct clock. The connection matters only when you type the code into the service. |
| A screenshot of the QR code is a safe backup. | The QR code holds the secret. Anyone with the picture can make your codes. |
| One-time codes stop all phishing. | A fake site can ask for your code and use it straight away. Phishing-resistant options such as passkeys help here. |
| A longer time step is more convenient and just as safe. | A longer step gives an attacker more time to use a stolen code[2]. |
What can I do this week?
- Check that your phone clock is set to update automatically.
- Search your photos and cloud album for saved set-up QR codes, and delete them.
- Store your backup codes in a password manager or another safe place.
- Turn on a one-time code check for one important account that does not have one yet.
Checklist
- I can explain that codes come from a shared secret plus a moving number.
- I know HOTP uses a counter and TOTP uses the time.
- I keep my phone clock set automatically.
- I do not keep pictures of set-up QR codes.
- I have my backup codes stored safely.
- I know a fake site can still ask for my code.
- If I build sign-in, I limit wrong guesses and refuse reused codes.
Common questions
What is the difference between HOTP and TOTP?
HOTP uses a counter that goes up each time a code is made. TOTP uses the current time, usually in 30-second steps[2]. Most phone authenticator apps use TOTP. Some code devices with a button use HOTP.
Why do my codes stop working?
Most often the phone clock is wrong, so the app and service disagree on the time step. Set the clock to update automatically. For counter codes, pressing the button many times can push the device out of step with the service.
Why are codes six digits?
Six digits are easy to type, and the HOTP document sets six as the minimum[1]. Short codes are only safe because each one works once, for a short time, and services limit wrong guesses.
Can someone guess my code?
A random guess has about a one in a million chance with six digits. Services should limit how many wrong tries they accept[1], which keeps guessing impractical. The bigger risks are a stolen secret or a fake site asking for your code.
Are text message codes the same thing?
Not quite. A text message code is made by the service and sent to your phone. An authenticator app makes the code itself from the shared secret. App codes do not depend on your phone number, so they avoid some text message risks.
Where next?
Ready for the detail? Read the practitioner guide: Authenticator Apps and OTP Guide. It covers set-up, recovery and how teams run one-time codes at scale.
Also useful:
- Authenticator Apps for Beginners
- D3-OTP One-time Password
- OTP Bots and Real-Time OTP Relay
- 2FA vs Two-Step Verification vs MFA
Related NHI Mgmt Group resources: Replay Attacks for Beginners and Magic Links and One-Time Codes for Beginners.
Key terms
- One-time password
- A code that works for a single sign-in or a short time, then cannot be used again.
- HOTP
- A one-time passcode recipe that mixes the shared secret with a counter that goes up each time you ask for a code.
- TOTP
- A one-time passcode recipe that mixes the shared secret with the current time, usually in 30-second steps.
- A secret value known to both your device and the service, used to work out matching codes.
- HMAC
- A way of mixing a secret key with data to make a value that only someone with the key could produce.
- Multi-factor authentication
- Signing in with two or more different kinds of proof, such as a password and a one-time code.
Sources and further reading
- IETF, "RFC 4226: HOTP: An HMAC-Based One-Time Password Algorithm", Informational, December 2005. https://www.rfc-editor.org/rfc/rfc4226 (accessed 30 Nov 2026).
- IETF, "RFC 6238: TOTP: Time-Based One-Time Password Algorithm", Informational, May 2011. https://www.rfc-editor.org/rfc/rfc6238 (accessed 30 Nov 2026).