mirror of
https://github.com/XiaoDaiGua-Ray/ray-template.git
synced 2026-11-27 23:13:38 +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.
32 lines
961 B
TypeScript
32 lines
961 B
TypeScript
import { AES, lib } from 'crypto-js'
|
||
import { CRYPTO_KEY } from './constant'
|
||
import type { WordArray } from './types'
|
||
|
||
/**
|
||
*
|
||
* @param data 可序列化为 JSON 的数据
|
||
* @param key AES 口令或原始密钥,默认使用模板历史口令
|
||
*
|
||
* @description
|
||
* 字符串口令沿用历史 OpenSSL 格式;原始密钥使用独立的随机 IV,并将 IV 与密文一并返回。
|
||
*
|
||
* @returns 可传给 decrypt 的序列化密文。
|
||
*
|
||
* @throws 数据不可序列化或 AES 加密失败时抛错。
|
||
*/
|
||
export const encrypt = (data: unknown, key?: string | WordArray) => {
|
||
const json = JSON.stringify(data)
|
||
|
||
if (json === void 0)
|
||
throw new TypeError('[encrypt]: Value is not JSON serializable.')
|
||
|
||
if (typeof key === 'object') {
|
||
const iv = lib.WordArray.random(16)
|
||
const ciphertext = AES.encrypt(json, key, { iv })
|
||
|
||
return `${iv.toString()}:${ciphertext.toString()}`
|
||
}
|
||
|
||
return AES.encrypt(json, key || CRYPTO_KEY).toString()
|
||
}
|