Skip to Content
oxcrypto

HOTP

Counter-based codes from RFC 4226.

from oxcrypto.otp.hotp import HOTP

secret = b"12345678901234567890"
assert HOTP.SHA1.generate(secret, counter=1) == "287082"
assert HOTP.SHA1.verify(secret, "287082", counter=0, window=2)
assert HOTP.SHA1.match(secret, "287082", counter=0, window=2) == 1

verify and match check counters counter to counter + window. verify returns whether one matches and does not record it, so it accepts the same code again. match returns the first matching counter, or None; store it and pass the next counter to the next call to reject reuse. A match at counter 0 returns 0, so test the result with is not None.

Variants

  • HOTP.SHA1 (the RFC 4226 algorithm)
  • HOTP.SHA256
  • HOTP.SHA512

Methods

generate(secret: bytes, /, *, counter: int, digits: int = 6) -> str
verify(secret: bytes, token: str, /, *, counter: int, digits: int = 6, window: int = 0) -> bool
match(secret: bytes, token: str, /, *, counter: int, digits: int = 6, window: int = 0) -> int | None

On this page