feat: use new Key in Keys manager

This commit is contained in:
Vladislav Hristov 2019-11-01 13:49:33 +02:00
parent 61ada747f3
commit 59a281c895
3 changed files with 82 additions and 8 deletions

View File

@ -1,8 +1,14 @@
'use strict'; 'use strict';
const Key = require('./keyRSA'); // const Key = require('./keyRSA');
const Key = require('./Key');
const keyOrNull = key => (key ? Key(key) : null); const keyOrNull = (key) => {
if (key instanceof Key)
return key;
return key ? new Key({ pem: key }) : null;
};
module.exports = class Keys { module.exports = class Keys {
constructor({ constructor({
@ -25,15 +31,15 @@ module.exports = class Keys {
const keys = {}; const keys = {};
Object.keys({ A006: '', X002: '', E002: '' }).forEach((key) => { Object.keys({ A006: '', X002: '', E002: '' }).forEach((key) => {
keys[key] = Key().generate(); keys[key] = Key.generate(); // Key().generate();
}); });
return new Keys(keys); return new Keys(keys);
} }
setBankKeys(bankKeys) { setBankKeys(bankKeys) {
this.keys.bankX002 = Key().importKey(bankKeys.bankX002); this.keys.bankX002 = new Key(bankKeys.bankX002); // Key().importKey(bankKeys.bankX002);
this.keys.bankE002 = Key().importKey(bankKeys.bankE002); this.keys.bankE002 = new Key(bankKeys.bankE002); // Key().importKey(bankKeys.bankE002);
} }
a() { a() {

59
lib/keymanagers/_Keys.js Normal file
View File

@ -0,0 +1,59 @@
'use strict';
const Key = require('./keyRSA');
const keyOrNull = key => (key ? Key(key) : null);
module.exports = class Keys {
constructor({
A006,
E002,
X002,
bankX002,
bankE002,
}) {
this.keys = {
A006: keyOrNull(A006),
E002: keyOrNull(E002),
X002: keyOrNull(X002),
bankX002: keyOrNull(bankX002),
bankE002: keyOrNull(bankE002),
};
console.log('debug');
}
static generate() {
const keys = {};
Object.keys({ A006: '', X002: '', E002: '' }).forEach((key) => {
keys[key] = Key().generate();
});
return new Keys(keys);
}
setBankKeys(bankKeys) {
this.keys.bankX002 = Key().importKey(bankKeys.bankX002);
this.keys.bankE002 = Key().importKey(bankKeys.bankE002);
}
a() {
return this.keys.A006;
}
e() {
return this.keys.E002;
}
x() {
return this.keys.X002;
}
bankX() {
return this.keys.bankX002;
}
bankE() {
return this.keys.bankE002;
}
};

View File

@ -76,9 +76,18 @@ function rsaPublicKeyPem(modulus_b64, exponent_b64) {
const BN = require('bn.js'); const BN = require('bn.js');
const NodeRSA = require('node-rsa'); const NodeRSA = require('node-rsa');
const { pki: { const {
rsa, publicKeyToPem, privateKeyToPem, publicKeyFromPem, privateKeyFromPem pki: {
}, jsbn: { BigInteger } } = require('node-forge'); rsa,
publicKeyToPem,
privateKeyToPem,
publicKeyFromPem,
privateKeyFromPem,
},
jsbn: {
BigInteger,
},
} = require('node-forge');
const isKeyInstance = (obj) => { const isKeyInstance = (obj) => {
if (typeof obj !== 'object') if (typeof obj !== 'object')