import CryptoJS from 'crypto-js';

/**
 * AES128ECBEncrypt 加密方法
 */
export const AES128ECBEncrypt = (inputText,key) => {
  var inputArr = []; 
  while (inputText.length < 32) {
    inputText = inputText+'0';
	}
	for(var i=0;i<inputText.length/2;i++){
		inputArr.push(parseInt(inputText.substring(i*2, i*2+2), 16))
	}
	var inputParse= int8arrayParse(inputArr);
  var keyParse = CryptoJS.enc.Utf8.parse(key);
	var n = (inputParse.toString(CryptoJS.enc.Base64),
		CryptoJS.AES.encrypt(inputParse, keyParse, {
			iv: [],
			mode: CryptoJS.mode.ECB,
			padding: CryptoJS.pad.NoPadding
	}));
	var w = int8arrayStringify(n.ciphertext);
	var encryptStr = bufTohex(w.buffer);
  return encryptStr;
}

export const bufTohex = (buffer) => {
  return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
}

export const int8arrayParse = (e) => {
		for (var r = e.length, i = [], n = 0; n < r; n++) i[n >>> 2] |= (255 & e[n]) << 24 - n % 4 * 8;
    return CryptoJS.lib.WordArray.create(i, r);
}

export const int8arrayStringify = (t) => {
		for (var e = t.words, r = t.sigBytes, i = new Int8Array(r), n = 0; n < r; n++) {
			var o = e[n >>> 2] >> 24 - n % 4 * 8 & 255;
			i[n] = o;
		}
		return i;
}

