pyauth / pyotp

Python One-Time Password Library
https://pyauth.github.io/pyotp/
Other
3.03k stars 326 forks source link

Confusion about positive integer #166

Closed edent closed 5 months ago

edent commented 5 months ago

Running totp.at(-29) produces a TOTP code. Running totp.at(-30) correctly raises "ValueError("input must be positive integer")" as per: https://github.com/pyauth/pyotp/blob/249674bf2814e1fcc3152c618514159cd44b4610/src/pyotp/otp.py#L28-L34

I'm just curious as to why some negative numbers work.

peterthomassen commented 5 months ago

This is because the interval division is subject to a rounding error for inputs smaller than the interval: https://github.com/pyauth/pyotp/blob/249674bf2814e1fcc3152c618514159cd44b4610/src/pyotp/totp.py#L113-L116

So it seems that values in the range [1-interval, -1] "work". (For the default interval of 30, this is from -29 up to -1).