mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-03-13 08:28:13 -04:00
🔨 Refactor(custom): refactor encrypt class
This commit is contained in:
parent
0f7b043da3
commit
2fab2a9765
@ -3,18 +3,15 @@ import picgo from '@core/picgo'
|
|||||||
import { DEFAULT_AES_PASSWORD } from '~/universal/utils/static'
|
import { DEFAULT_AES_PASSWORD } from '~/universal/utils/static'
|
||||||
import { configPaths } from '~/universal/utils/configPaths'
|
import { configPaths } from '~/universal/utils/configPaths'
|
||||||
|
|
||||||
function getDerivedKey (): Buffer {
|
|
||||||
const userPassword = picgo.getConfig<string>(configPaths.settings.aesPassword) || DEFAULT_AES_PASSWORD
|
|
||||||
const fixedSalt = Buffer.from('a8b3c4d2e4f5098712345678feedc0de', 'hex')
|
|
||||||
const fixedIterations = 100000
|
|
||||||
const keyLength = 32
|
|
||||||
return crypto.pbkdf2Sync(userPassword, fixedSalt, fixedIterations, keyLength, 'sha512')
|
|
||||||
}
|
|
||||||
|
|
||||||
export class AESHelper {
|
export class AESHelper {
|
||||||
key: Buffer
|
key: Buffer
|
||||||
|
|
||||||
constructor () {
|
constructor () {
|
||||||
this.key = getDerivedKey()
|
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')
|
||||||
}
|
}
|
||||||
|
|
||||||
encrypt (plainText: string) {
|
encrypt (plainText: string) {
|
||||||
@ -22,17 +19,15 @@ export class AESHelper {
|
|||||||
const cipher = crypto.createCipheriv('aes-256-cbc', this.key, iv)
|
const cipher = crypto.createCipheriv('aes-256-cbc', this.key, iv)
|
||||||
let encrypted = cipher.update(plainText, 'utf8', 'hex')
|
let encrypted = cipher.update(plainText, 'utf8', 'hex')
|
||||||
encrypted += cipher.final('hex')
|
encrypted += cipher.final('hex')
|
||||||
const encryptedData = `${iv.toString('hex')}:${encrypted}`
|
return `${iv.toString('hex')}:${encrypted}`
|
||||||
return encryptedData
|
|
||||||
}
|
}
|
||||||
|
|
||||||
decrypt (encryptedData: string) {
|
decrypt (encryptedData: string) {
|
||||||
const parts = encryptedData.split(':')
|
const [ivHex, encryptedText] = encryptedData.split(':')
|
||||||
if (parts.length !== 2) {
|
if (!ivHex || !encryptedText) {
|
||||||
return '{}'
|
return '{}'
|
||||||
}
|
}
|
||||||
const iv = Buffer.from(parts[0], 'hex')
|
const iv = Buffer.from(ivHex, 'hex')
|
||||||
const encryptedText = parts[1]
|
|
||||||
const decipher = crypto.createDecipheriv('aes-256-cbc', this.key, iv)
|
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')
|
||||||
|
Loading…
Reference in New Issue
Block a user