Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
});

Expand All @@ -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) {
Expand Down
22 changes: 12 additions & 10 deletions lib/file-encryptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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;
Expand All @@ -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() {
Expand Down
10 changes: 8 additions & 2 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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.');
}
Expand Down