fix(frontend/player): fix decyphring return type

This commit is contained in:
Alex Kaminskii 2022-10-17 15:27:47 +02:00
parent 107b34e8b2
commit f1f7168974
3 changed files with 8 additions and 16 deletions

View file

@ -216,6 +216,7 @@ export default class MessageDistributor extends StatedScreen {
}
private loadMessages() {
// TODO: reuseable decryptor instance
const createNewParser = (shouldDecrypt=true) => {
const decrypt = shouldDecrypt && this.session.fileKey
? (b: Uint8Array) => decryptSessionBytes(b, this.session.fileKey)
@ -237,7 +238,7 @@ export default class MessageDistributor extends StatedScreen {
.then(createNewParser(false))
// Fallback to back Compatability with mobsUrl
.catch(e =>
loadFiles(this.session.mobsUrl, createNewParser())
loadFiles(this.session.mobsUrl, createNewParser(false))
)
)
.then(this.onFileReadSuccess)

View file

@ -1,31 +1,24 @@
const u8aFromHex = (hexString:string) =>
Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)))
const is16BitHex = (maybeHex: string | undefined) =>
maybeHex && maybeHex.length % 2 === 0 && !/[^a-fA-F0-9]/u.test(maybeHex)
function truncPadding(padded: Uint8Array): Uint8Array {
let i = padded.length - 1
for (; !padded[i] ;i--) {}
return padded.subarray(0, i)
}
export function decryptSessionBytes(cypher: Uint8Array, keyString: string): Promise<Uint8Array> {
if (keyString.length !== 64) {
return Promise.reject("Wrong key string format")
}
const [hexKey, hexIV] = keyString.match(/.{8}/g)
const [hexKey, hexIV] = keyString.match(/.{32}/g)
if (!is16BitHex(hexIV) || !is16BitHex(hexKey)) {
return Promise.reject("Wrong key/iv pair")
}
const iv = u8aFromHex(hexIV)
const byteKey = u8aFromHex(hexKey)
return crypto.subtle.importKey("raw",byteKey, { name: "AES-CBC" }, false, ["decrypt"])
const iv = u8aFromHex(hexIV)
return crypto.subtle.importKey("raw", byteKey, { name: "AES-CBC" }, false, ["decrypt"])
.then(key => crypto.subtle.decrypt({ name: "AES-CBC", iv: iv}, key, cypher))
.then(truncPadding)
.then((bArray: ArrayBuffer) => new Uint8Array(bArray)) //?? TS doesn not catch the `decrypt`` returning type
}

View file

@ -3,11 +3,9 @@ import APIClient from 'App/api_client';
const NO_NTH_FILE = "nnf"
const NO_UNPROCESSED_FILES = "nuf"
type onDataCb = (data: Uint8Array) => void
export const loadFiles = (
urls: string[],
onData: onDataCb,
onData: (data: Uint8Array) => void,
): Promise<void> => {
const firstFileURL = urls[0]
urls = urls.slice(1)