node-ebics-client/packages/ebics-client/lib/keymanagers/keyRSA.js
2019-10-11 10:46:37 +03:00

50 lines
1.0 KiB
JavaScript

'use strict';
const BN = require('bn.js');
const NodeRSA = require('node-rsa');
const keyOrNull = (encodedKey) => {
if (encodedKey === null) return new NodeRSA();
return (encodedKey instanceof NodeRSA) ? encodedKey : new NodeRSA(encodedKey);
};
module.exports = encodedKey => ({
key: keyOrNull(encodedKey),
generate(keySize = 2048) {
return new NodeRSA({ b: keySize });
},
importKey({ mod, exp }) {
this.key = new NodeRSA();
this.key.importKey({ n: mod, e: exp }, 'components-public');
return this;
},
n(to = 'buff') {
const keyN = Buffer.from(this.key.exportKey('components-public').n);
return to === 'hex'
? keyN.toString('hex', 1)
: keyN;
},
e(to = 'buff') {
const eKey = new BN(this.key.exportKey('components-public').e).toBuffer();
return to === 'hex'
? eKey.toString('hex')
: eKey;
},
d() {
return this.key.keyPair.d.toBuffer();
},
toPem() {
return this.key.isPrivate() ? this.key.exportKey('pkcs1-private-pem') : this.key.exportKey('pkcs8-public-pem');
},
});