Created
June 7, 2016 06:41
-
-
Save jonaslindmark/e506fa5fff4e99fbce25a8299e9dcf0b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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