transcend-io / penumbra

Encrypt/decrypt anything in the browser using streams on background threads.
https://penumbra-example.vercel.app/
Apache License 2.0
146 stars 20 forks source link

Encryption of a native File using streams #255

Open hennedo opened 1 year ago

hennedo commented 1 year ago

Hello,

I saw there is already an issue #230 regarding support for native files, also a PR https://github.com/transcend-io/penumbra/pull/232 which helps using these files. I'm not sure if it would have been better to add to one of those as this asks for a different approach.

The native File API also exposes a ReadableStream which seems to be, at least in my testing with firefox, directly read from storage and is not loaded into RAM. My understanding is that the proposed PR https://github.com/transcend-io/penumbra/pull/232 creates an ArrayBuffer containing the whole file, which will propably result in poor performance when encrypting huge files. In the code of penumbra I found the encryptStream function, which is currently not exposed, would it be possible to expose a wrapper of this and then attach the readable stream of the native file to the encryptStream function?

The exposed encryptStream function could have the same parameters as encrypt but accepts a ReadableStream as second parameter. It could return a ReadableStream with the encrypted data which will in theory at least halve the RAM workload as one does not need to store the whole file in an ArrayBuffer to pass it to the encrypt function and then also store it somewhere (or stream it somewhere) a second time..

It's the first time I'm using Streams in Javascript and I hope I did not misunderstand anything..