Merge pull request #208 from indexzero/clean-secure
[fix] cleanup secure with new module
This commit is contained in:
commit
f554382435
2 changed files with 11 additions and 34 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
"dependencies": {
|
||||
"async": "^1.4.0",
|
||||
"ini": "^1.3.0",
|
||||
"secure-keys": "^1.0.0",
|
||||
"yargs": "^3.19.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
Loading…
Reference in a new issue