Skip to content

Instantly share code, notes, and snippets.

@adamgreig
Created August 14, 2013 18:18
Show Gist options
  • Save adamgreig/6233882 to your computer and use it in GitHub Desktop.
Save adamgreig/6233882 to your computer and use it in GitHub Desktop.
TOPT Codes
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