Port decode to typescript

This commit is contained in:
Marvin Sextro 2021-07-01 19:49:43 +02:00
parent 5054e4da54
commit 254a748895
4 changed files with 80 additions and 68 deletions

34
package-lock.json generated
View File

@ -9,7 +9,7 @@
"dependencies": {
"@zxing/browser": "^0.0.9",
"@zxing/library": "^0.18.6",
"base45-js": "^1.0.1",
"base45": "^3.0.0",
"cbor-js": "^0.1.0",
"do-not-zip": "^1.0.0",
"file-saver": "^2.0.5",
@ -27,6 +27,7 @@
"worker-loader": "^3.0.7"
},
"devDependencies": {
"@types/pako": "^1.0.1",
"@types/react": "^17.0.11",
"autoprefixer": "^10.0.4",
"postcss": "^8.1.10",
@ -293,6 +294,12 @@
"resolved": "https://registry.npmjs.org/@types/node/-/node-15.12.4.tgz",
"integrity": "sha512-zrNj1+yqYF4WskCMOHwN+w9iuD12+dGm0rQ35HLl9/Ouuq52cEtd0CH9qMgrdNmi5ejC1/V7vKEXYubB+65DkA=="
},
"node_modules/@types/pako": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@types/pako/-/pako-1.0.1.tgz",
"integrity": "sha512-GdZbRSJ3Cv5fiwT6I0SQ3ckeN2PWNqxd26W9Z2fCK1tGrrasGy4puvNFtnddqH9UJFMQYXxEuuB7B8UK+LLwSg==",
"dev": true
},
"node_modules/@types/parse-json": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.0.tgz",
@ -675,13 +682,10 @@
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==",
"dev": true
},
"node_modules/base45-js": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/base45-js/-/base45-js-1.0.1.tgz",
"integrity": "sha512-AQW3SAO/VVaof+gE/QoQLstyzFRIuQZOwkAX9AY/Ur+qBkNqUfsv8h77vBqsiAgCrTBNBWU+c5m5B0QgKmAT+Q==",
"engines": {
"node": ">= 0.6.0"
}
"node_modules/base45": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/base45/-/base45-3.0.0.tgz",
"integrity": "sha512-SWayzDiUFaga+G3X/oL2XIDbjhdpmBGUqLU6T7zB89aNWYnEcfa36DIWW95rxn0sWAWbwrKcSyqcWw6AApYwsg=="
},
"node_modules/base64-js": {
"version": "1.5.1",
@ -4677,6 +4681,12 @@
"resolved": "https://registry.npmjs.org/@types/node/-/node-15.12.4.tgz",
"integrity": "sha512-zrNj1+yqYF4WskCMOHwN+w9iuD12+dGm0rQ35HLl9/Ouuq52cEtd0CH9qMgrdNmi5ejC1/V7vKEXYubB+65DkA=="
},
"@types/pako": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@types/pako/-/pako-1.0.1.tgz",
"integrity": "sha512-GdZbRSJ3Cv5fiwT6I0SQ3ckeN2PWNqxd26W9Z2fCK1tGrrasGy4puvNFtnddqH9UJFMQYXxEuuB7B8UK+LLwSg==",
"dev": true
},
"@types/parse-json": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.0.tgz",
@ -5007,10 +5017,10 @@
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==",
"dev": true
},
"base45-js": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/base45-js/-/base45-js-1.0.1.tgz",
"integrity": "sha512-AQW3SAO/VVaof+gE/QoQLstyzFRIuQZOwkAX9AY/Ur+qBkNqUfsv8h77vBqsiAgCrTBNBWU+c5m5B0QgKmAT+Q=="
"base45": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/base45/-/base45-3.0.0.tgz",
"integrity": "sha512-SWayzDiUFaga+G3X/oL2XIDbjhdpmBGUqLU6T7zB89aNWYnEcfa36DIWW95rxn0sWAWbwrKcSyqcWw6AApYwsg=="
},
"base64-js": {
"version": "1.5.1",

View File

@ -11,7 +11,7 @@
"dependencies": {
"@zxing/browser": "^0.0.9",
"@zxing/library": "^0.18.6",
"base45-js": "^1.0.1",
"base45": "^3.0.0",
"cbor-js": "^0.1.0",
"do-not-zip": "^1.0.0",
"file-saver": "^2.0.5",
@ -29,6 +29,7 @@
"worker-loader": "^3.0.7"
},
"devDependencies": {
"@types/pako": "^1.0.1",
"@types/react": "^17.0.11",
"autoprefixer": "^10.0.4",
"postcss": "^8.1.10",

View File

@ -1,55 +0,0 @@
// Taken from https://github.com/ehn-dcc-development/ehn-sign-verify-javascript-trivial/blob/main/cose_verify.js
// and https://github.com/ehn-dcc-development/dgc-check-mobile-app/blob/2c2ebf4e9b7650ceef44f7e1fb05a57572830c5b/src/app/cose-js/sign.js
const base45 = require('base45-js')
const zlib = require('pako')
const cbor = require('cbor-js')
export function typedArrayToBufferSliced(array) {
return array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset)
}
export function typedArrayToBuffer(array) {
var buffer = new ArrayBuffer(array.length)
array.map(function (value, i) {
buffer[i] = value
})
return array.buffer
}
export function decodeData(data) {
data = data.toString('ASCII')
if (data.startsWith('HC1')) {
data = data.substring(3)
if (data.startsWith(':')) {
data = data.substring(1)
} else {
console.log("Warning: unsafe HC1: header - update to v0.0.4")
}
} else {
console.log("Warning: no HC1: header - update to v0.0.4")
}
data = base45.decode(data)
if (data[0] == 0x78) {
data = zlib.inflate(data)
}
data = cbor.decode(typedArrayToBuffer(data))
if (!Array.isArray(data)) {
throw new Error('Expecting Array')
}
if (data.length !== 4) {
throw new Error('Expecting Array of length 4')
}
let plaintext = data[2]
let decoded = cbor.decode(typedArrayToBufferSliced(plaintext))
return decoded
}

56
src/decode.ts Normal file
View File

@ -0,0 +1,56 @@
// Taken from https://github.com/ehn-dcc-development/ehn-sign-verify-javascript-trivial/blob/main/cose_verify.js
// and https://github.com/ehn-dcc-development/dgc-check-mobile-app/blob/main/src/app/cose-js/sign.js
import base45 from 'base45';
import pako from 'pako';
import cbor from 'cbor-js';
export function typedArrayToBufferSliced(array: Uint8Array): ArrayBuffer {
return array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset);
}
export function typedArrayToBuffer(array: Uint8Array): ArrayBuffer {
var buffer = new ArrayBuffer(array.length);
array.map(function (value, i) {
return buffer[i] = value;
})
return array.buffer;
}
export function decodeData(data: string): Object {
if (data.startsWith('HC1')) {
data = data.substring(3);
if (data.startsWith(':')) {
data = data.substring(1);
} else {
console.log("Warning: unsafe HC1: header");
}
} else {
console.log("Warning: no HC1: header");
}
var arrayBuffer: Uint8Array = base45.decode(data);
if (arrayBuffer[0] == 0x78) {
arrayBuffer = pako.inflate(arrayBuffer);
}
var payloadArray: Array<Uint8Array> = cbor.decode(typedArrayToBuffer(arrayBuffer));
if (!Array.isArray(payloadArray)) {
throw new Error('Expecting Array');
}
if (payloadArray.length !== 4) {
throw new Error('Expecting Array of length 4');
}
var plaintext: Uint8Array = payloadArray[2];
var decoded: Object = cbor.decode(typedArrayToBufferSliced(plaintext));
return decoded;
}