@@ -2,6 +2,7 @@ import { gcm } from '@noble/ciphers/aes';
22import { randomBytes } from '@noble/ciphers/webcrypto' ;
33import { secp256r1 } from '@noble/curves/p256' ;
44import { Buffer } from 'buffer' ;
5+ import { unzlibSync , zlibSync } from 'fflate' ;
56
67import { CryptoKey , CryptoKeyPair } from './types' ;
78import { EncryptedData , RPCRequest , RPCResponse } from ':core/message' ;
@@ -61,7 +62,8 @@ export async function encrypt(sharedSecret: CryptoKey, plainText: string): Promi
6162 const iv = randomBytes ( 12 ) ;
6263 const stream = gcm ( sharedSecret . _key , iv ) ;
6364 const plainTextBytes = new Uint8Array ( Buffer . from ( plainText , 'utf8' ) ) ;
64- const cipherText = stream . encrypt ( plainTextBytes ) ;
65+ const compressedBytes = zlibSync ( plainTextBytes ) ;
66+ const cipherText = stream . encrypt ( compressedBytes ) ;
6567
6668 return {
6769 iv,
@@ -74,8 +76,8 @@ export async function decrypt(
7476 { iv, cipherText } : EncryptedData
7577) : Promise < string > {
7678 const stream = gcm ( new Uint8Array ( sharedSecret . _key ) , new Uint8Array ( iv ) ) ;
77- const plainTextBytes = stream . decrypt ( new Uint8Array ( cipherText ) ) ;
78-
79+ const compressedBytes = stream . decrypt ( new Uint8Array ( cipherText ) ) ;
80+ const plainTextBytes = unzlibSync ( compressedBytes ) ;
7981 return Buffer . from ( plainTextBytes ) . toString ( 'utf8' ) ;
8082}
8183
0 commit comments