-
Notifications
You must be signed in to change notification settings - Fork 4
/
print_payload_qr.py
59 lines (47 loc) · 1.07 KB
/
print_payload_qr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/env python3.9
import sys
from base45 import b45decode
import zlib
from cbor2 import loads
from cose.messages import Sign1Message
from pyzbar.pyzbar import decode
from PIL import Image
import argparse
# Initialize components
CLI_PARSER = argparse.ArgumentParser()
def unpack_qr(qr_text):
compressed_bytes = b45decode(qr_text[4:])
cose_bytes = zlib.decompress(compressed_bytes)
cose_message = Sign1Message.decode(cose_bytes)
cbor_message = loads(cose_message.payload)
return {
"COSE": cose_message,
"JSON": cbor_message[-260][1]
}
def read_qr_pyzbar(file):
barcode = decode(Image.open(file))[0]
return barcode.data.decode("utf-8")
CLI_PARSER.add_argument(
'--file',
type=str,
help='QR file')
args = CLI_PARSER.parse_args()
if args.file is None:
CLI_PARSER.print_help()
sys.exit()
print()
print()
data = read_qr_pyzbar(args.file)
print("Raw QR data")
print(data)
json = unpack_qr(data)
print()
print("Hcert")
print(data)
print()
print("JSON")
print(json["JSON"])
print()
print("COSE")
print(json["COSE"])
print()