I am trying to decryt cloud storage files. Those files have a header that I want to skip.
For example, this file is composed of [cloud storage header + salt + content + cloud storage footer]. Lets say salt is ‘SALT’.
------WebKitFormBoundaryMpzAvNgQ8HhW5A8i
Content-Disposition: form-data; name="file"; filename="blob"
Content-Type: application/octet-stream
SALTáB†nJÎÆBÂÀ?ó“´ZM¥SJz·xM×±È(ºKéGw#²ÇœQëKBt'ø%ÄkæsUA-h]aWA¯(•Ÿ€Ø&©€
------WebKitFormBoundaryMpzAvNgQ8HhW5A8i--
My decrypt function works fine for ‘normal’ files but returns undefined for cloud storage ones. I have tried to skip/delete header but I am missing something. The footer problem I haven’t figured out how to solve it yet.
... some code...
const file = input.file;
const GCP_HEADER = await readGGCPHeader(file);
const header = await readSignatureAndHeader(file, SIGNATURE, HEADER_SIZE, GCP_HEADER);
const decRx = decodeKeys(publicKey.value, privateKey.value);
const state = initializeDecryption(header, decRx);
let buffer = [];
let index = SIGNATURE.length + HEADER_SIZE;
while (index < file.size) {
const offset = index + CHUNK_SIZE + ABYTES_SIZE;
const chunk = file.slice(index, offset);
index = offset;
// Decrypt the chunk and update the decrypted buffer
buffer = await decryptChunk(chunk, state, buffer);
// Define a new blob and set output element to it
const blob = new Blob(buffer, { type: "application/octet-stream" });
input.file = blob;
}
async function readSignatureAndHeader(file, signature, HEADER_SIZE, GCP_HEADER) {
const fileReader = new FileReader();
fileReader.readAsArrayBuffer(file.slice(0, signature.length + HEADER_SIZE + GCP_HEADER.length));
const signatureAndHeader = await new Promise((resolve) => {
fileReader.onload = () => resolve(new Uint8Array(fileReader.result));
});
return signatureAndHeader.slice(signature.length + GCP_HEADER.length);
}
async function readGGCPHeader(file) {
const fileReader = new FileReader();
fileReader.readAsArrayBuffer(file);
const header = await new Promise((resolve) => {
fileReader.onload = () => {
const text = new TextDecoder().decode(fileReader.result);
const gcpHeaderEnd = text.indexOf("SALT");
resolve(new Uint8Array(fileReader.result.slice(0, gcpHeaderEnd)));
};
});
return header;
}
I think the problem is related to how I read the headers and the salt in the readSignatureAndHeader function.