From e808166a6d59ddd0f58f9b539e0ed3ad7df845b0 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Mon, 8 Apr 2019 20:13:26 +0200 Subject: [PATCH] moved away form forge --- src/RSA.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/RSA.ts diff --git a/src/RSA.ts b/src/RSA.ts new file mode 100644 index 0000000..f820039 --- /dev/null +++ b/src/RSA.ts @@ -0,0 +1,33 @@ +import { generateKeyPair, privateDecrypt, publicEncrypt } from 'crypto' + +type PrivateKey = string +type PublicKey = string + +export type KeyPair = { + pub: PublicKey + prv: PrivateKey +} + + +export default class RSA { + + static gen = (size: number = 2 ** 12): Promise => new Promise((resolve, reject) => { + // @ts-ignore + generateKeyPair('rsa', { + modulusLength: 4096, + publicKeyEncoding: { type: 'pkcs1', format: 'pem' }, + privateKeyEncoding: { type: 'pkcs1', format: 'pem' }, + }, (err: string, pub: string, prv: string) => { + if (err) reject() + else resolve({ + pub, + prv, + }) + }) + }) + + static encrypt = (data: string, key: PublicKey): string => publicEncrypt(key, Buffer.from(data)).toString('base64') + + static decrypt = (data: string, key: PrivateKey): string => privateDecrypt(key, Buffer.from(data, 'base64')).toString() + +} \ No newline at end of file