0

I recently have taken up interest in qr codes and first learned of base45 encoding and CBOR. I have a code snippet below, what I am wondering is if there is a way to actually reverse so that you could write a json object or change the name and convert it back to how it originally looks.

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')

// decode the qr code

const greenpassJpeg = fs.readFileSync(__dirname + '/real-qr.jpg')
const greenpassImageData = jpeg.decode(greenpassJpeg, { useTArray: true })

const decodedGreenpass = jsQR(greenpassImageData.data, greenpassImageData.width, greenpassImageData.height)
const greenpassBody = decodedGreenpass.data.substr(4)

const decodedData = base45.decode(greenpassBody)
const output = pako.inflate(decodedData)

const results = cbor.decodeAllSync(output);
[headers1, headers2, cbor_data, signature] = results[0].value

const greenpassData = cbor.decodeAllSync(cbor_data)
const greenpassJson = JSON.stringify(greenpassData[0].get(-260).get (1), null, 2)
dbzx10299
  • 722
  • 2
  • 14
  • 1
    According to the documentation `encode` returns a string and `decode` returns a buffer. I've no idea why you think you should get an array for one or why you think you have one for the other. – Quentin Jan 18 '22 at 13:42
  • I'm simply confused how to reverse this code block. I've gotten close but I can't get it to be identical as the starting point – dbzx10299 Jan 18 '22 at 13:45
  • 1
    You cannot construct the correct `signature` for your `cbor_data` unless you know the private key, of course. Your code seems not to verify the `signature`, see https://stackoverflow.com/questions/68247625/how-can-i-display-a-cbor-web-token-encoded-token-in-a-legible-manner/68420433#68420433 – Heiko Theißen Jan 18 '22 at 16:46

0 Answers0