From c866598e8561f07b5f76c6134966bf667dc51894 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 5 Oct 2016 02:21:19 -0700 Subject: [PATCH] crypto: error messages. --- lib/crypto/crypto.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/crypto/crypto.js b/lib/crypto/crypto.js index 09b6388f..9918f1ca 100644 --- a/lib/crypto/crypto.js +++ b/lib/crypto/crypto.js @@ -294,6 +294,12 @@ crypto.encrypt = co(function* encrypt(data, passphrase, iv) { crypto.encipher = function encipher(data, key, iv) { var cipher; + assert(Buffer.isBuffer(data)); + assert(Buffer.isBuffer(key)); + assert(Buffer.isBuffer(iv)); + assert(key.length === 32); + assert(iv.length === 16); + if (!nodeCrypto) return aes.cbc.encrypt(data, key, iv); @@ -340,6 +346,28 @@ crypto.decrypt = co(function* decrypt(data, passphrase, iv) { */ crypto.decipher = function decipher(data, key, iv) { + assert(Buffer.isBuffer(data)); + assert(Buffer.isBuffer(key)); + assert(Buffer.isBuffer(iv)); + assert(key.length === 32); + assert(iv.length === 16); + try { + return crypto._decipher(data, key, iv); + } catch (e) { + throw new Error('Bad key for decryption.'); + } +}; + +/** + * Decrypt with aes-256-cbc. + * @private + * @param {Buffer} data + * @param {Buffer} key + * @param {Buffer} iv + * @returns {Buffer} + */ + +crypto._decipher = function decipher(data, key, iv) { var decipher; if (!nodeCrypto)