Skip to content

Instantly share code, notes, and snippets.

@oldpatricka
Created September 8, 2021 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oldpatricka/e503676787ce5120dc727ca2e335a237 to your computer and use it in GitHub Desktop.
Save oldpatricka/e503676787ce5120dc727ca2e335a237 to your computer and use it in GitHub Desktop.
"""
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