TechKnowSurge
NIST 800-53 IA-5 CompTIA Security+ 1.4 ISC2 CISSP 5.6 CompTIA SecurityX 2.1 CompTIA Security+ 4.6 NIST 800-53 SC-45
VideoSecurityFree

HOTP and TOTP

HOTP and TOTP are two widely used one-time password protocols that allow an authenticator app and a server to independently generate matching codes without direct communication. HOTP uses an incrementing counter while TOTP uses the current time, with both methods relying on a shared secret key and HMAC-based hashing to produce short, truncated authentication codes.

Complete this video to capture a CTF flag worth 1 point.

About this video

One-time passwords are credentials valid for a single use or a brief time window, providing stronger authentication than static passwords. They are typically delivered through SMS or generated by an authenticator app installed on a user's device. SMS delivery is straightforward: the server sends a code directly to the user, who enters it to authenticate. Authenticator apps work differently — the app and server must arrive at the same code independently, without transmitting it, which is where HOTP and TOTP come in. HMAC-Based One-Time Password, or HOTP, generates codes by hashing a shared secret key together with a counter value using a cryptographic hashing algorithm. The resulting hash is a large hexadecimal fingerprint that gets truncated down to the number of digits required for authentication, typically six. Both the server and the authenticator app hold the same key, exchanged once during setup, and both increment the same counter each time a code is used. Because users sometimes generate a code without logging in, the counter on the app can advance ahead of the server's counter. HOTP addresses this with a lookahead window, allowing the server to check a range of future counter values until it finds a match. Time-Based One-Time Password, or TOTP, follows the same hashing and truncation process but replaces the counter with a time value, generating codes that rotate on a fixed interval such as every 30 seconds. Since both the server and the device derive the code from the current time, they stay synchronized without needing to track login events. Minor clock differences between devices are handled through a lookup window that accepts codes falling within a short time range on either side of the server's current value. In both protocols, the shared secret key must be provisioned to the authenticator app before use. This is done either by manually entering the key into the app or by scanning a QR code displayed by the server, which encodes the key and all necessary configuration data. Once the key is in place, the app and server can generate matching codes independently for the lifetime of the account registration.

What you'll learn

What's covered

HOTP and TOTP

Aligned to

NIST 800-53
IA-5 Authenticator Management
SC-45 System Time Synchronization
CompTIA Security+
1.4 Explain the importance of using appropriate cryptographic solutions.
4.6 Given a scenario, implement and maintain identity and access management.
ISC2 CISSP
5.6 Implement authentication systems
CompTIA SecurityX
2.1 Given a scenario, analyze the security requirements and objectives to provide the appropriate authentication and authorization controls.

Key terms

HMAC-based One-time Password
HOTP
HMAC-based One-time Password is an algorithm that generates a one-time code using HMAC-SHA1 and a counter value, used in hardware tokens for multi-factor authentication.
Time-based One-time Password
TOTP
Time-based One-time Password generates short-lived authentication codes using HMAC and the current Unix time, widely used in authenticator apps as a second factor.
One-time Password
OTP
A One-time Password is a code valid for only a single authentication session or transaction, generated by hardware tokens, authenticator apps, or SMS, providing stronger security than static passwords by eliminating the risk of credential replay attacks.
Shared Secret Key
A cryptographic key known only to the authenticator app and the server, used as a common input to generate matching one-time passwords.
Counter Drift
A synchronization problem in HOTP where the client counter advances without a corresponding login, causing the client and server counters to fall out of sync.
Look-Ahead Window
A configurable tolerance in HOTP that allows the server to check a range of future counter values to compensate for counter drift.
Clock Skew
A time synchronization problem in TOTP where slight differences between the client and server system clocks can cause generated codes to not match.
Hash Function
A mathematical algorithm that converts input data of any size into a fixed-size output value used to verify data integrity.

Topics

Hotp Totp One Time Passwords Multi Factor Authentication Hmac Cryptography

Transcript

Two very common implementations of one-time passwords, or OTP, would be HOTP and TOTP.

A one-time password is just a password that you would use once. You're given the password, once you log in then it's no longer valid, or perhaps it expires within a certain time frame.

Two common ways to receive a one-time password would be either through SMS, think of like text messages with this, so if you receive a code through text message to log in and you're given maybe like 15 minutes to log into the system, then that would be an example of SMS. And then there's an authenticator app, an actual app that you would install on your device that would give you these codes.

So here's the thing: whatever the server is expecting, you need to know on your side. So this is your side right here, this is the server side. Now when it comes to SMS, that's pretty straightforward. You have a server here, it sends out a text message to you and you're going to get a text message. In this case it's 134 278, and so you get that text message, you enter it in to authenticate, and now you're logged in because the two match.

But what happens when it's an authenticator app? How does the authenticator app know what the number is compared to what the server has, that they need to be on the same page? And they're not communicating, it's not being sent to the authenticator app. What's happening usually is that the authenticator app generates this number, as well as the server generates this number, and they're on the same page. But how does that happen? Well, HOTP and TOTP are two solutions that help with this. They are two solutions that help coordinate between devices that are not communicating, but be able to come up with the same number.

HMAC-Based One-Time Password

We're going to take a look at HMAC-based one-time password, or HOTP. In order to understand HOTP, we have to understand what a hash is. Essentially there's a hashing algorithm, something that's going to take in data and generate a unique fingerprint, a unique fingerprint in the form of letters and numbers. In this case right here it's some sort of hexadecimal number. It's going to generate this unique fingerprint based off of the data that's put into it.

HOTP is going to hash the key and the counter together to generate this fingerprint right here. So what the key is, is it's a known value, a key that only this server and the authenticator app know what this key is going to be. And then you have a counter, and we'll discuss the counter here in a little bit, but essentially these two get hashed together to create a fingerprint. And then it gets truncated, because this is a very large number right here, so it gets truncated down into the number of digits that you need for the actual authentication. In this case right here this is a six-digit authentication.

The key is exchanged ahead of time, so both the authenticator app and the server know what the key is. And the counter just counts up, so it starts at zero, then 1, 2, 3, 4, 5, 6, 7, and so on and so forth. And so the count number plus the key, or the key and the count, are combined or hashed together, truncated, to come up with these numbers. So then what will happen is every time you generate a new key it will count up. So here's one right here, and then here's two, the second, the third time you log in, the fourth time, the fifth time, the sixth time, and so on and so forth. And so what the server does is keep track of every time that you have logged in, and then it will compare those two.

Now there is a problem with this method. Let's say we're on the counter of three right here, and what happens is we open up the app and we see this number right here, but then we close the app and we don't actually log in. The next time we open up that app it's going to count up, so now we're on four, and then same thing five. If we just keep counting up without logging in, what's going to happen is that the two are going to get off. The authenticator app and the server are going to be off. And so what the server is going to do when you try to log in with 958 675, it could reject you, because the fact is that it's still on three and so it's looking for 034 278.

So there is a look ahead that we can, with this protocol, be able to specify. We're going to look ahead a certain amount of counts ahead, and so now what's going to happen is if we enter in this number right here, it's going to start looking ahead to see if it can find the right one. Once it finds the right one, then it will accept you in. So this is how it compensates for that issue if the counters get off.

Time-Based One-Time Password

Time-based one-time password, or TOTP, is very similar. The only difference is instead of having a counter that counts up every time we try to use one of these numbers, 0, 1, 2, 3, instead we use time. Now there's an equation that this time goes through to calculate it out into this hash. We're not as concerned with whatever algorithm that goes into creating that time, but just realize that what's happening here is the key is combined with time to create that unique fingerprint, that hash, and then it gets truncated down into a smaller number, whatever number that we're looking for.

So now we're using time rather than a counter. So rather than counting up 0, 1, 2, 3, instead what's going to happen is it's going to be based off of time. So we've got 0, 30 seconds, 1 minute, 1 minute 30 seconds, 2 minutes, and so on and so forth.

Now the thing is, it's based off of the system times, so the system times of these two devices could be different. Now hopefully they're syncing to the cloud, so they should be relatively the same, but clocks do get off by a little bit. And so how do we compensate for that? Well, let's say the phone is at this 2-minute mark right here and the server right here is at 1:30. What's going to happen is there's going to be a lookup window, so whatever you type in here, that server is going to compare to plus or minus a certain amount of time. And so as long as your code there lines up with the plus or minus of whatever the server is set up to look in, then you should be good to go.

Syncing the Keys

We still have one unanswered question here, and that is how do we sync these keys? How do we have the same keys on these two devices?

Well, there's two answers to that. One is we could just manually enter it in on the device. We have some sort of authenticator app, and within the authenticator app we would open it up and one of the things it would ask us for is the key. We could enter in that key and then we would have it synced up, the keys on both sides.

Another way is through a QR code. Perhaps we visit a website, and with that website it's going to just display a QR code that's coming from the server. So now we have a QR code, we scan that on our phone into the authenticator app, and what is embedded onto that is information such as the key, on how to set up this connection.

About TechKnowSurge

TechKnowSurge builds IT and cybersecurity professionals through hands-on, concept-first training built around real understanding — not memorization. Free interactive tools, structured programs, and 25+ years of real-world experience, all in one place.

Explore free tools and programs →