From b44726809748f0d945d11cc1f6ee9c1658e502d6 Mon Sep 17 00:00:00 2001 From: Jarrett Cruger Date: Mon, 1 Feb 2016 22:21:15 -0800 Subject: [PATCH] [fix] cleanup secure with new module --- lib/nconf/stores/file.js | 44 +++++++++------------------------------- package.json | 1 + 2 files changed, 11 insertions(+), 34 deletions(-) diff --git a/lib/nconf/stores/file.js b/lib/nconf/stores/file.js index 78a23ff..5761b0a 100644 --- a/lib/nconf/stores/file.js +++ b/lib/nconf/stores/file.js @@ -9,6 +9,7 @@ var crypto = require('crypto'), fs = require('fs'), path = require('path'), util = require('util'), + Secure = require('secure-keys'), formats = require('../formats'), Memory = require('./memory').Memory, exists = fs.exists || path.exists, @@ -49,6 +50,12 @@ var File = exports.File = function (options) { if (!this.secure.secret) { throw new Error('secure.secret option is required'); } + + this.keys = new Secure({ + secret: this.secure.secret, + alg: this.secure.alg, + format: this.format + }); } if (options.search) { @@ -166,19 +173,7 @@ File.prototype.stringify = function () { self = this; if (this.secure) { - data = Object.keys(data).reduce(function (acc, key) { - var value = self.format.stringify(data[key]); - acc[key] = { - alg: self.secure.alg, - value: cipherConvert(value, { - alg: self.secure.alg, - secret: self.secure.secret, - encs: { input: 'utf8', output: 'hex' } - }) - } - - return acc; - }, {}); + data = this.keys.encrypt(data); } return this.format.stringify(data, null, this.spacing); @@ -197,18 +192,11 @@ File.prototype.parse = function (contents) { return parsed; } - return Object.keys(parsed).reduce(function (acc, key) { - var decrypted = cipherConvert(parsed[key].value, { - alg: parsed[key].alg || self.secure.alg, - secret: self.secure.secret, - encs: { input: 'hex', output: 'utf8' } - }); + return this.keys.decrypt(parsed); - acc[key] = self.format.parse(decrypted); - return acc; - }, {}); }; + // // ### function search (base) // #### @base {string} Base directory (or file) to begin searching for the target file. @@ -299,15 +287,3 @@ File.prototype.search = function (base) { return fullpath; }; - -// -// ### function cipherConvert (contents, opts) -// Returns the result of the cipher operation -// on the contents contents. -// -function cipherConvert(contents, opts) { - var encs = opts.encs; - var cipher = crypto.createCipher(opts.alg, opts.secret); - return cipher.update(contents, encs.input, encs.output) - + cipher.final(encs.output); -} diff --git a/package.json b/package.json index ee96101..a9747ff 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "dependencies": { "async": "^1.4.0", "ini": "^1.3.0", + "secure-keys": "^1.0.0", "yargs": "^3.19.0" }, "devDependencies": {