Skip to content

Instantly share code, notes, and snippets.

@robert-nix
Last active November 29, 2021 17:49
Show Gist options
  • Save robert-nix/733c14a21c7912450f8b3857c952c9a1 to your computer and use it in GitHub Desktop.
Save robert-nix/733c14a21c7912450f8b3857c952c9a1 to your computer and use it in GitHub Desktop.
convert a smart health card url to JSON
# Decode Smart Health Card URL
import argparse
import base64
import json
import zlib
from urllib.parse import urlparse
parser = argparse.ArgumentParser(description='Decode Smart Health Card URL')
parser.add_argument('url', help='URL to decode')
def unzlib(data):
"""inflate a headerless and crcless deflated data
"""
return zlib.decompress(data, wbits=-15)
def main():
args = parser.parse_args()
# get the decimal-encoded part of the url
decimal = urlparse(args.url).path[1:]
# decode to JWS string
data = ''.join(chr(int(''.join(decimal[i:i + 2]), 10) + 45)
for i in range(0, len(decimal), 2))
# decode base64 parts
parts = [base64.urlsafe_b64decode(s + '===') for s in data.split('.')]
card = {
'header': json.loads(parts[0]),
'payload': json.loads(unzlib(parts[1])),
'signature': parts[2].hex()
}
print(json.dumps(card, indent=2))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment