Re:How does Google Authenticator work?
by jcims on Friday August 27, 2021 @03:25PM
(#28331980)
You can also roll your own with https://github.com/pyauth/pyotp
[ Parent
| on HN ]
It's almost overkill to bother with a library.
#!/usr/bin/env python3
import time, hmac, base64, struct
def totp(seed,curtime,period,len):
k = base64.b32decode(seed)
mac = hmac.new(k, struct.pack('>Q', int(curtime/period)), 'sha1').digest()
offset=mac[-1] & 0x0f
otp=struct.unpack('>L', mac[offset:offset+4])[0] & 0x7fffffff
return str(otp)[-len:].zfill(len)
myseed='KRUGS4ZANFZSAYJAOJQW4ZDPNUQHG5DS' # use gpg or similar to store
print(totp(myseed,time.time(),60,6))True, I also type this in from memory whenever I need a quick TOTP calculation.
Meaning the core TOTP piece isn't much code, and a library might not store/encrypt the seed, display the data, etc, in a way that you want. The bulk of the code for an authenticator app isn't typically the TOTP bit.
You forgot to add the test cases :)
Re:How does Google Authenticator work?
by codetrotter on Friday August 27, 2021 @03:35PM
(#28332087)
> roll your own
Libraries for a couple of other languages: