如何从解码缓冲区编码 CBOR
Posted
技术标签:
【中文标题】如何从解码缓冲区编码 CBOR【英文标题】:How to encode CBOR from the decoding buffer 【发布时间】:2021-11-12 22:27:33 【问题描述】:在解码、编码和解码 CBOR 时,缓冲区返回的结果不同。
const greenpassData = cbor.decodeAllSync(cbor_data);
// --------------------------------
//Want the CBOR data after decoding CBOR Data
const greenpassDataEncode = cbor.encode(greenpassData);
console.log(greenpassDataEncode);
console.log(cbor_data);
完整代码 如何解码 GreenPass 二维码。 受https://git.gir.st/greenpass.git/blob_plain/master:/greenpass.py启发的简单绿色通行证解码器 完整代码 如何解码 GreenPass 二维码。 受https://git.gir.st/greenpass.git/blob_plain/master:/greenpass.py启发的简单绿色通道解码器 完整代码 如何解码 GreenPass 二维码。 受https://git.gir.st/greenpass.git/blob_plain/master:/greenpass.py启发的简单绿色通道解码器 完整代码 如何解码 GreenPass 二维码。 受https://git.gir.st/greenpass.git/blob_plain/master:/greenpass.py启发的简单绿色通道解码器
/**
* 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.jpeg';
// 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);
console.log(greenpassBody)
// 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);
// --------------------------------
//Want the cbor data after decoding cbor Data
const greenpassDataEncode = cbor.encode(greenpassData);
console.log(greenpassDataEncode);
console.log(cbor_data);
// --------------------------------
var josnString = JSON.stringify(greenpassData[0].get(-260).get(1), null, 2);
//new Encoded Array
const ecodeArrayGreenPassData = cbor.encode(greenpassData);
const greenpassData2 = cbor.decodeAllSync(ecodeArrayGreenPassData);
【问题讨论】:
我也想知道这个。缓冲区中有一些奇怪的第一项,切片时,缓冲区相同 console.log('c', cbor_data); console.log('g', greenpassDataEncode.slice(1)); 【参考方案1】:也许是因为……
错误..
让您的控制台记录 Pako 创建的内容,这应该类似于:
Uint8Array(350) [
210, 132, 67,
....
35, 68, 98, 99,
... 250 more items
]
..而普通的 cbor.encode() 从 decodeAllSync 结果构建,如下所示:
<Buffer 11 21 31 41 51 61 ... 302 more bytes>
..你错过了这一点:
const build = new Uint8Array(cbor.encode(my-decode-result))
玩得开心!
(..但老实说:也花了我一点时间..不过,剩下的路应该更容易。)
【讨论】:
哦.. 刚刚认识到:我不使用“cbor.decodeAllSync()”(返回一个数组),而是使用“cbor.decode()”(..returns 对象)。 ..就像一个魅力。以上是关于如何从解码缓冲区编码 CBOR的主要内容,如果未能解决你的问题,请参考以下文章
Google Protocol Buffers - 对编码解码 base64 char * c 字符串协议缓冲区数据感到困惑