Skip to content

Instantly share code, notes, and snippets.

@yoh1496
Created February 2, 2022 09:03
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 yoh1496/f3e6d4f320568add130b2d5d73125615 to your computer and use it in GitHub Desktop.
Save yoh1496/f3e6d4f320568add130b2d5d73125615 to your computer and use it in GitHub Desktop.
Parse and verify SMART Health Cards.
import zlib, base64
# Paste your SHC code here
strSHC = "shc:/567629095243206034602924374044603122295953265460346029254077280433602870286471674522280928613331456437653141590640220306450459085643550341424541364037063665417137241236380304375622046737407532323925433443326057360106452931531270742428395038692212766728666731266342087422573776302062041022437658685343255820002167287607585708105505622752282407670809680507692361773323356634342439664440596761410443377667202663224433674530596175400038397052612140292974753658337372662132066669047253044469405210524536242721550377673434280323045475690310233670562227414567090555653507636250537239522776211205312561442568282012726838630039087127042463716936535535602928393065580072763158437500341209546904210458383257586630101033123422114008776058732325243477645920113037325929083272452732223707055550412927584543582550667760036577724025621136525340592771740903663844771261692077697211447057562509437029626707254539002011763240720310114260256672645965627243654061066553770056003044082967606162724306592273682223412466107335331229606157521057357572327529693965670332063208596309543400076452696835713027450728663529345234666377297208583525543653527774072234735706452828641140633528387577054371703966706421520708254156041170353656054471407636552612616834377244090406554327122559623453686207006139712936404138601156656945315611255669116044703333731263580306106975715411702932060511012768634011703371553353213365032550756476005853005224547339310064671161682376335069647622323339523133724171327531702738363650063527592633763908656123314363227707566731311074"
# decode
arrChar = []
it = iter(strSHC.replace("shc:/", ""))
for i, j in zip(it, it):
arrChar.append(chr(int(i) * 10 + int(j) + 45))
# Header
strHeader = ''.join(arrChar).split('.')[0]
bytesHeader = base64.urlsafe_b64decode(strHeader + "====")
# Payload
strPayload = ''.join(arrChar).split('.')[1]
bytesPayload = base64.urlsafe_b64decode(strPayload + "====")
# Signature
strSignature = ''.join(arrChar).split('.')[2]
bytesSignature = base64.urlsafe_b64decode(strSignature + "====")
print("Header: ")
print(str(bytesHeader, "utf-8"))
print("Payload: ")
print(str(zlib.decompress(bytesPayload, -15), "utf-8"))
from Crypto.PublicKey import ECC
endian = "big"
# Key parameters from https://smarthealth.cards/examples/issuer/.well-known/jwks.json
x_int = int.from_bytes(base64.urlsafe_b64decode("11XvRWy1I2S0EyJlyf_bWfw_TQ5CJJNLw78bHXNxcgw" + "===="), endian)
y_int = int.from_bytes(base64.urlsafe_b64decode("eZXwxvO1hvCY0KucrPfKo7yAyMT6Ajc3N7OkAB6VYy8" + "===="), endian)
curve = "P-256"
key = ECC.construct(curve=curve, point_x=x_int, point_y=y_int )
# Calc Hash
from Crypto.Hash import SHA256
hashobj = SHA256.new((strHeader+"."+strPayload).encode("ascii"))
# Signature
strSignature = ''.join(arrChar).split('.')[2]
bytesSignature = base64.urlsafe_b64decode(strSignature + "====")
# Verify Signature
from Crypto.Signature import DSS
sigobj = DSS.new(key, "fips-186-3")
verification_status = True
try:
sigobj.verify(hashobj, bytesSignature)
except ValueError:
verification_status = False
print("Verification Status:")
print(verification_status)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment