Skip to content

Instantly share code, notes, and snippets.

@zzzeek
Created April 12, 2013 22:53
Show Gist options
  • Select an option

  • Save zzzeek/5375840 to your computer and use it in GitHub Desktop.

Select an option

Save zzzeek/5375840 to your computer and use it in GitHub Desktop.
two factor auth. stick your various provider's one-time keys in the dictionary there (the thing you get from the QR code)
#!/usr/local/bin/python
keys = {
'some_provider':'SOMEKEYTHATISABIGSTRINGRIGHTHERE'
}
import hmac, base64, struct, hashlib, time
def get_hotp_token(secret, intervals_no):
key = base64.b32decode(secret, True)
msg = struct.pack(">Q", intervals_no)
h = hmac.new(key, msg, hashlib.sha1).digest()
o = ord(h[19]) & 15
h = (struct.unpack(">I", h[o:o+4])[0] & 0x7fffffff) % 1000000
return h
def get_totp_token(secret):
return get_hotp_token(secret, intervals_no=int(time.time())//30)
if __name__ == '__main__':
for key, value in keys.items():
print key, "%.6d" % get_totp_token(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment