mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
feat(projects): 1.0 beta
This commit is contained in:
29
packages/utils/src/crypto.ts
Normal file
29
packages/utils/src/crypto.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import CryptoJS from 'crypto-js';
|
||||
|
||||
export class Crypto<T extends object> {
|
||||
/**
|
||||
* secret
|
||||
*/
|
||||
secret: string;
|
||||
|
||||
constructor(secret: string) {
|
||||
this.secret = secret;
|
||||
}
|
||||
|
||||
encrypt(data: T): string {
|
||||
const dataString = JSON.stringify(data);
|
||||
const encrypted = CryptoJS.AES.encrypt(dataString, this.secret);
|
||||
return encrypted.toString();
|
||||
}
|
||||
|
||||
decrypt(encrypted: string) {
|
||||
const decrypted = CryptoJS.AES.decrypt(encrypted, this.secret);
|
||||
const dataString = decrypted.toString(CryptoJS.enc.Utf8);
|
||||
try {
|
||||
return JSON.parse(dataString) as T;
|
||||
} catch {
|
||||
// avoid parse error
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user