🔨 Refactor(custom): refactored aeshelper

This commit is contained in:
Kuingsmile 2024-11-13 13:56:09 +08:00
parent fdae9bda2d
commit 7d5eaf17c4

View File

@ -6,15 +6,13 @@ import { configPaths } from '#/utils/configPaths'
import { DEFAULT_AES_PASSWORD } from '#/utils/static' import { DEFAULT_AES_PASSWORD } from '#/utils/static'
export class AESHelper { export class AESHelper {
key: Buffer private key: Buffer = crypto.pbkdf2Sync(
picgo.getConfig<string>(configPaths.settings.aesPassword) || DEFAULT_AES_PASSWORD,
constructor() { Buffer.from('a8b3c4d2e4f5098712345678feedc0de', 'hex'),
const userPassword = picgo.getConfig<string>(configPaths.settings.aesPassword) || DEFAULT_AES_PASSWORD 100000,
const fixedSalt = Buffer.from('a8b3c4d2e4f5098712345678feedc0de', 'hex') 32,
const fixedIterations = 100000 'sha512'
const keyLength = 32 )
this.key = crypto.pbkdf2Sync(userPassword, fixedSalt, fixedIterations, keyLength, 'sha512')
}
encrypt(plainText: string) { encrypt(plainText: string) {
const iv = crypto.randomBytes(16) const iv = crypto.randomBytes(16)
@ -26,11 +24,9 @@ export class AESHelper {
decrypt(encryptedData: string) { decrypt(encryptedData: string) {
const [ivHex, encryptedText] = encryptedData.split(':') const [ivHex, encryptedText] = encryptedData.split(':')
if (!ivHex || !encryptedText) { if (!ivHex || !encryptedText) return '{}'
return '{}'
} const decipher = crypto.createDecipheriv('aes-256-cbc', this.key, Buffer.from(ivHex, 'hex'))
const iv = Buffer.from(ivHex, 'hex')
const decipher = crypto.createDecipheriv('aes-256-cbc', this.key, iv)
let decrypted = decipher.update(encryptedText, 'hex', 'utf8') let decrypted = decipher.update(encryptedText, 'hex', 'utf8')
decrypted += decipher.final('utf8') decrypted += decipher.final('utf8')
return decrypted return decrypted