Google Two Factor Authenticator

Description

This is a Google Two Factor Authenticator script that allows for you to generate a verification code to get into your account

More information about this is available here.

Libraries Used

* No External Libraries were used

Usage

python googleotp.py <Google Secret Key>

EXAMPLE:
python googleotp.py AAAAAAA123AAAAAA

The Code

-={ Click here to show the code. }=-

-={ Click here to hide the code. }=-

import hashlib
import hmac
import binascii
import time
import datetime
import calendar
import base64
 
# Google Two-Factor Passcode Generator
#
# Based off https://github.com/bdauvergne/python-oath/blob/master/hotp.py
#
#
# Original hotp.py copyright
#
# Python implementation of HOTP and TOTP algorithms from the OATH project.
# 
# Copyright 2010, Benjamin Dauvergne
# 
# * All rights reserved.
# * Redistribution and use in source and binary forms, with or without
# * modification, are permitted provided that the following conditions are met:
# 
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
 
def usage():
	print "python %s <Google Secret key>" % ( sys.argv[0] )
	print ""
	print "Parameters"
	print "----------"
	print "Google Secret key - 16 chars A-Z 2-7, looks something like this AAAAAAA123AAAAAA"
	print ""
	print "Example"
	print "-------"
	print "python %s AAAAAAA123AAAAAA" % ( sys.argv[0] )
 
def truncated_value(h):
	bytes = map(ord, h)
	offset = bytes[19] & 0xf
	v = (bytes[offset] & 0x7f) << 24 | (bytes[offset+1] & 0xff) << 16 | \
			(bytes[offset+2] & 0xff) << 8 | (bytes[offset+3] & 0xff)
	return v
 
def dec(h,p):
	v = str(truncated_value(h))
	return v[len(v)-p:]
 
def __hotp(key, counter, hash=hashlib.sha1):
	hex_counter = hex(long(counter))[2:-1]
	hex_counter = '0' * (16 - len(hex_counter)) + hex_counter
	bin_counter = binascii.unhexlify(hex_counter)
	bin_key = base64.b32decode(key.upper())
 
	return hmac.new(bin_key, bin_counter, hash).digest()
 
def hotp(key,counter,format='dec6',hash=hashlib.sha1):
	'''
	Compute a HOTP value as prescribed by RFC4226
	See http://tools.ietf.org/html/rfc4226
	'''
	bin_hotp = __hotp(key, counter, hash)
 
	if format == 'hex40':
		return binascii.hexlify(bin_hotp[0:5])
	elif format == 'dec4':
		return dec(bin_hotp, 4)
	elif format == 'dec6':
		return dec(bin_hotp, 6)
	elif format == 'dec7':
		return dec(bin_hotp, 7)
	elif format == 'dec8':
		return dec(bin_hotp, 8)
	else:
		raise ValueError('unknown format')
 
def totp(key, format='dec8', period=30, t=None, hash=hashlib.sha1):
	'''Compute a TOTP value as prescribed by OATH specifications.
	See http://tools.ietf.org/html/draft-mraihi-totp-timebased-06
	'''
	if t is None:
		t = time.time()
	else:
		if isinstance(t, datetime.datetime):
			t = calendar.timegm(t.utctimetuple())
		else:
			t = int(t)
	T = int(t/period)
	return hotp(key, T, format=format, hash=hash)
 
if __name__ == '__main__':
	import sys
	if len(sys.argv) > 1:
		print totp(sys.argv[1], format='dec6')
	else:
		usage()

Download

 
 googletwofactor.txt · Last modified: 2011/03/07 20:24 by bpienig
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
Recent changes RSS feed Driven by DokuWiki