/*
 * @Description: aes加密
 * @Autor: tanglitong
 * @Date: 2020-06-30 18:00:01
 * @LastEditors: tanglitong
 * @LastEditTime: 2020-08-12 14:56:06
 */
import CryptoJS from 'crypto-js'
/**
 * @description: 滑块ase加密
 * @param {word} 要加密的内容
 * @param {keyWord} String 密钥
 * @return:
 * @author: tanglitong
 */
export function aesEncrypt(word, keyWord = process.env.VUE_APP_KEY_WORD) {
  var key = CryptoJS.enc.Utf8.parse(keyWord)
  var srcs = CryptoJS.enc.Utf8.parse(word)
  var encrypted = CryptoJS.AES.encrypt(srcs, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });
  return encrypted.toString();
}

/**
 * @description: 自定义ase加密
 * @return {type}
 */
export function aesEncryptWord(word, keyWord = process.env.VUE_APP_KEY_WORD) {
  var key = CryptoJS.enc.Utf8.parse(keyWordDecrypt(keyWord))
  var srcs = CryptoJS.enc.Utf8.parse(word)
  var encrypted = CryptoJS.AES.encrypt(srcs, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });
  return encrypted.toString();
}

/**
 * @description: ase密钥解密
 * @param {type}
 * @return {type}
 */
function keyWordDecrypt(keyWord, chunk = 4, order = [0.5,0.375,0.125,0.25]) {
  const keyWordArr = keyWord.split('')
  let mergeArr = []
  let mergeStr = ''
  for (let i = 0, j = keyWordArr.length; i < j; i += chunk) {
    mergeArr.push(keyWordArr.slice(i, i + chunk).join(''))
  }

  order.forEach(element => {
    mergeStr += (mergeArr[element * 8 - 1])
  });
  return mergeStr
}

/**
 * @description: 生成字母数字随机数
 * @param {type}
 * @return {type}
 */
function randomn(n) {
  let res = ''
  for (; res.length < n; res += Math.random().toString(36).substr(2).toUpperCase()) {}
  return res.substr(0, n)
}
