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": {