[fix] cleanup secure with new module
This commit is contained in:
parent
3d4682a62f
commit
b447268097
2 changed files with 11 additions and 34 deletions
|
@ -9,6 +9,7 @@ var crypto = require('crypto'),
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
util = require('util'),
|
util = require('util'),
|
||||||
|
Secure = require('secure-keys'),
|
||||||
formats = require('../formats'),
|
formats = require('../formats'),
|
||||||
Memory = require('./memory').Memory,
|
Memory = require('./memory').Memory,
|
||||||
exists = fs.exists || path.exists,
|
exists = fs.exists || path.exists,
|
||||||
|
@ -49,6 +50,12 @@ var File = exports.File = function (options) {
|
||||||
if (!this.secure.secret) {
|
if (!this.secure.secret) {
|
||||||
throw new Error('secure.secret option is required');
|
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) {
|
if (options.search) {
|
||||||
|
@ -166,19 +173,7 @@ File.prototype.stringify = function () {
|
||||||
self = this;
|
self = this;
|
||||||
|
|
||||||
if (this.secure) {
|
if (this.secure) {
|
||||||
data = Object.keys(data).reduce(function (acc, key) {
|
data = this.keys.encrypt(data);
|
||||||
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;
|
|
||||||
}, {});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.format.stringify(data, null, this.spacing);
|
return this.format.stringify(data, null, this.spacing);
|
||||||
|
@ -197,18 +192,11 @@ File.prototype.parse = function (contents) {
|
||||||
return parsed;
|
return parsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Object.keys(parsed).reduce(function (acc, key) {
|
return this.keys.decrypt(parsed);
|
||||||
var decrypted = cipherConvert(parsed[key].value, {
|
|
||||||
alg: parsed[key].alg || self.secure.alg,
|
|
||||||
secret: self.secure.secret,
|
|
||||||
encs: { input: 'hex', output: 'utf8' }
|
|
||||||
});
|
|
||||||
|
|
||||||
acc[key] = self.format.parse(decrypted);
|
|
||||||
return acc;
|
|
||||||
}, {});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// ### function search (base)
|
// ### function search (base)
|
||||||
// #### @base {string} Base directory (or file) to begin searching for the target file.
|
// #### @base {string} Base directory (or file) to begin searching for the target file.
|
||||||
|
@ -299,15 +287,3 @@ File.prototype.search = function (base) {
|
||||||
|
|
||||||
return fullpath;
|
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": {
|
"dependencies": {
|
||||||
"async": "^1.4.0",
|
"async": "^1.4.0",
|
||||||
"ini": "^1.3.0",
|
"ini": "^1.3.0",
|
||||||
|
"secure-keys": "^1.0.0",
|
||||||
"yargs": "^3.19.0"
|
"yargs": "^3.19.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
Loading…
Reference in a new issue