mirror of
https://github.com/node-ebics/node-ebics-client.git
synced 2024-11-23 06:32:08 +00:00
50 lines
1.0 KiB
JavaScript
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');
|
|
},
|
|
});
|