Skip to content

Instantly share code, notes, and snippets.

@jonaslindmark
Created June 7, 2016 06:41
Show Gist options
  • Save jonaslindmark/e506fa5fff4e99fbce25a8299e9dcf0b to your computer and use it in GitHub Desktop.
Save jonaslindmark/e506fa5fff4e99fbce25a8299e9dcf0b to your computer and use it in GitHub Desktop.
import base64
import hashlib
import hmac
import struct
import sys
import 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)
secret = sys.argv[1]
token = str(get_totp_token(secret))
diff = 6 - len(token)
print ("0" * diff) + token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment