forge icon indicating copy to clipboard operation
forge copied to clipboard

Page dies when encrypting large files

Open chiahsoon opened this issue 4 years ago • 1 comments

Hi! I'm trying to encrypt large file sizes (>= 1 GB) using AES-GCM, but when i try to upload a 1GB file, the page dies before finishing the encryption.

Pretty sure i'm missing some relevant javascript concepts here but any direction/help will be very much appreciated! My code currently looks something like this:

  // arrBuf represents file.arrayBuffer();
  const cipher = forge.cipher.createCipher('AES-GCM', key);
  cipher.start({
      iv: iv,
      additionalData: '',
      tagLength: 128,
  });
  const chunkSize = 1024 * 64;
  for (let idx = 0; idx < arrBuf.byteLength; idx += chunkSize) {
      const slice = arrBuf.slice(idx, idx + chunkSize);
      cipher.update(forge.util.createBuffer(slice)); 
  }
  cipher.finish();

chiahsoon avatar Mar 12 '22 16:03 chiahsoon

Hi @chiahsoon, I see 2 issues:

  • your whole file is placed in an array buffer living in the heap, you may want to use a readable stream to keep the heap low while processing the file encryption.
  • when you call cipher.update(chunkBuffer) you add the chunk to the cipher buffer making it grow with each new chunk, you can use cipher.output.getBytes() to flush the cipher buffer, you will get the current encryption state (write it or pass it to a stream).

PierreJeanjacquot avatar Jul 12 '23 14:07 PierreJeanjacquot