๐ข HOTP (Counter-Based) Code Generator
Enter a secret and counter
Generate an HOTP (HMAC-based one-time password), defined in RFC 4226, from a Base32-encoded secret and a counter value. Unlike TOTP, which derives its code from the current time, HOTP derives it from an explicit counter that increments by 1 with each use โ the scheme used by many hardware tokens.
How to use
- Enter the Base32-encoded secret key.
- Enter a counter value (typically starting at 0 and incrementing by 1 with each authentication).
- The generated one-time password is shown.
How the calculation works
HOTP (HMAC-based One-Time Password), defined in RFC 4226, generates one-time passwords from a counter. TOTP is HOTP with the counter replaced by the time. 1. Encode the counter as an 8-byte big-endian number. 2. Compute the HMAC-SHA1 of the counter with the Base32 secret (20 bytes). 3. Use the low 4 bits of the last byte as an offset, read 4 bytes from there, and drop the top bit to get a 31-bit integer (dynamic truncation). 4. Take that integer modulo 10โถ for a six-digit code. Each time a code is used, both the server and the device advance the counter by one. The results have been checked against the test values in Appendix D of RFC 4226.
Worked example
RFC 4226 Appendix D (secret "12345678901234567890", 6 digits) Counter 0: 755224 Counter 1: 287082 Counter 2: 359152 Counter 3: 969429 โฆ Counter 9: 520489 Increase the counter by one at a time to watch the codes change.
Things to be aware of
- Pressing the button on a device without using the code moves only the device's counter ahead. Most servers accept a few counters ahead and resynchronise.
- HOTP codes stay valid until used, which is why time-limited TOTP is more common.
- Never show the secret for a real service to anyone.
FAQ
What's the difference between HOTP and TOTP?
Both use the same RFC 4226 HMAC-based OTP mechanism, but TOTP (RFC 6238) uses the current time divided into fixed intervals in place of a counter, while HOTP uses an explicit counter (typically incremented by 1 on each successful authentication).
Why do I need to increment the counter manually?
If the server and client counters drift out of sync, the generated code won't match. In real deployments, both sides increment the counter together on successful authentication. This tool includes manual ยฑ1 buttons for testing.
Has this been verified against the RFC 4226 test vectors?
Yes โ the implementation exactly matches the official test vectors in RFC 4226 Appendix D (secret "12345678901234567890", counters 0 through 9).