Skip to content

Instantly share code, notes, and snippets.

@bulv1ne
Last active August 29, 2015 14:24
Show Gist options
  • Save bulv1ne/f66f1eb195fed5592969 to your computer and use it in GitHub Desktop.
Save bulv1ne/f66f1eb195fed5592969 to your computer and use it in GitHub Desktop.
Google Authenticator
# https://stackoverflow.com/questions/8529265/google-authenticator-implementation-in-python/8549884#8549884
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 # without ord in python3
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)
# http://blog.matael.org/writing/python-and-qrcodes/
# https://github.com/google/google-authenticator/wiki/Key-Uri-Format
# pip install pillow qrcode
import qrcode
img = qrcode.make('otpauth://totp/[email protected]?secret=testtesttesttest&issuer=test')
img.save('filename.png')
# django
def secureImage(request,imagePath):
response = HttpResponse(mimetype="image/png")
img = qrcode.make('otpauth://totp/[email protected]?secret=testtesttesttest&issuer=test')
img.save(response, 'png')
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment