问题
I use forge framework for some kind of PGP realization. So I need to encrypt very larg file (2gb or more) and keep it encrypted. I want to use as small RAM memory as possible.
What is the best way to do it?
回答1:
This is a general design issue -- not so much related to forge. Like MDG said, you'll need to use streaming to avoid keeping the whole file and encrypted file in memory.
Forge's cipher objects (see: AES) will allow you to consume chunks of data from a stream. You can do cipher.update()
to encrypt an arbitrarily-sized chunk of data from a stream and then call cipher.output.getBytes()
to clear the internal buffer and get any encrypted output which you can then send somewhere. This should keep your memory usage low.
Keep in mind that the bytes you get from a forge buffer will be encoded in a string. The string encoding type is what node.js recognizes as 'binary'. (It's a way to encode bytes in a JS string for browsers that don't support TypedArrays, which many browsers did not at the time that forge was first written). If you're talking to a node.js server, you can just put this string into a Buffer
using the 'binary' encoding. If you're talking to another server, you may want to base64 encode them before transmission. You can do this using forge.util.encode64
.
You may also want to check out OpenPGP.js.
回答2:
Encrypting large files should be stream based for low memory usage, You can have a look at node's built in crypto module or try out different packages on npm (file-encryptor for example)
来源:https://stackoverflow.com/questions/22509062/forge-encrypt-large-file