Last active
November 27, 2015 09:17
-
-
Save matoken/e82e309c4f34e572a80d 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): | |
try: | |
key = base64.b32decode(secret, True) | |
except: | |
print "invalid Secret Key!" | |
return(-1) | |
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) | |
code = get_totp_token(secret) | |
if( code == -1 ): | |
exit(-1) | |
print( "%06d" % code ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment