// https://blog.csdn.net/HuanFengZhiQiu/article/details/109174550
import CryptoJS from 'crypto-js';

export default class Crypto {
  constructor(key: string, options?: any) {
    if (!key) {
      throw Error('必须有key');
    }
    this.key = CryptoJS.enc.Utf8.parse(key);
  }

  encrypt(content) {
    const encryptedData = CryptoJS.AES.encrypt(content, this.key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7,
    });
    return encryptedData.toString();
  }

  decrypt(content) {
    const encryptedHexStr = CryptoJS.enc.Base64.parse(content);
    const encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    const decryptedData = CryptoJS.AES.decrypt(encryptedBase64Str, this.key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7,
    });
    return decryptedData.toString(CryptoJS.enc.Utf8);
  }
}
