Commits

Thejesh GN committed 0cb5fc3

fixed hashing and added QR code url

  • Participants
  • Parent commits d2a1bbe

Comments (0)

Files changed (1)

+#!/usr/bin/python
 ###############################################################################
 # Repo: http://code.thejeshgn.com/g2fa
 # Copyright (C) 2012 Thejesh GB <i@thejeshgn.com>
 import math
 import struct
 import hashlib, hmac
+import binascii
 
 period = 30 #interval between two generated keys
 length = 6  #length of OTP
 
 
 def otp(key, time):
-	bin_counter = struct.pack('I', 0) + struct.pack('I', time)
-	hash= hmac.new(key)
-	digest = hmac.new(key, bin_counter, hashlib.sha512).hexdigest()
-	result = str(oath_truncate(digest)).ljust(6, '0')
+	bin_counter = struct.pack('!L', 0)+struct.pack('!L', time)
+	# values = (time)
+	# s = struct.Struct('!L')
+	# packed_data = s.pack(values)
+	# print 'Original values:', values
+	# print 'Format string  :', s.format
+	# print 'Uses           :', s.size, 'bytes'
+	# print 'Packed Value   :', binascii.hexlify(packed_data)
+	digest = hmac.new(key, bin_counter, hashlib.sha1)
+	# print digest.hexdigest()
+	result = str(truncate(digest.digest())).ljust(length, '0')
 	return result
 
-def oath_truncate(hash):
+def truncate(hash):
 	offset = ord(hash[19]) & 0xf;
 	return (
 	    ((ord(hash[offset+0]) & 0x7f) << 24 ) |
 
 #This is for testing only
 if __name__ == "__main__":
-
-	TIME_STAMP = 44376375  #timestamp()
-	YOUR_SECRET_INITIAL_KEY = "PEHMPSDNLXIOG65U"   # must be at least 16 base 32 characters, keep this secret
+	TIME_STAMP = timestamp()
+	YOUR_SECRET_INITIAL_KEY = "KKK67SDNLXIOG65U"   # must be at least 16 base 32 characters, keep this secret
 	SECRET_KEY 	  = decode(YOUR_SECRET_INITIAL_KEY)
-	print TIME_STAMP
-	print YOUR_SECRET_INITIAL_KEY
-	print SECRET_KEY
 	o =  otp(SECRET_KEY, TIME_STAMP)
-	print o
+	print "TESTING PURPOSE"
+	print "TIME_STAMP ="+str(TIME_STAMP)
+	print "OTP ="+str(o)
+	YOUR_SITE = "g2fa-example.com"
+	print "http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=otpauth://totp/"+YOUR_SITE+"?secret="+YOUR_SECRET_INITIAL_KEY+"&chld=H|0"