diff --git a/README.md b/README.md index 9dfeb37..730d3e0 100644 --- a/README.md +++ b/README.md @@ -12,17 +12,18 @@ Use it in your script: var encryptor = require('file-encryptor'); - var key = 'My Super Secret Key'; + var key = crypto.randomBytes(24); + var iv = crypto.randomBytes(16); // Encrypt file. - encryptor.encryptFile('input_file.txt', 'encrypted.dat', key, function(err) { + encryptor.encryptFile('input_file.txt', 'encrypted.dat', key, iv, function(err) { // Encryption complete. }); ... // Decrypt file. - encryptor.decryptFile('encrypted.dat', 'output_file.txt', key, function(err) { + encryptor.decryptFile('encrypted.dat', 'output_file.txt', key, iv, function(err) { // Decryption complete. }); @@ -40,7 +41,9 @@ Available algorithms can be found by executing: Setting algorithm option: - var key = 'My Super Secret Key'; + var key = crypto.randomBytes(32); + var iv = crypto.randomBytes(16); + var options = { algorithm: 'aes256' }; encryptor.encryptFile('input_file.txt', 'encrypted.dat', key, options, function(err) { diff --git a/lib/file-encryptor.js b/lib/file-encryptor.js index 33deefe..8407607 100644 --- a/lib/file-encryptor.js +++ b/lib/file-encryptor.js @@ -3,7 +3,7 @@ var crypto = require('crypto'), var Encryptor = {}; -Encryptor.encryptFile = function(inputPath, outputPath, key, options, callback) { +Encryptor.encryptFile = function(inputPath, outputPath, key, iv, options, callback) { if(typeof options === 'function') { callback = options; @@ -12,20 +12,21 @@ Encryptor.encryptFile = function(inputPath, outputPath, key, options, callback) options = Encryptor.combineOptions(options); - var keyBuf = new Buffer(key); + var keyBuf = Buffer.from(key); + var ivBuf = Buffer.from(iv); var inputStream = fs.createReadStream(inputPath); var outputStream = fs.createWriteStream(outputPath); - var cipher = crypto.createCipher(options.algorithm, keyBuf); + var cipher = crypto.createCipheriv(options.algorithm, keyBuf, ivBuf); inputStream.on('data', function(data) { - var buf = new Buffer(cipher.update(data), 'binary'); + var buf = Buffer.from(cipher.update(data), 'binary'); outputStream.write(buf); }); inputStream.on('end', function() { try { - var buf = new Buffer(cipher.final('binary'), 'binary'); + var buf = Buffer.from(cipher.final('binary'), 'binary'); outputStream.write(buf); outputStream.end(); outputStream.on('close', function() { @@ -38,7 +39,7 @@ Encryptor.encryptFile = function(inputPath, outputPath, key, options, callback) }); }; -Encryptor.decryptFile = function(inputPath, outputPath, key, options, callback) { +Encryptor.decryptFile = function(inputPath, outputPath, key, iv, options, callback) { if(typeof options === 'function') { callback = options; @@ -47,20 +48,21 @@ Encryptor.decryptFile = function(inputPath, outputPath, key, options, callback) options = Encryptor.combineOptions(options); - var keyBuf = new Buffer(key); + var keyBuf = Buffer.from(key); + var ivBuf = Buffer.from(iv); var inputStream = fs.createReadStream(inputPath); var outputStream = fs.createWriteStream(outputPath); - var cipher = crypto.createDecipher(options.algorithm, keyBuf); + var cipher = crypto.createDecipheriv(options.algorithm, keyBuf, ivBuf); inputStream.on('data', function(data) { - var buf = new Buffer(cipher.update(data), 'binary'); + var buf = Buffer.from(cipher.update(data), 'binary'); outputStream.write(buf); }); inputStream.on('end', function() { try { - var buf = new Buffer(cipher.final('binary'), 'binary'); + var buf = Buffer.from(cipher.final('binary'), 'binary'); outputStream.write(buf); outputStream.end(); outputStream.on('close', function() { diff --git a/test/basic.js b/test/basic.js index 5242139..5103b04 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,14 +1,18 @@ -var encryptor = require('../lib/file-encryptor'), +var crypto = require('crypto'), + encryptor = require('../lib/file-encryptor'), fs = require('fs'), path = require('path'); -var key = 'My Super Secret Key'; +var key = crypto.randomBytes(32); +var iv = crypto.randomBytes(16); var encrypt = function(input) { encryptor.encryptFile( path.join(__dirname, input), path.join(__dirname, input + '.data'), key, + iv, + { algorithm: 'aes256' }, function(err) { console.log(input + ' encryption complete.'); decrypt(input, input + '.data'); @@ -21,6 +25,8 @@ var decrypt = function(original, encrypted) { path.join(__dirname, encrypted), path.join(__dirname, 'decrypted.' + original), key, + iv, + { algorithm: 'aes256' }, function(err) { console.log(original + ' decryption complete.'); }