Skip to content

Instantly share code, notes, and snippets.

@aniline
Created September 12, 2017 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aniline/d5e11d8d20cd96f2f5d7ae731566be94 to your computer and use it in GitHub Desktop.
Save aniline/d5e11d8d20cd96f2f5d7ae731566be94 to your computer and use it in GitHub Desktop.
TOTP like Google Authenticator
import hashlib, hmac, base64, struct, time, math
def GoogleTOTP(s):
msg = int(math.floor(time.time()/30))
hm = hmac.new(base64.b32decode(s), struct.pack('>q', msg), hashlib.sha1).digest()
offs = ord(hm[-1]) & 0xF
code = str(((struct.unpack('>L',hm[offs:offs+4])[0]) & 0x7FFFFFFF) % 1000000).zfill(6)
return code
print GoogleTOTP("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment