Created
October 9, 2015 13:19
-
-
Save matoken/8159502c4a87dc3b0341 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/python | |
import hmac, base64, struct, hashlib, time, os, sys | |
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( len(sys.argv) > 1): | |
secret = sys.argv[1] | |
elif( os.path.exists( os.environ["HOME"] + "/.google_authenticator" )): | |
f = open( os.environ["HOME"] + "/.google_authenticator" ) | |
secret = f.readline().replace('\n','') | |
else: | |
print sys.argv[0] + " SECRETKEY" | |
exit(-1) | |
#for i in xrange(1, 10): | |
# print i, get_hotp_token(secret, intervals_no=i) | |
print get_totp_token(secret) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment