Skip to content

Instantly share code, notes, and snippets.

@joedf
Forked from remi/shc.js
Last active November 30, 2021 04:27
Show Gist options
  • Save joedf/40a9edd59a8ff961afe5f3803eccfa3d to your computer and use it in GitHub Desktop.
Save joedf/40a9edd59a8ff961afe5f3803eccfa3d to your computer and use it in GitHub Desktop.
Extract JSON object from https://smarthealth.cards QR code
// modified by joedf from https://gist.github.com/remi/e3aa2f78845ee13f706ed83aead5145f
// Extract JSON payload from SHC QR code (without any kind of private/public key verification)
// Credits + inspiration
// https://github.com/dvci/health-cards-walkthrough/blob/main/SMART%20Health%20Cards.ipynb
// Usage
// $ node shc2.js "shc:/01234569…"
const zlib = require("zlib");
// Extract the QR data from arguments
const data = process.argv[2];
// Convert the data to a JWT and extract its base64-encode payload
const jwt = data
.split("/")[1]
.match(/(..?)/g)
.map((number) => String.fromCharCode(parseInt(number, 10) + 45))
.join("")
.split(".");
const header = jwt[0];
const payload = jwt[1];
const secret = jwt[2];
console.log('\nHeader: ', decode_JWT(header,1));
console.log('\nPayload: ', decode_JWT(payload));
console.log('\nSecret: ', decode_JWT(secret,2));
function decode_JWT(data, mode=0) {
// Decode the data
const buffer = Buffer.from(data, "base64");
var ret;
switch(mode)
{
case 0: // Uncompress the data and return the result
ret = prettyJson(zlib.inflateRawSync(buffer).toString("utf8")); break;
case 1: ret = prettyJson(buffer.toString("utf8")); break;
default: ret = data;
}
return ret;
}
function prettyJson(s) { return JSON.stringify(JSON.parse(s), null, 4); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment