Created
September 8, 2021 03:35
-
-
Save oldpatricka/e503676787ce5120dc727ca2e335a237 to your computer and use it in GitHub Desktop.
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
""" | |
Install dependencies with: | |
brew install zbar | |
virtualenv venv ; source venv/bin/activate | |
pip install pyzbar pillow | |
run with | |
python read-qr.py qr.png | |
copies heavily from https://github.com/marcan2020/shc-decoder-poc/blob/main/shc-decoder-poc.py | |
""" | |
import json | |
import sys | |
import base64 | |
import re | |
import zlib | |
from pyzbar.pyzbar import decode | |
from PIL import Image | |
qrfile = sys.argv[1] | |
qr = decode(Image.open(qrfile)) | |
assert len(qr) == 1 | |
qr_data = qr[0].data.decode('utf-8') | |
parts = re.findall("..", qr_data[5:]) | |
jws = "" | |
for p in parts: | |
jws += chr(int(p)+ 45) | |
def decode(data): | |
missing_padding = len(data) % 4 | |
if missing_padding: | |
data += "="* (4 - missing_padding) | |
return base64.urlsafe_b64decode(data) | |
jws_parts = list(map(decode, jws.split("."))) | |
print("JWS Header:") | |
print(json.dumps(json.loads(jws_parts[0]), indent=2)) | |
# https://bugs.python.org/issue5784 | |
shc_data = zlib.decompress(jws_parts[1], wbits=-15) | |
print("SHC Data:") | |
print(json.dumps(json.loads(shc_data), indent=2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment