feat(projects): 添加cryptojs,对本地缓存数据进行加密

This commit is contained in:
Soybean
2022-01-01 22:52:05 +08:00
parent 25d3404c9c
commit 7a0648dba5
5 changed files with 994 additions and 775 deletions

27
src/utils/crypto/index.ts Normal file
View 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;
}