import CryptoJS from 'crypto-js'
import bcrypt from 'bcryptjs'
export const encrypt = function(word, keyStr) {
  keyStr = keyStr || '1994+=--jkl;P,fj'
  const key = CryptoJS.enc.Utf8.parse(keyStr)
  const srcs = CryptoJS.enc.Utf8.parse(word)
  const encrypted = CryptoJS.AES.encrypt(srcs, key, {
    iv: key,
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  return encrypted.toString()
}
export const md5 = function(word) {
  return CryptoJS.MD5(word).toString()
}

/**
 * AES解密
 * @param {any} word 解密数据
 */
export const cryptoDecrypt = (word, keyStr) => {
  const secretKey = CryptoJS.enc.Utf8.parse(keyStr)
  const ivParsed = CryptoJS.enc.Utf8.parse(keyStr)
  const decrypt = CryptoJS.AES.decrypt(word, secretKey, { iv: ivParsed, mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
  const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
  return decryptedStr
}
export const generateSalt = () => {
  return bcrypt.genSaltSync(10)
}

