mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-02-02 02:58:13 -05:00
🔨 Refactor(custom): refactored aeshelper
This commit is contained in:
parent
fdae9bda2d
commit
7d5eaf17c4
@ -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<string>(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<string>(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
|
||||
|
Loading…
Reference in New Issue
Block a user