Reputation: 1291
I'm currently using the 'Google Authenticator' app with a site that I'm working on and I was wondering if it's possible (whether Google provides it or not) to write unit tests that shows some possible outputs of the dual authentication program.
Rather than using a phone to generate the numeric code, I was wondering if there was also a class that allows me to generate it on my computer so that I could unit test the results.
Upvotes: 3
Views: 1336
Reputation: 2023
You can generate codes if you know the pre-shared secret string (one being encoded in QR code you scan with the Authenticator app). Here's a code in Python (via @Tadeck, GitHub):
import hmac, base64, struct, hashlib, 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 = 'MZXW633PN5XW6MZX'
for i in xrange(1, 10):
print i, get_hotp_token(secret, intervals_no=i)
Upvotes: 2