55 lines
1.2 KiB
TypeScript
55 lines
1.2 KiB
TypeScript
|
|
import AES from 'crypto-js/aes';
|
||
|
|
import Pkcs7 from 'crypto-js/pad-pkcs7';
|
||
|
|
import ECB from 'crypto-js/mode-ecb';
|
||
|
|
import Md5 from 'crypto-js/md5';
|
||
|
|
import UTF8 from 'crypto-js/enc-utf8';
|
||
|
|
import Base64 from 'crypto-js/enc-base64';
|
||
|
|
|
||
|
|
export interface EncryptionParams {
|
||
|
|
key: string;
|
||
|
|
iv: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export class AesEncryption {
|
||
|
|
private key;
|
||
|
|
private iv;
|
||
|
|
|
||
|
|
constructor(opt: Partial<EncryptionParams> = {}) {
|
||
|
|
const { key, iv } = opt;
|
||
|
|
if (key) {
|
||
|
|
this.key = UTF8.parse(key);
|
||
|
|
}
|
||
|
|
if (iv) {
|
||
|
|
this.iv = UTF8.parse(iv);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
get getOptions() {
|
||
|
|
return {
|
||
|
|
mode: ECB,
|
||
|
|
padding: Pkcs7,
|
||
|
|
iv: this.iv,
|
||
|
|
} as any;
|
||
|
|
}
|
||
|
|
|
||
|
|
encryptByAES(cipherText: string) {
|
||
|
|
return AES.encrypt(cipherText, this.key, this.getOptions).toString();
|
||
|
|
}
|
||
|
|
|
||
|
|
decryptByAES(cipherText: string) {
|
||
|
|
return AES.decrypt(cipherText, this.key, this.getOptions).toString(UTF8);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function encryptByBase64(cipherText: string) {
|
||
|
|
return UTF8.parse(cipherText).toString(Base64);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function decryptByBase64(cipherText: string) {
|
||
|
|
return Base64.parse(cipherText).toString(UTF8);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function encryptByMd5(password: string) {
|
||
|
|
return Md5(password).toString();
|
||
|
|
}
|