Skip to content

Instantly share code, notes, and snippets.

@flyte
Created December 18, 2013 12:46
Show Gist options
  • Save flyte/8021745 to your computer and use it in GitHub Desktop.
Save flyte/8021745 to your computer and use it in GitHub Desktop.
Generate/validate a QR code value.
"""
Easy version...
def qr_hash(prefix, data_to_hash):
qr = ""
for char in prefix:
qr += hex(ord(char))[2:]
qr += md5(data_to_hash).hexdigest()
return qr
def verify_qr(prefix, qr):
prefix_hex = ""
for char in prefix:
prefix_hex += hex(ord(char))[2:]
return qr.startswith(prefix_hex)
"""
from hashlib import md5
asciihex = lambda string: "".join([hex(ord(x))[2:] for x in string])
def qr_hash(prefix, data_to_hash):
return asciihex(prefix) + md5(data_to_hash).hexdigest()
def verify_qr(prefix, qr):
return qr.startswith(asciihex(prefix))
"""
In [1]: qr = qr_hash("ABC", "Hello World!")
In [2]: qr
Out[2]: '414243ed076287532e86365e841e92bfc50d8c'
In [3]: verify_qr("ABC", qr)
Out[3]: True
In [4]: verify_qr("XYZ", qr)
Out[4]: False
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment