Skip to content

Commit feaece1

Browse files
committed
cache public key
1 parent 2048083 commit feaece1

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

packages/crypto/src/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { SOURCEBOT_ENCRYPTION_KEY } from './environment';
55
const algorithm = 'aes-256-cbc';
66
const ivLength = 16; // 16 bytes for CBC
77

8+
const publicKeyCache = new Map<string, string>();
9+
810
const generateIV = (): Buffer => {
911
return crypto.randomBytes(ivLength);
1012
};
@@ -67,12 +69,17 @@ export function decrypt(iv: string, encryptedText: string): string {
6769

6870
export function verifySignature(data: string, signature: string, publicKeyPath: string): boolean {
6971
try {
70-
if (!fs.existsSync(publicKeyPath)) {
71-
throw new Error(`Public key file not found at: ${publicKeyPath}`);
72+
let publicKey = publicKeyCache.get(publicKeyPath);
73+
74+
if (!publicKey) {
75+
if (!fs.existsSync(publicKeyPath)) {
76+
throw new Error(`Public key file not found at: ${publicKeyPath}`);
77+
}
78+
79+
publicKey = fs.readFileSync(publicKeyPath, 'utf8');
80+
publicKeyCache.set(publicKeyPath, publicKey);
7281
}
7382

74-
const publicKey = fs.readFileSync(publicKeyPath, 'utf8');
75-
7683
const base64Signature = signature.replace(/-/g, '+').replace(/_/g, '/');
7784
const paddedSignature = base64Signature + '='.repeat((4 - base64Signature.length % 4) % 4);
7885
const signatureBuffer = Buffer.from(paddedSignature, 'base64');

0 commit comments

Comments
 (0)