October 22, 2024
Chicago 12, Melborne City, USA
javascript

Decrypt returns false when files exceed chunk size


I am trying to decrypt a file in chunks, but it fails. The decryption function, crypto_secretstream_xchacha20poly1305_pull, returns false.

After some tests, I realized that my code is able to decrypt files that are smaller than my chunk size of 64MB. Although it still throws an error, it can decrypt those files. On the other hand, for files larger than my chunk size, it is unable to decrypt them and writes ‘undefined’ to the file.

function decryptChunk(chunk, state, buffer) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = (event) => {
      const chunkArr = new Uint8Array(chunk);
      const decryptedChunk = sodium.crypto_secretstream_xchacha20poly1305_pull(state, chunkArr);
      buffer.push(decryptedChunk.message);
      resolve(buffer);
    };
    reader.onerror = console.log(reader);
    reader.readAsArrayBuffer(chunk);
  });
}

State and chunkArr variables seem to be well-formed but looks like not. After reading the doc, I didn’t find the possible issue. What am I doing wrong?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video