code optimization

This commit is contained in:
Vladislav Hristov
2018-06-15 09:33:41 +03:00
parent d5d80ee1b8
commit 187636019c
26 changed files with 776 additions and 669 deletions

View File

@@ -1,40 +0,0 @@
'use strict';
const fs = require('fs');
/* const extractKeys = (keysObject, encryptAlgorithm, passphrase) => Object.entries(keysObject).reduce((keys, [key, data]) => {
keys[key] = decrypt(data, encryptAlgorithm, passphrase);
return keys;
}, {}); */
module.exports = class FsKeyStorage {
/**
* @param {String} path - destingiton file to save the keys
*/
constructor({ path }) {
if (!path)
throw new Error('Invalid path provided');
this._path = path;
}
get path() {
return this._path;
}
read() {
return fs.readFileSync(this._path, { encoding: 'utf8' });
// return extractKeys(JSON.parse(fs.readFileSync(this._path, { encoding: 'utf8' })), this.algorithm, this.passphrase);
}
save(data) {
fs.writeFileSync(this._path, data, { encoding: 'utf8' });
// fs.writeFileSync(this._path, encrypt(JSON.stringify(data), this.algorithm, this.passphrase), { encoding: 'utf8' });
}
hasData() {
if (fs.existsSync(this._path))
return this.read() !== '';
return false;
}
};

View File

@@ -19,76 +19,50 @@ const decrypt = (data, algorithm, passphrase) => {
return decrypted;
};
module.exports = class KeysManager {
constructor(keysStorage, passphrase, algorithm = 'aes-256-cbc', createIfNone = true) {
this._storage = keysStorage;
this._passphrase = passphrase;
this._algorithm = algorithm;
module.exports = (keysStorage, passphrase, algorithm = 'aes-256-cbc') => {
const storage = keysStorage;
const pass = passphrase;
const algo = algorithm;
// const createIfNone = createIfNone;
if (createIfNone && !this._storage.hasData())
this.generate();
}
return {
generate(save = true) {
const keys = Keys.generate();
/**
* Generates the keys to work with. Then either
* saves them to the storage or returnes the keys generated
*
* @param {Boolean} save
* @default true
*
* @returns void | Keys object
*/
generate(save = true) {
const keys = Keys.generate();
if (save) {
this.write(keys);
if (save) this.write(keys);
return this;
}
return keys;
}
return keys;
},
/**
* Writes the keys to the storage
*
* @param {Keys} keysObject
*
* @returns void
*/
write(keysObject) {
keysObject = keysObject.keys;
write(keysObject) {
keysObject = keysObject.keys;
Object.keys(keysObject).map((key) => {
keysObject[key] = keysObject[key] === null ? null : keysObject[key].toPem();
Object.keys(keysObject).map((key) => {
keysObject[key] = keysObject[key] === null ? null : keysObject[key].toPem();
return key;
});
return key;
});
this._storage.save(encrypt(JSON.stringify(keysObject), this._algorithm, this._passphrase));
}
storage.save(encrypt(JSON.stringify(keysObject), algo, pass));
setBankKeys(bankKeys) {
const keys = this.keys();
return this;
},
keys.setBankKeys(bankKeys);
this.write(keys);
}
setBankKeys(bankKeys) {
const keys = this.keys();
/**
* Gets the keys
*
* @returns Keys object
*/
keys() {
return this._read();
}
keys.setBankKeys(bankKeys);
this.write(keys);
},
/**
* Reads the keys from the storage
*
* @returns Keys object
*/
_read() {
const keysString = this._storage.read();
keys() {
const keysString = storage.read();
return new Keys(JSON.parse(decrypt(keysString, this._algorithm, this._passphrase)));
}
return new Keys(JSON.parse(decrypt(keysString, algo, pass)));
},
};
};

View File

@@ -0,0 +1,26 @@
'use strict';
const fs = require('fs');
module.exports = (pathToFile) => {
const path = pathToFile;
return {
read() {
return fs.readFileSync(path, { encoding: 'utf8' });
},
save(data) {
fs.writeFileSync(path, data, { encoding: 'utf8' });
return this;
},
hasData() {
if (fs.existsSync(path))
return this.read() !== '';
return false;
},
};
};