mirror of
https://github.com/XiaoDaiGua-Ray/ray-template.git
synced 2026-11-24 04:33:35 +08:00
Share workspace packages between Ray Admin and the interactive docs. BREAKING CHANGE: v6 is a new major line. v5 stays on legacy/v5.
37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import { AES, enc } from 'crypto-js'
|
||
import { CRYPTO_KEY } from './constant'
|
||
import type { CipherParams, WordArray } from './types'
|
||
|
||
/**
|
||
*
|
||
* @param data encrypt 返回的密文,或 CryptoJS CipherParams
|
||
* @param key 与加密时相同的口令或原始密钥
|
||
*
|
||
* @returns 解密后的 JSON;密文无法解析时记录错误并返回 undefined。
|
||
*/
|
||
export const decrypt = (
|
||
data: string | CipherParams,
|
||
key?: string | WordArray,
|
||
) => {
|
||
try {
|
||
if (typeof key === 'object') {
|
||
const parts =
|
||
typeof data === 'string' ? /^([a-f0-9]{32}):(.+)$/i.exec(data) : null
|
||
const iv =
|
||
typeof data === 'string' ? parts && enc.Hex.parse(parts[1]) : data.iv
|
||
|
||
if (!iv) throw new Error('Missing AES initialization vector.')
|
||
|
||
const decrypted = AES.decrypt(parts?.[2] ?? data, key, { iv })
|
||
|
||
return JSON.parse(decrypted.toString(enc.Utf8))
|
||
}
|
||
|
||
const decrypted = AES.decrypt(data, key || CRYPTO_KEY)
|
||
|
||
return JSON.parse(decrypted.toString(enc.Utf8))
|
||
} catch (error) {
|
||
console.error('[decrypt]: Invalid ciphertext or key.', error)
|
||
}
|
||
}
|