smartcrypto/ts/smartcrypto.classes.keypair.ts

35 lines
992 B
TypeScript
Raw Normal View History

2022-10-23 21:53:34 +00:00
import * as plugins from './smartcrypto.plugins.js';
import { PublicKey } from './smartcrypto.classes.publickey.js';
import { PrivateKey } from './smartcrypto.classes.privatekey.js';
2019-10-01 13:27:21 +00:00
export class KeyPair {
// STATIC
public static async createNewKeyPair(): Promise<KeyPair> {
const done = plugins.smartpromise.defer<KeyPair>();
const rsa = plugins.nodeForge.pki.rsa;
2019-10-01 17:28:50 +00:00
rsa.generateKeyPair({ bits: 2048, workers: 2 }, async (err, keypair) => {
2019-10-01 13:27:21 +00:00
if (err) {
console.log(err);
throw err;
}
2019-10-01 17:28:50 +00:00
done.resolve(
new KeyPair({
privateKey: new PrivateKey(keypair.privateKey),
2021-02-20 17:52:21 +00:00
publicKey: new PublicKey(keypair.publicKey),
2019-10-01 17:28:50 +00:00
})
);
});
2019-10-01 13:27:21 +00:00
return done.promise;
}
// INSTANCE
public publicKey: PublicKey;
public privateKey: PrivateKey;
2019-10-01 17:28:50 +00:00
constructor(optionsArg: { privateKey: PrivateKey; publicKey: PublicKey }) {
2019-10-01 13:27:21 +00:00
this.privateKey = optionsArg.privateKey;
this.publicKey = optionsArg.publicKey;
}
2019-10-01 17:28:50 +00:00
}