TOTP
Time-based codes from RFC 6238.
from oxcrypto.otp.totp import TOTP
secret = b"12345678901234567890"
assert TOTP.SHA1.generate(secret, time=59, digits=8) == "94287082"
assert TOTP.SHA1.verify(secret, "94287082", time=89, digits=8)
assert TOTP.SHA1.match(secret, "94287082", time=89, digits=8) == 1time is Unix time in seconds and defaults to the system clock. verify and
match accept codes from skew periods before or after time. verify
returns whether the code matches and does not record it, so it accepts the
same code again within the window. match returns the matching time step,
time // period, or None; store it and accept only larger time steps next
time to reject reuse. Test the result with is not None, because step 0 is
0.
Variants
TOTP.SHA1TOTP.SHA256TOTP.SHA512
Methods
generate(secret: bytes, /, *, time: int | None = None, digits: int = 6, period: int = 30) -> str
verify(secret: bytes, token: str, /, *, time: int | None = None, digits: int = 6, period: int = 30, skew: int = 1) -> bool
match(secret: bytes, token: str, /, *, time: int | None = None, digits: int = 6, period: int = 30, skew: int = 1) -> int | None