Skip to content

Instantly share code, notes, and snippets.

@lmillucci
Created June 29, 2021 12:08
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lmillucci/48804b0598553689fc5054da10e63231 to your computer and use it in GitHub Desktop.
Save lmillucci/48804b0598553689fc5054da10e63231 to your computer and use it in GitHub Desktop.
Simple green pass decoder in JavaScript
/**
* Simple green pass decoder inspired by https://git.gir.st/greenpass.git/blob_plain/master:/greenpass.py
*
* 2021 Lorenzo Millucci
*
* Before usage install following dependecies `npm install base45 cbor jpeg-js jsqr pako`
*/
const base45 = require('base45');
const cbor = require('cbor');
const fs = require('fs')
const jpeg = require('jpeg-js');
const jsQR = require("jsqr");
const pako = require('pako');
// Set the path to the green pass QR
const FILE_PATH = __dirname + '/greenpass.jpg';
// Read image file
const greenpassJpeg = fs.readFileSync(FILE_PATH);
const greenpassImageData = jpeg.decode(greenpassJpeg, {useTArray: true});
// Decode QR
const decodedGreenpass = jsQR(greenpassImageData.data, greenpassImageData.width, greenpassImageData.height);
// Remove `HC1:` from the string
const greenpassBody = decodedGreenpass.data.substr(4);
// Data is Base45 encoded
const decodedData = base45.decode(greenpassBody);
// And zipped
const output = pako.inflate(decodedData);
const results = cbor.decodeAllSync(output);
[headers1, headers2, cbor_data, signature] = results[0].value;
const greenpassData = cbor.decodeAllSync(cbor_data);
console.log(JSON.stringify(greenpassData[0].get(-260).get(1), null, 2));
@lmillucci
Copy link
Author

Hello! Do you know why I get errors on [headers1, headers2, cbor_data, signature] = results[0].value; -> 'headers1' is not defined, 'headers2 is not defined, 'cbor_data' is not defined, 'signature' is not defined ? Thank you!

I don't know. I assume that something went wrong during CBOR decoding

@jumpjack
Copy link

Add some debug lines as follows, to figure out where the process failed:

// Data is Base45 encoded
const decodedData = base45.decode(greenpassBody);
console.log("decodedData ", decodedData );

// And zipped
const output = pako.inflate(decodedData);
console.log("output ",output );

const results = cbor.decodeAllSync(output);
console.log("results ",results );

[headers1, headers2, cbor_data, signature] = results[0].value;

const greenpassData = cbor.decodeAllSync(cbor_data);

@jumpjack
Copy link

You can also have a look at my page for greenpass decoding in browser standalone:
https://github.com/jumpjack/greenpass

@aortukgp
Copy link

Hello! Do you know why I get errors on [headers1, headers2, cbor_data, signature] = results[0].value; -> 'headers1' is not defined, 'headers2 is not defined, 'cbor_data' is not defined, 'signature' is not defined ? Thank you!

Because you have to declare those variables first. Like this:

let headers1, headers2, cbor_data, signature;

And then write this:

[headers1, headers2, cbor_data, signature] = results[0].value;

@jumpjack
Copy link

Suggestion for decoding/reading (not checking) the "signature" variable?
It should result in something like:

kid: 53FOjX/4aJs=
key: <EllipticCurvePublicNumbers(curve=secp256r1, x=59224424711316661084877973301841821584140021680113528472675651838972371380627, y=54841068689176540860306147861276004028606373898471432794562118907413910993957>

@grildev
Copy link

grildev commented Nov 19, 2021

Suggestion for decoding/reading (not checking) the "signature" variable? It should result in something like:

kid: 53FOjX/4aJs=
key: <EllipticCurvePublicNumbers(curve=secp256r1, x=59224424711316661084877973301841821584140021680113528472675651838972371380627, y=54841068689176540860306147861276004028606373898471432794562118907413910993957>

I have the same question

@Mukinayi
Copy link

Hi Lorenzo!

@Mukinayi
Copy link

Lorenzo can you advise a library to encrypt and sign cbor message ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment