mirror of
https://github.com/soybeanjs/soybean-admin.git
synced 2025-12-26 17:55:58 +08:00
feat(projects): 添加cryptojs,对本地缓存数据进行加密
This commit is contained in:
27
src/utils/crypto/index.ts
Normal file
27
src/utils/crypto/index.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import CryptoJS from 'crypto-js';
|
||||
|
||||
const CryptoSecret = '__CryptoJS_Secret__';
|
||||
|
||||
/**
|
||||
* 加密数据
|
||||
* @param data - 数据
|
||||
* @param secret - 密钥
|
||||
*/
|
||||
export function encrypto(data: any) {
|
||||
const newData = JSON.stringify(data);
|
||||
return CryptoJS.AES.encrypt(newData, CryptoSecret).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密数据
|
||||
* @param ciphertext - 密文
|
||||
* @param secret - 密钥
|
||||
*/
|
||||
export function decrypto(ciphertext: string) {
|
||||
const bytes = CryptoJS.AES.decrypt(ciphertext, CryptoSecret);
|
||||
const originalText = bytes.toString(CryptoJS.enc.Utf8);
|
||||
if (originalText) {
|
||||
return JSON.parse(originalText);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user