import * as forge from 'node-forge' ; import { CryptoStore } from '.' ; export async function createForgeCrypto ( pubKey ) { const publicKey = forge . pki . publicKeyFromPem ( pubKey ) ; const key = forge . random . getBytesSync ( 16 ) ; return { toAESEncrypted ( buffer , iv ) { const cipher = forge . cipher . createCipher ( 'AES-CFB' , key ) ; cipher . start ( { iv : forge . util . binary . raw . encode ( iv ) } ) ; cipher . update ( new forge . util . ByteStringBuffer ( buffer ) ) ; cipher . finish ( ) ; const array = forge . util . binary . raw . decode ( cipher . output . data ) ; return array ; } , toAESDecrypted ( buffer , iv ) { const cipher = forge . cipher . createDecipher ( 'AES-CFB' , key ) ; cipher . start ( { iv : forge . util . binary . raw . encode ( iv ) } ) ; cipher . update ( new forge . util . ByteStringBuffer ( buffer ) ) ; const array = forge . util . binary . raw . decode ( cipher . output . data ) ; return array ; } , toRSAEncrypted ( buffer ) { const encrypted = publicKey . encrypt ( new forge . util . ByteStringBuffer ( buffer ) . data , 'RSA-OAEP' ) ; return forge . util . binary . raw . decode ( encrypted ) ; } , randomCipherIV ( ) { const buffer = new Uint8Array ( 16 ) ; forge . util . binary . raw . decode ( forge . random . getBytesSync ( 16 ) , buffer ) ; return buffer ; } , getRSAEncryptedKey ( ) { const encrypted = publicKey . encrypt ( new forge . util . ByteStringBuffer ( key ) . data , 'RSA-OAEP' ) ; return forge . util . binary . raw . decode ( encrypted ) ; } , } ; }