var CryptoJS = require("crypto-js");
console.log(CryptoJS.HmacSHA1("Message", "Key"));

let crypto = {}

//前后端定义的密钥，AES使用16位
const CRYPTOJS_KEY = "uAY9ugkHQpvozZeA";

const WHITE_LIST = [
	'/yeepay',
	'/put-file',
	'/blade-test',
	'/payment',
	'/sms',
	'/mall/yeepay/product/fee/modifyProductFeeNotify',
	'/shop/yeepay/product/fee/modifyProductFeeNotify',
	'/withdraw',
	'/pressure_test/scanPay',
	'/pressure_test/deCodeNotifyData',
	'/ocr',
	'/import-bank-type'
]

crypto.isNotInWhiteList = (url) => {
	return !WHITE_LIST.some(item => {
		return url.indexOf(item) > 0
	})
}


// 加密
crypto.encrypt = (plaintText) => {
	var plaintText = plaintText;
	var options = {
		mode: CryptoJS.mode.ECB,
		padding: CryptoJS.pad.Pkcs7
	};
	var key = CryptoJS.enc.Utf8.parse(CRYPTOJS_KEY);
	var encryptedData = CryptoJS.AES.encrypt(plaintText, key, options);
	var encryptedBase64Str = encryptedData.toString().replace(/\//g, "_");
	encryptedBase64Str = encryptedBase64Str.replace(/\+/g, "-");
	return encryptedBase64Str;
}

//解密
crypto.decrypt = (encryptedBase64Str) => {
	var vals = encryptedBase64Str.replace(/\-/g, '+').replace(/_/g, '/');
	var options = {
		mode: CryptoJS.mode.ECB,
		padding: CryptoJS.pad.Pkcs7
	};
	var key = CryptoJS.enc.Utf8.parse(CRYPTOJS_KEY);
	var decryptedData = CryptoJS.AES.decrypt(vals, key, options);
	var decryptedStr = CryptoJS.enc.Utf8.stringify(decryptedData);
	return decryptedStr
}

export default crypto
