Skip to content

Instantly share code, notes, and snippets.

@toshsan
Forked from adamgreig/gist:6233882
Last active August 29, 2015 14:01
Show Gist options
  • Save toshsan/7faf3f67e6768196b7ad to your computer and use it in GitHub Desktop.
Save toshsan/7faf3f67e6768196b7ad to your computer and use it in GitHub Desktop.
TOPT Code generator, for use with Google Authenticator
import time
import hmac
import struct
import base64
import hashlib
def code(secret):
key = base64.b32decode(secret)
message = struct.pack(">Q", int(time.time()) / 30)
h = hmac.new(key, message, hashlib.sha1).digest()
offset = ord(h[19]) & 0xF
code = (struct.unpack(">I", h[offset:offset+4])[0] & 0x7FFFFFFF) % 1E6
print "{0:06n}".format(code)
code("AAAAAAAAAAAAAAAA")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment