Created
December 14, 2021 21:12
-
-
Save xoolive/ed8dde0c5920477eec77ca41b6618ad0 to your computer and use it in GitHub Desktop.
Read the contents of a covid pass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import sys | |
import zlib | |
import base45 | |
import cbor2 | |
import cv2 | |
from cose.messages import CoseMessage | |
# initialize the cv2 QRCode detector | |
detector = cv2.QRCodeDetector() | |
if len(sys.argv) > 1: | |
img = cv2.imread(sys.argv[1]) | |
data, bbox, other = detector.detectAndDecode(img) | |
print(data) | |
print(data, bbox) | |
payload = data[4:] | |
else: | |
cap = cv2.VideoCapture(0) | |
while True: | |
_, img = cap.read() | |
# detect and decode | |
data, bbox, other = detector.detectAndDecode(img) | |
# check if there is a QRCode in the image | |
if data: | |
payload = data[4:] | |
break | |
cv2.imshow("QRCODEscanner", img) | |
if cv2.waitKey(1) == ord("q"): | |
break | |
cap.release() | |
cv2.destroyAllWindows() | |
# decode Base45 (remove HC1: prefix) | |
decoded = base45.b45decode(payload) | |
# decompress using zlib | |
decompressed = zlib.decompress(decoded) | |
# decode COSE message (no signature verification done) | |
cose = CoseMessage.decode(decompressed) | |
# decode the CBOR encoded payload and print as json | |
print(json.dumps(cbor2.loads(cose.payload), indent=2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment