|
|
|
#!/usr/bin/env python2
|
|
|
|
# coding: utf
|
|
|
|
|
|
|
|
from Crypto.Hash import SHA256
|
|
|
|
from Crypto.PublicKey import RSA
|
|
|
|
from Crypto.Signature import PKCS1_v1_5
|
|
|
|
|
|
|
|
import qrcode
|
|
|
|
|
|
|
|
import base64
|
|
|
|
import json
|
|
|
|
|
|
|
|
PRIVATE_KEY_FILE = "domain.key"
|
|
|
|
|
|
|
|
BOX_SIZE = 5
|
|
|
|
BORDER = 2
|
|
|
|
|
|
|
|
|
|
|
|
def sign(message, private_key_str):
|
|
|
|
priv_key = RSA.importKey(private_key_str)
|
|
|
|
h = SHA256.new(message.encode("utf-8"))
|
|
|
|
signature = PKCS1_v1_5.new(priv_key).sign(h)
|
|
|
|
result = base64.b64encode(signature).decode()
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def create_qr(orderId, skuList, output_filename):
|
|
|
|
"""
|
|
|
|
Create a QR code for an order and write it as a PNG.
|
|
|
|
Params:
|
|
|
|
* orderId - order identifier.
|
|
|
|
* skuList - list of SKUs of the order.
|
|
|
|
* output_filename: the name of the output file.
|
|
|
|
"""
|
|
|
|
with open(PRIVATE_KEY_FILE, "r") as file:
|
|
|
|
key = file.read()
|
|
|
|
|
|
|
|
|
|
|
|
# dump to str to avoid ordering issues when verifying signature
|
|
|
|
order = json.dumps({"i": orderId, "sk": skuList})
|
|
|
|
signed_order = sign(order, key)
|
|
|
|
data = {"o": order, "s": signed_order}
|
|
|
|
|
|
|
|
data_str = json.dumps(data)
|
|
|
|
|
|
|
|
#data_str_compressed = zlib.compress(data_str)
|
|
|
|
|
|
|
|
qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=BOX_SIZE, border=BORDER)
|
|
|
|
qr.add_data(data_str)
|
|
|
|
qr.make(fit=True)
|
|
|
|
|
|
|
|
img = qr.make_image(fill_color="black", back_color="white")
|
|
|
|
img.save(output_filename)
|
|
|
|
|
|
|
|
|
|
|
|
create_qr("1234-5678", ["001", "002", "003", "004", "005", "006", "007", "008", "009"], "output.png")
|