Node.js encrypts large file using AES

后端 未结 3 1622
花落未央
花落未央 2020-12-08 12:34

I try to use following code to encrypt a file of 1 GB. But Node.js abort with \"FATAL ERROR: JS Allocation failed - process out of memory\". How can I deal with it?

相关标签:
3条回答
  • 2020-12-08 12:56

    crypto.createCipher() without initialization vector is deprecated since NodeJS v10.0.0 use crypto.createCipheriv() instead.

    You can also pipe streams using stream.pipeline() instead of pipe method and then promisify it (so the code will easily fit into promise-like and async/await flow).

    const {createReadStream, createWriteStream} = require('fs');
    const {pipeline} = require('stream');
    const {randomBytes, createCipheriv} = require('crypto');
    const {promisify} = require('util');
    
    const key = randomBytes(32); // ... replace with your key
    const iv = randomBytes(16); // ... replace with your initialization vector
    
    promisify(pipeline)(
            createReadStream('./text.txt'),
            createCipheriv('aes-256-cbc', key, iv),
            createWriteStream('./text.txt.enc')
    )
    .then(() => {/* ... */})
    .catch(err => {/* ... */});
    
    0 讨论(0)
  • 2020-12-08 12:58

    I would simply use the Fileger to do it. It's a promise-based package and a clean alternative to the NodeJS filesystem.

    const fileger = require("fileger")
    const file = new fileger.File("./your-file-path.txt");
    
    file.encrypt("your-password") // this will encrypt the file
       .then(() => {
          file.decrypt("your-password") // this will decrypt the file
       })
    
    0 讨论(0)
  • 2020-12-08 13:21

    You could write the encrypted file back to disk instead of buffering the entire thing in memory:

    var fs = require('fs');
    var crypto = require('crypto');
    
    var key = '14189dc35ae35e75ff31d7502e245cd9bc7803838fbfd5c773cdcd79b8a28bbd';
    var cipher = crypto.createCipher('aes-256-cbc', key);
    var input = fs.createReadStream('test.txt');
    var output = fs.createWriteStream('test.txt.enc');
    
    input.pipe(cipher).pipe(output);
    
    output.on('finish', function() {
      console.log('Encrypted file written to disk!');
    });
    
    0 讨论(0)
提交回复
热议问题