smartkey/ts/smartkey.classes.keypair.ts

50 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

2019-02-23 14:53:18 +00:00
import * as plugins from './smartkey.plugins';
/**
* represents a keypair
*/
2019-02-23 15:02:12 +00:00
export class KeyPair {
2019-02-23 14:53:18 +00:00
// static
2019-02-23 15:02:12 +00:00
public static async createKeyPair(passphraseArg = ''): Promise<KeyPair> {
2019-02-23 14:53:18 +00:00
const done = plugins.smartpromise.defer<KeyPair>();
2019-02-23 15:02:12 +00:00
plugins.crypto.generateKeyPair(
'rsa',
{
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: passphraseArg
}
2019-02-23 14:53:18 +00:00
},
2019-02-23 15:02:12 +00:00
(err, publicKeyArg, privateKeyArg) => {
// convey error
if (err) {
done.reject(err);
throw err;
}
const keyPairInstance = new KeyPair({
privateKey: privateKeyArg,
publicKey: publicKeyArg
});
done.resolve(keyPairInstance);
2019-02-23 14:53:18 +00:00
}
2019-02-23 15:02:12 +00:00
);
2019-02-23 14:53:18 +00:00
return done.promise;
}
// instance
public publicKey: string;
public privateKey: string;
2019-02-23 15:02:12 +00:00
constructor(optionsArg: { privateKey: string; publicKey: string }) {
2019-02-23 14:53:18 +00:00
this.privateKey = optionsArg.privateKey;
this.publicKey = optionsArg.publicKey;
}
2019-02-23 15:02:12 +00:00
}