Skip to content

Instantly share code, notes, and snippets.

@system123
Created March 2, 2021 17:09
Show Gist options
  • Save system123/0eb90fed5e41d94f0f297f3c6e6f4ac0 to your computer and use it in GitHub Desktop.
Save system123/0eb90fed5e41d94f0f297f3c6e6f4ac0 to your computer and use it in GitHub Desktop.
Google Auth TOTP code generator, just seed it with your MFA secret key and it'll generate codes exactly as Google Auth does.
def get_totp_token(secret, refresh_time=int(time.time())//30):
secret = secret.strip().replace(' ', '')
secret = secret.ljust((len(secret) + 7) & (-8) ,'=')
key = base64.b32decode(secret, True)
msg = struct.pack(">Q", refresh_time)
h = bytearray(hmac.new(key, msg, hashlib.sha1).digest())
o = h[19] & 15
h = str((struct.unpack(">I", h[o:o+4])[0] & 0x7fffffff) % 1000000)
return h.zfill(6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment