const CryptoJS1 = require("./aes.js");
//3DES hex 16进制字符串加密
// const encodeHexString = (data, key) => {
//   let keyHex = CryptoJS.enc.Hex.parse(key);
//   let dataHex = CryptoJS.enc.Hex.parse(data);
//   let encrypted = CryptoJS.TripleDES.encrypt(dataHex, keyHex, {
//     mode: CryptoJS_mode_ECB,
//     padding: CryptoJS.pad.Pkcs7  //java 和 android pkcs5填充，js对应pkcs7
//   });
//   return encrypted.toString().toUpperCase();
//   // return encrypted.ciphertext.toString();
// };
const encodeHexString = (data, key) => {
    var byteKey =CryptoJS1.enc.Hex.parse(key);
    var byteData =CryptoJS1.enc.Hex.parse(data);
    var encrypt =CryptoJS1.TripleDES.encrypt(byteData, byteKey, { mode:CryptoJS1.mode.ECB, padding:CryptoJS1.pad.NoPadding });
    var encryptedStr = encrypt.ciphertext.toString();
    return encryptedStr;
     // return encrypted.ciphertext.toString();
   }; 

//3DES hex 16进制字符串解密
const decodeHexString = (data, key) => {
  let keyHex = CryptoJS1.enc.Hex.parse(key);
  let dataHex = CryptoJS1.enc.Hex.parse(data);
  dataHex = CryptoJS1.enc.Base64.stringify(dataHex)
  let decrypted = CryptoJS1.TripleDES.decrypt(dataHex, keyHex, { 
    mode: CryptoJS1.mode.ECB, 
    padding: CryptoJS1.pad.NoPadding 
  });
  return decrypted.toString(CryptoJS1.enc.Hex)//decrypted.toString();
};
 
const encodeHex = (data, key) =>
{
  // key 不是16个字节，或者 24 字节返回空，
  if (key.length != 32 && key.length != 48)
    return null;
  if (key.length == 32) {
    //16字节密钥，则K1 = K3, 使用前8字节填充
    key = key + key.substr(0, 16);
  }
 
  let encData = encodeHexString(data, key);
  return encData//.substr(0, 16);
};
 
const decodeHex = (data, key) =>
{
  // key 不是16个字节，或者 24 字节返回空，
  if (key.length != 32 && key.length != 48)
    return null;
 
  if (key.length == 32) {
    //16字节密钥，则K1 = K3, 使用前8字节填充
    key = key + key.substr(0, 16);
  }
 
  // //由于加密结果的后面8字节固定使用0808080808080808的密文 ，9F4AB5416A6D9DE5 填充，因此固定在需要解密的密文，后面增加9F4AB5416A6D9DE5
  // if (data.length == 32) {
  //   data = data + "9F4AB5416A6D9DE5";
  // }
  return decodeHexString(data, key);
};
 
//buffer转16进制，解析nfc扫描结果的id
function ab2hex(buffer) {
  var hexArr = Array.prototype.map.call(
      new Uint8Array(buffer),
      function (bit) {
          return ('00' + bit.toString(16)).slice(-2)
      }
  )
  return hexArr.join('').toUpperCase();
}

//buffer转num数组
function ab2NumArr(buffer) {
  var numArr = Array.prototype.map.call(
      new Uint8Array(buffer),
      function (bit) {
          return bit
      }
  )
  return numArr
}

//num数组转字节字符串
function numArrToBit(numArr){
  var bitArr = []
  for(var i in numArr){
    bitArr.push(String.fromCharCode(numArr[i]))
  }
  return bitArr.join('')
}

//num数组转16进制字符串
function numArrToHex(numArr){
  var bitArr = []
  for(var i in numArr){
    var temp = numArr[i].toString(16).toUpperCase()
    if(temp.length == 1)temp = "0"+temp
    bitArr.push(temp)
  }
  return bitArr.join('')
}
function hex2int(hex) {
  var len = hex.length, a = new Array(len), code;
  for (var i = 0; i < len; i++) {
      code = hex.charCodeAt(i);
      if (48<=code && code < 58) {
          code -= 48;
      } else {
          code = (code & 0xdf) - 65 + 10;
      }
      a[i] = code;
  }
  return a.reduce(function(acc, c) {
      acc = 16 * acc + c;
      return acc;
  }, 0);
}
//16进制字符串转num数组
function hexToNumArr(hex){
  var numArr = []
  for (var i = 0; i < hex.length;i = i + 2) {
    numArr.push(parseInt(hex.substr(i, 2), 16));
  }
  return numArr
}
function str2ab(str) {
  var buf = new ArrayBuffer(str.length*2); // 每个字符占用2个字节
  var bufView = new Uint16Array(buf);
  for (var i=0, strLen=str.length; i<strLen; i++) {
       bufView[i] = str.charCodeAt(i);
  }
  return buf;
}
function str2NumArr(str) {
  var len = str.length
  var arr = new Array(len);
  for (var i=0; i<len; i++) {
    arr[i] = str.charCodeAt(i);
  }
  return arr;
}
//
function hexToStr(hex) {
  var trimedStr = hex.trim();
  var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
  var len = rawStr.length;
  var curCharCode;
  var resultStr = [];
  for (var i = 0; i < len; i = i + 2) {
    curCharCode = parseInt(rawStr.substr(i, 2), 16);
    resultStr.push(curCharCode);
  }
  var bytesView = new Uint8Array(resultStr);
  var str = String.fromCharCode(...bytesView);
  // str = unescape(str.replace(/\\u/g, '%u'))
  return str;
}
//hex转2进制
function num2bin(num){
  return ("00000000" + (parseInt(num)).toString(2)).substr(-8);
}
let tools = {
  encode3des:encodeHex,
  decode3des:decodeHex,
  ab2hex,
  ab2NumArr,
  numArrToBit,
  numArrToHex,
  hexToNumArr,
  str2ab,
  hexToStr,
  num2bin,
  hex2int,
  str2NumArr,
}

module.exports = {
  tools
}