├── .gitignore ├── README.md ├── lib └── file-encryptor.js ├── package.json └── test ├── basic.js └── example.txt /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # file-encryptor 2 | 3 | Encrypts files using Node's built-in Cipher class. Encryption is stream-based for low memory footprint. 4 | 5 | ## Getting Started 6 | 7 | Install the module: 8 | 9 | npm install file-encryptor 10 | 11 | Use it in your script: 12 | 13 | var encryptor = require('file-encryptor'); 14 | 15 | var key = 'My Super Secret Key'; 16 | 17 | // Encrypt file. 18 | encryptor.encryptFile('input_file.txt', 'encrypted.dat', key, function(err) { 19 | // Encryption complete. 20 | }); 21 | 22 | ... 23 | 24 | // Decrypt file. 25 | encryptor.decryptFile('encrypted.dat', 'output_file.txt', key, function(err) { 26 | // Decryption complete. 27 | }); 28 | 29 | The input file will be streamed through the Cipher and to the output file. If the output files does not 30 | exists, it will be created. If the output file already exists, it will be truncated. 31 | 32 | ## Change Cipher Algorithm 33 | 34 | By default the "aes192" cipher algorithm is used. This can be changed by passing a new algorithm string 35 | as an option. 36 | 37 | Available algorithms can be found by executing: 38 | 39 | openssl list-cipher-algorithms 40 | 41 | Setting algorithm option: 42 | 43 | var key = 'My Super Secret Key'; 44 | var options = { algorithm: 'aes256' }; 45 | 46 | encryptor.encryptFile('input_file.txt', 'encrypted.dat', key, options, function(err) { 47 | // Decryption complete; 48 | }); 49 | 50 | ... 51 | 52 | encryptor.decryptFile('encrypted.dat', 'outputfile.txt', key, options, function(err) { 53 | // Encryption complete; 54 | }); 55 | 56 | ## License 57 | Copyright (c) 2013 Modulus 58 | Licensed under the MIT license. 59 | -------------------------------------------------------------------------------- /lib/file-encryptor.js: -------------------------------------------------------------------------------- 1 | var crypto = require('crypto'), 2 | fs = require('fs'); 3 | 4 | var Encryptor = {}; 5 | 6 | Encryptor.encryptFile = function(inputPath, outputPath, key, options, callback) { 7 | 8 | if(typeof options === 'function') { 9 | callback = options; 10 | options = {}; 11 | } 12 | 13 | options = Encryptor.combineOptions(options); 14 | 15 | var keyBuf = new Buffer(key); 16 | 17 | var inputStream = fs.createReadStream(inputPath); 18 | var outputStream = fs.createWriteStream(outputPath); 19 | var cipher = crypto.createCipher(options.algorithm, keyBuf); 20 | 21 | inputStream.on('data', function(data) { 22 | var buf = new Buffer(cipher.update(data), 'binary'); 23 | outputStream.write(buf); 24 | }); 25 | 26 | inputStream.on('end', function() { 27 | try { 28 | var buf = new Buffer(cipher.final('binary'), 'binary'); 29 | outputStream.write(buf); 30 | outputStream.end(); 31 | outputStream.on('close', function() { 32 | return callback(); 33 | }); 34 | } catch(e) { 35 | fs.unlink(outputPath); 36 | return callback(e); 37 | } 38 | }); 39 | }; 40 | 41 | Encryptor.decryptFile = function(inputPath, outputPath, key, options, callback) { 42 | 43 | if(typeof options === 'function') { 44 | callback = options; 45 | options = {}; 46 | } 47 | 48 | options = Encryptor.combineOptions(options); 49 | 50 | var keyBuf = new Buffer(key); 51 | 52 | var inputStream = fs.createReadStream(inputPath); 53 | var outputStream = fs.createWriteStream(outputPath); 54 | var cipher = crypto.createDecipher(options.algorithm, keyBuf); 55 | 56 | inputStream.on('data', function(data) { 57 | var buf = new Buffer(cipher.update(data), 'binary'); 58 | outputStream.write(buf); 59 | }); 60 | 61 | inputStream.on('end', function() { 62 | try { 63 | var buf = new Buffer(cipher.final('binary'), 'binary'); 64 | outputStream.write(buf); 65 | outputStream.end(); 66 | outputStream.on('close', function() { 67 | return callback(); 68 | }); 69 | } catch(e) { 70 | fs.unlink(outputPath); 71 | return callback(e); 72 | } 73 | }); 74 | }; 75 | 76 | Encryptor.combineOptions = function(options) { 77 | var result = {}; 78 | for(var key in Encryptor.defaultOptions) { 79 | result[key] = Encryptor.defaultOptions[key]; 80 | } 81 | 82 | for(var key in options) { 83 | result[key] = options[key]; 84 | } 85 | 86 | return result; 87 | }; 88 | 89 | Encryptor.defaultOptions = { 90 | algorithm: 'aes192' 91 | }; 92 | 93 | module.exports = Encryptor; 94 | 95 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "file-encryptor", 3 | "version": "0.1.1", 4 | "description": "Encrypts files using Node's built-in Cipher class.", 5 | "homepage": "https://github.com/onmodulus/file-encryptor", 6 | "author": "Modulus ", 7 | "contributors": { 8 | "name": "Brandon Cannaday", 9 | "email": "brandon@modulus.io" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/onmodulus/file-encryptor" 14 | }, 15 | "main": "./lib/file-encryptor.js", 16 | "dependencies": {} 17 | } 18 | -------------------------------------------------------------------------------- /test/basic.js: -------------------------------------------------------------------------------- 1 | var encryptor = require('../lib/file-encryptor'), 2 | fs = require('fs'), 3 | path = require('path'); 4 | 5 | var key = 'My Super Secret Key'; 6 | 7 | var encrypt = function(input) { 8 | encryptor.encryptFile( 9 | path.join(__dirname, input), 10 | path.join(__dirname, input + '.data'), 11 | key, 12 | function(err) { 13 | console.log(input + ' encryption complete.'); 14 | decrypt(input, input + '.data'); 15 | } 16 | ); 17 | }; 18 | 19 | var decrypt = function(original, encrypted) { 20 | encryptor.decryptFile( 21 | path.join(__dirname, encrypted), 22 | path.join(__dirname, 'decrypted.' + original), 23 | key, 24 | function(err) { 25 | console.log(original + ' decryption complete.'); 26 | } 27 | ); 28 | }; 29 | 30 | encrypt('example.txt'); 31 | -------------------------------------------------------------------------------- /test/example.txt: -------------------------------------------------------------------------------- 1 | This is an example file. 2 | 3 | This is some example content. --------------------------------------------------------------------------------