From 0e21c84ab43af76004b452b86bb09edbe68b2f64 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Tue, 1 Oct 2019 15:27:21 +0200 Subject: [PATCH] fix(core): update --- ts/smartcrypto.classes.keypair.ts | 36 +++++++++++++++++++++++++++ ts/smartcrypto.classes.privatekey.ts | 20 +++++++++++++++ ts/smartcrypto.classes.publickey.ts | 21 ++++++++++++++++ ts/smartcrypto.classes.smartcrypto.ts | 14 +---------- 4 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 ts/smartcrypto.classes.keypair.ts create mode 100644 ts/smartcrypto.classes.privatekey.ts create mode 100644 ts/smartcrypto.classes.publickey.ts diff --git a/ts/smartcrypto.classes.keypair.ts b/ts/smartcrypto.classes.keypair.ts new file mode 100644 index 0000000..cfcf786 --- /dev/null +++ b/ts/smartcrypto.classes.keypair.ts @@ -0,0 +1,36 @@ +import * as plugins from './smartcrypto.plugins'; +import { PublicKey } from './smartcrypto.classes.publickey'; +import { PrivateKey } from './smartcrypto.classes.privatekey'; + +export class KeyPair { + // STATIC + public static async createNewKeyPair(): Promise { + const done = plugins.smartpromise.defer(); + const rsa = plugins.nodeForge.pki.rsa; + rsa.generateKeyPair({bits: 2048, workers: 2}, async (err, keypair) => { + if (err) { + console.log(err); + throw err; + } + + done.resolve(new KeyPair({ + privateKey: new PrivateKey(keypair.privateKey), + publicKey: new PublicKey(keypair.publicKey), + })); + + }); + return done.promise; + } + + // INSTANCE + public publicKey: PublicKey; + public privateKey: PrivateKey; + + constructor(optionsArg: { + privateKey: PrivateKey; + publicKey: PublicKey; + }) { + this.privateKey = optionsArg.privateKey; + this.publicKey = optionsArg.publicKey; + } +} \ No newline at end of file diff --git a/ts/smartcrypto.classes.privatekey.ts b/ts/smartcrypto.classes.privatekey.ts new file mode 100644 index 0000000..246cc35 --- /dev/null +++ b/ts/smartcrypto.classes.privatekey.ts @@ -0,0 +1,20 @@ +import * as plugins from './smartcrypto.plugins'; + +export class PrivateKey { + // STATIC + public static createFromPrivateKey(pemString: string) { + const privateKey = plugins.nodeForge.pki.privateKeyFromPem(pemString); + return new PrivateKey(privateKey); + } + + // INSTANCE + public forgePrivateKey: plugins.nodeForge.pki.PrivateKey; + + constructor(privateKeyArg: plugins.nodeForge.pki.PrivateKey) { + this.forgePrivateKey = privateKeyArg; + } + + public toPemString(): string { + return plugins.nodeForge.pki.privateKeyToPem(this.forgePrivateKey); + } +} diff --git a/ts/smartcrypto.classes.publickey.ts b/ts/smartcrypto.classes.publickey.ts new file mode 100644 index 0000000..46d04d2 --- /dev/null +++ b/ts/smartcrypto.classes.publickey.ts @@ -0,0 +1,21 @@ +import * as plugins from './smartcrypto.plugins'; + +export class PublicKey { + // STATIC + public static createFromPrivateKey(pemString: string) { + const privateKey = plugins.nodeForge.pki.publicKeyFromPem(pemString); + return new PublicKey(privateKey); + } + + + // INSTANCE + public forgePublicKey: plugins.nodeForge.pki.PublicKey; + + constructor (publicKeyArg: plugins.nodeForge.pki.PublicKey) { + this.forgePublicKey = publicKeyArg; + } + + public toPemString(): string { + return plugins.nodeForge.pki.publicKeyToPem(this.forgePublicKey); + } +} \ No newline at end of file diff --git a/ts/smartcrypto.classes.smartcrypto.ts b/ts/smartcrypto.classes.smartcrypto.ts index 68055ea..6c3641c 100644 --- a/ts/smartcrypto.classes.smartcrypto.ts +++ b/ts/smartcrypto.classes.smartcrypto.ts @@ -1,17 +1,5 @@ import * as plugins from './smartcrypto.plugins'; export class Smartcrypto { - public static async createRSAKeyPair(): Promise { - const done = plugins.smartpromise.defer(); - const rsa = plugins.nodeForge.pki.rsa; - rsa.generateKeyPair({bits: 2048, workers: 2}, (err, keypair) => { - if (err) { - console.log(err); - throw err; - } - done.resolve(keypair); - // keypair.privateKey, keypair.publicKey - }); - return done.promise; - } + } \ No newline at end of file