Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-cipher

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-cipher - npm Package Compare versions

Comparing version 5.0.0 to 5.0.1

240

lib/node-cipher.js

@@ -16,17 +16,36 @@ /**

let debug = require('debug');
let fse = require('fs-extra');
let fs = require('fs-extra');
let validate = require('validate');
/**
* Configure debuggers.
* @const {Array} ALL_CIPHERS
*/
let debugEncrypt = require('debug')('nodecipher:encrypt');
let debugDecrypt = require('debug')('nodecipher:decrypt');
const ALL_CIPHERS = crypto.getCiphers();
/**
* Get all valid ciphers.
* @const {string} DEFAULT_SALT
*/
let ciphers = crypto.getCiphers();
const DEFAULT_SALT = 'nodecipher';
/**
* @const {number} DEFAULT_ITERATIONS
*/
const DEFAULT_ITERATIONS = 1000;
/**
* @const {number} DEFAULT_KEYLEN
*/
const DEFAULT_KEYLEN = 512;
/**
* @const {string} DEFAULT_DIGEST
*/
const DEFAULT_DIGEST = 'sha1';
/**
* @const {string} DEFAULT_ALGORITHM
*/
const DEFAULT_ALGORITHM = 'cast5-cbc';
/**
* @class NodeCipher

@@ -37,12 +56,5 @@ */

/**
* NodeCipher instance constructor.
*
* @constructor
*/
constructor() {}
/**
* Encrypt a file using the options provided.
*
* @see _encryptOrDecrypt
* @see _parseCipherRequest
* @param {Object} options

@@ -54,5 +66,3 @@ * @param {Function} [callback]

_encrypt(options, callback, scope) {
debugEncrypt('encrypt with options ' + JSON.stringify(options));
this._encryptOrDecrypt(options, crypto.createCipher, err => {
this._parseCipherRequest(NodeCipher.Actions.ENCRYPT, options, err => {
if (_.isFunction(callback)) {

@@ -67,3 +77,3 @@ callback.call(scope, err);

*
* @see _encryptOrDecrypt
* @see _parseCipherRequest
* @param {Object} options

@@ -75,5 +85,3 @@ * @param {Function} [callback]

_decrypt(options, callback, scope) {
debugDecrypt('decrypt with options ' + JSON.stringify(options));
this._encryptOrDecrypt(options, crypto.createDecipher, err => {
this._parseCipherRequest(NodeCipher.Actions.DECRYPT, options, err => {
if (_.isFunction(callback)) {

@@ -90,18 +98,24 @@ callback.call(scope, err);

* @see _cipher
* @param {Object} action
* @param {Object} options
* @param {Crypto.<Cipher|Decipher>} method
* @param {Function} [done]
* @param {Function} done
* @private
*/
_encryptOrDecrypt(options, method, done) {
_parseCipherRequest(action, options, done) {
let opts = this._parseOptions(options);
let errors = this._validateOptions(opts);
action.debugger('attempt with options (async): ' + JSON.stringify(opts));
// Check for errors.
if (errors.length) {
let err = new Error(errors[0].message);
let errorMessage = _.first(errors).message;
let err = new Error(errorMessage);
done(err);
} else {
this._cipher(options, method, done);
action.debugger('encountered error: ' + errorMessage);
return done(err);
}
this._cipher(action, opts, done);
}

@@ -112,32 +126,32 @@

*
* @param {Object} action
* @param {Object} options
* @param {Crypto.<Cipher|Decipher>} method
* @param {Function} done
* @private
*/
_cipher(options, method, done) {
_cipher(action, options, done) {
this._deriveKeyFromOptions(options, (err, key) => {
if (!err) {
let readStream = fse.createReadStream(options.input);
let writeStream = fse.createOutputStream(options.output);
let handleError = this._handleStreamError(readStream, done);
let cipher = method(options.algorithm, key.toString('hex'));
if (err) {
return done(err);
}
// Wait for the writable steam to finish, then call our "done" function.
writeStream.on('finish', () => {
done(null);
});
let readStream = fs.createReadStream(options.input);
let writeStream = fs.createOutputStream(options.output);
let handleError = this._handleStreamError(readStream, done);
let cipher = action.method(options.algorithm, key.toString('hex'));
// Pipe the readable input stream through our cipher method, created from
// our chosen algorithm and password, and write the ciphered result into
// our writable output stream.
readStream
.on('error', handleError)
.pipe(cipher)
.on('error', handleError)
.pipe(writeStream)
.on('error', handleError);
} else {
done(err);
}
// Wait for the writable steam to finish, then call our "done" function.
writeStream.on('finish', () => {
done(null);
});
// Pipe the readable input stream through our cipher method, created from
// our chosen algorithm and password, and write the ciphered result into
// our writable output stream.
readStream
.on('error', handleError)
.pipe(cipher)
.on('error', handleError)
.pipe(writeStream)
.on('error', handleError);
});

@@ -156,9 +170,10 @@ }

_deriveKeyFromOptions(options, callback) {
let password = options.password;
let salt = options.salt;
let iterations = options.iterations;
let keylen = options.keylen;
let digest = options.digest;
crypto.pbkdf2(password, salt, iterations, keylen, digest, callback);
crypto.pbkdf2(
options.password,
options.salt,
options.iterations,
options.keylen,
options.digest,
callback
);
}

@@ -169,3 +184,3 @@

*
* @see _encryptOrDecryptSync
* @see _parseCipherRequestSync
* @param {Object} options

@@ -175,5 +190,3 @@ * @private

_encryptSync(options) {
debugEncrypt('synch encrypt with options ' + JSON.stringify(options));
this._encryptOrDecryptSync(options, crypto.createCipher);
this._parseCipherRequestSync(NodeCipher.Actions.ENCRYPT, options);
}

@@ -184,3 +197,3 @@

*
* @see _encryptOrDecryptSync
* @see _parseCipherRequestSync
* @param {Object} options

@@ -190,24 +203,30 @@ * @private

_decryptSync(options) {
debugDecrypt('synch decrypt with options ' + JSON.stringify(options));
this._encryptOrDecryptSync(options, crypto.createDecipher);
this._parseCipherRequestSync(NodeCipher.Actions.DECRYPT, options);
}
/**
* The synchronous version of _encryptOrDecrypt().
* The synchronous version of _parseCipherRequest().
*
* @see _cipherSync
* @param {Object} action
* @param {Object} options
* @param {Crypto.<Cipher|Decipher>} method
* @private
*/
_encryptOrDecryptSync(options, method) {
_parseCipherRequestSync(action, options) {
let opts = this._parseOptions(options);
let errors = this._validateOptions(opts);
action.debugger('attempt with options (sync): ' + JSON.stringify(opts));
// Check for errors.
if (errors.length) {
throw new Error(errors[0].message);
} else {
this._cipherSync(opts, method);
let errorMessage = _.first(errors).message;
let err = new Error(errorMessage);
action.debugger('encountered error: ' + errorMessage);
throw err;
}
this._cipherSync(action, opts);
}

@@ -218,14 +237,14 @@

*
* @param {Object} action
* @param {Object} options
* @param {Crypto.<Cipher|Decipher>} method
* @private
*/
_cipherSync(options, method) {
_cipherSync(action, options) {
try {
let key = this._deriveKeyFromOptionsSync(options);
let inputBuffer = fse.readFileSync(options.input);
let cipher = method(options.algorithm, key.toString('hex'));
let inputBuffer = fs.readFileSync(options.input);
let cipher = action.method(options.algorithm, key.toString('hex'));
// Write the ciphered buffer to our output file.
fse.writeFileSync(options.output, Buffer.concat([
fs.writeFileSync(options.output, Buffer.concat([
cipher.update(inputBuffer),

@@ -240,16 +259,16 @@ cipher.final()

/**
* The synchronous version of _deriveKeyFromOptions().
*
* @param {Object} options
* @returns {string}
* @private
*/
* The synchronous version of _deriveKeyFromOptions().
*
* @param {Object} options
* @returns {hex}
* @private
*/
_deriveKeyFromOptionsSync(options) {
let password = options.password;
let salt = options.salt;
let iterations = options.iterations;
let keylen = options.keylen;
let digest = options.digest;
return crypto.pbkdf2Sync(password, salt, iterations, keylen, digest);
return crypto.pbkdf2Sync(
options.password,
options.salt,
options.iterations,
options.keylen,
options.digest
);
}

@@ -280,3 +299,3 @@

// Verify that the chosen algorithm is valid.
if (!_.includes(ciphers, options.algorithm)) {
if (!_.includes(ALL_CIPHERS, options.algorithm)) {
errors.push({

@@ -307,3 +326,14 @@ path: 'algorithm',

/**
* Public API.
*
* - encrypt()
* - decrypt()
* - encryptSync()
* - decryptSync()
* - list():Array
*/
/**
* Public method for encrypting a file using the options provided.

@@ -363,3 +393,3 @@ *

list() {
return ciphers;
return ALL_CIPHERS;
}

@@ -425,2 +455,18 @@

/**
* @enum {Object} Actions
*/
NodeCipher.Actions = {
ENCRYPT: {
name: 'encrypt',
method: crypto.createCipher,
debugger: debug('nodecipher:encrypt')
},
DECRYPT: {
name: 'decrypt',
method: crypto.createDecipher,
debugger: debug('nodecipher:decrypt')
}
};
/**
* @enum {string} defaults

@@ -432,9 +478,9 @@ */

password: undefined,
salt: 'nodecipher',
iterations: 1000,
keylen: 512,
digest: 'sha1',
algorithm: 'cast5-cbc'
salt: DEFAULT_SALT, // 'nodecipher'
iterations: DEFAULT_ITERATIONS, // 1000
keylen: DEFAULT_KEYLEN, // 512
digest: DEFAULT_DIGEST, // 'sha1'
algorithm: DEFAULT_ALGORITHM // 'cast5-cbc'
};
module.exports = new NodeCipher();
{
"name": "node-cipher",
"version": "5.0.0",
"version": "5.0.1",
"description": "Securely encrypt sensitive files for use in public source control.",

@@ -5,0 +5,0 @@ "main": "index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc