diff options
| author | netaneld122 <netaneld122@gmail.com> | 2021-03-01 09:46:52 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-01 09:46:52 +0200 |
| commit | eb5d270ffc7d28b6bffe4789a8ea421b27be0f25 (patch) | |
| tree | 9db9ff353b66b02616572fb317dd69cd780412b6 /verify.py | |
| parent | 934798e6b82c711f0e877fa7a05235aa2cf2845a (diff) | |
Added actual QR code reading (#3)
* Added actual QR code reading.
Added requirements.txt for dependencies.
Added .gitignore.
Updated README according to new usage.
* Fixed CR notes.
Migrated requirements to a Pipfile format.
Added ability to open textual payload.
Removed irrelevant paths in .gitignore.
* Fixed pipenv run cli.
Co-authored-by: Netanel <Netanel>
Co-authored-by: nati <nati>
Diffstat (limited to 'verify.py')
| -rw-r--r-- | verify.py | 91 |
1 files changed, 65 insertions, 26 deletions
@@ -1,37 +1,76 @@ +import argparse import base64 import json +from pathlib import Path + from cryptography.hazmat.primitives import hashes, serialization from cryptography.hazmat.primitives.asymmetric import padding -sig = base64.b64decode("base64EncodedSignature==") +from PIL import Image +from pyzbar import pyzbar + + +def cert(name): + return Path(__file__).absolute().parent / 'certs' / name + + +def verify(qr_code_bytes): + b64, payload = qr_code_bytes.split(b'#', maxsplit=1) + sig = base64.decodebytes(b64) + + h = hashes.Hash(hashes.SHA256()) + h.update(payload.decode().encode('utf8')) + digest = h.finalize() + + with open(cert("RamzorQRPubKey.pem"), "rb") as f: + k = serialization.load_pem_public_key(f.read()) + k.verify( + sig, + digest, + padding.PKCS1v15(), + hashes.SHA256(), + ) + + print("Valid signature!") + + data = json.loads(payload) + if data['ct'] == 1: + for i in range(len(data['p'])): + print(f"Details of person number {i+1}:") + print(f"\tIsraeli ID Number {data['p'][i]['idl']}") + print(f"\tID valid by {data['p'][i]['e']}") + print(f"Cert Unique ID {data['id']}") + elif data['ct'] == 2: + print(f"Israeli ID Number {data['idl']}") + print(f"ID valid by {data['e']}") + print(f"Cert Unique ID {data['id']}") + else: + print("Unsupported certificate type") + + +def read_qr_code(image_path): + return pyzbar.decode(Image.open(image_path))[0].data -payload = '{"id":"01/IL/ABCD1234ABCD1234ABCD1234ABCD1234#ABCD1234","et":1,"ct":1,"c":"IL MOH","cn":null,"fn":null,"g":null,"f":null,"gl":null,"fl":null,"idp":null,"idl":null,"b":"0001-01-01","e":"0001-01-01","a":"0001-01-01","p":[{"idl":"0123456789","e":"2021-01-01"}]}' -h = hashes.Hash(hashes.SHA256()) -h.update(payload.encode("utf-8")) -digest = h.finalize() +def create_arg_parser(): + parser = argparse.ArgumentParser("GreenPass QR code verifier") + group = parser.add_mutually_exclusive_group(required=True) + group.add_argument( + "-i", "--image_path", type=Path, help="Path to an image with the QR code", default=None) + group.add_argument( + "-t", "--txt_path", type=Path, help="Path to decoded QR code textual content", default=None) + return parser -with open("certs/RamzorQRPubKey.pem", "rb") as f: - k = serialization.load_pem_public_key(f.read()) - k.verify( - sig, - digest, - padding.PKCS1v15(), - hashes.SHA256(), - ) +if __name__ == '__main__': + # Parse arguments + parser = create_arg_parser() + args = parser.parse_args() -data = json.loads(payload) -if data['ct'] == 1: - for i in range(len(data['p'])): - print(f"Details of person number {i+1}:") - print(f"\tIsraeli ID Number {data['p'][i]['idl']}") - print(f"\tID valid by {data['p'][i]['e']}") - print(f"Cert Unique ID {data['id']}") -elif data['ct'] == 2: - print(f"Israeli ID Number {data['idl']}") - print(f"ID valid by {data['e']}") - print(f"Cert Unique ID {data['id']}") -else: - print("Unsupported certificate type") + # Choose correct input + if args.image_path: + verify(read_qr_code(args.image_path)) + elif args.txt_path: + with open(args.txt_path, 'rb') as f: + verify(f.read().strip()) |
