pyauth / pyotp

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

Return time remaining in generate_otp() #69

Open jleclanche opened 5 years ago

jleclanche commented 5 years ago

I have the following code in my library:

def get_token(...):
    ...
    msg = struct.pack(">Q", int(time / seconds))
    r = hmac.new(secret, msg, sha1).digest()
    k = r[19]
    idx = k & 0x0f
    h = struct.unpack(">L", r[idx:idx + 4])[0] & 0x7fffffff
    return h % (10 ** digits), -(time % seconds - seconds)

I'm replacing it with pyotp to simplify things. Unfortunately, I do use that second return parameter, which is the time remaining for the code's validity. I use it to show how long the user's code is still valid for, and to decide when to generate a new one.

I understand why at() doesn't return the time remaining but having it in generate_otp() at the very least would make this functionality possible.

Thanks!

tilkinsc commented 5 years ago

You could also take the time block used and subtract seconds from it.

tilkinsc commented 4 years ago

87