import CryptoJS from "crypto-js";

const key = CryptoJS.enc.Utf8.parse("daac3ae52eff4cec"); // 16位

const encrypt = (word) => {
  let encrypted = "";
  if (typeof word === "string") {
    const src = CryptoJS.enc.Utf8.parse(word);
    encrypted = CryptoJS.AES.encrypt(src, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7,
    });
  } else if (typeof word === "object") {
    // 对象格式的转成json字符串
    const data = JSON.stringify(word);
    const src = CryptoJS.enc.Utf8.parse(data);
    encrypted = CryptoJS.AES.encrypt(src, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7,
    });
  }
  return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
};

export { encrypt };
