mirror of
https://github.com/cupcakearmy/occulto.git
synced 2024-12-22 15:16:30 +00:00
moved away form forge
This commit is contained in:
parent
ab645ae0c3
commit
e808166a6d
33
src/RSA.ts
Normal file
33
src/RSA.ts
Normal file
@ -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<KeyPair> => new Promise<KeyPair>((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()
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user