From 7d5eaf17c42e36c54b9a43a2872e65576481811a Mon Sep 17 00:00:00 2001 From: Kuingsmile Date: Wed, 13 Nov 2024 13:56:09 +0800 Subject: [PATCH] :hammer: Refactor(custom): refactored aeshelper --- src/main/utils/aesHelper.ts | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/main/utils/aesHelper.ts b/src/main/utils/aesHelper.ts index e822076..1b1b808 100644 --- a/src/main/utils/aesHelper.ts +++ b/src/main/utils/aesHelper.ts @@ -6,15 +6,13 @@ import { configPaths } from '#/utils/configPaths' import { DEFAULT_AES_PASSWORD } from '#/utils/static' export class AESHelper { - key: Buffer - - constructor() { - const userPassword = picgo.getConfig(configPaths.settings.aesPassword) || DEFAULT_AES_PASSWORD - const fixedSalt = Buffer.from('a8b3c4d2e4f5098712345678feedc0de', 'hex') - const fixedIterations = 100000 - const keyLength = 32 - this.key = crypto.pbkdf2Sync(userPassword, fixedSalt, fixedIterations, keyLength, 'sha512') - } + private key: Buffer = crypto.pbkdf2Sync( + picgo.getConfig(configPaths.settings.aesPassword) || DEFAULT_AES_PASSWORD, + Buffer.from('a8b3c4d2e4f5098712345678feedc0de', 'hex'), + 100000, + 32, + 'sha512' + ) encrypt(plainText: string) { const iv = crypto.randomBytes(16) @@ -26,11 +24,9 @@ export class AESHelper { decrypt(encryptedData: string) { const [ivHex, encryptedText] = encryptedData.split(':') - if (!ivHex || !encryptedText) { - return '{}' - } - const iv = Buffer.from(ivHex, 'hex') - const decipher = crypto.createDecipheriv('aes-256-cbc', this.key, iv) + if (!ivHex || !encryptedText) return '{}' + + const decipher = crypto.createDecipheriv('aes-256-cbc', this.key, Buffer.from(ivHex, 'hex')) let decrypted = decipher.update(encryptedText, 'hex', 'utf8') decrypted += decipher.final('utf8') return decrypted