package com.palmnest.application.core.util.hangdian;

import java.util.Map;

import com.palmnest.application.core.util.MD5;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EncryptionUtils {

	private static final Logger logger  = LoggerFactory.getLogger(EncryptionUtils.class);
	private static final String SIGNKEY = "9ce2da0513564b5d9a5ae2c882208f55";

	/**
	 * 功能说明：通过参数获取sign
	 *
	 * @param parameters
	 * @return
	 * @createTime 2018年5月18日 上午11:09:25
	 * @author liuli
	 */
	public static String getSign(Map<String, String> parameters) {
		String signKey = SIGNKEY;
		String dateTime = parameters.get("dateTime");
		parameters.remove("sign");
		String signString = MapUtils.mapToString(parameters);
//		String serviceSign = MD5Utils.encodeMD5Hex((signString+signKey+dateTime));
		String serviceSign = DigestUtils.md5Hex((signString + signKey + dateTime));
		return serviceSign;
	}

	/**
	 * 功能说明：验证参数是否通过校验
	 *
	 * @param parameters
	 * @return
	 * @createTime 2018年5月18日 上午11:10:23
	 * @author liuli
	 */
	public static boolean vilidationSign(Map<String, String> parameters) {
		String signKey = SIGNKEY;
		String sign = parameters.get("sign");
		String dateTime = parameters.get("dateTime");
		parameters.remove("sign");
		String signString = MapUtils.mapToString(parameters);
//		logger.info("传入signString{}",signString);
//		String serviceSign = MD5Utils.encodeMD5Hex((signString+signKey+dateTime));
		String serviceSign = DigestUtils.md5Hex((signString + signKey + dateTime));
		logger.info("传入sign:{},系统生成sign:{}", sign, serviceSign);
		if (StringUtils.isEmpty(sign) || StringUtils.isEmpty(signString)) {
			return false;
		}
		return serviceSign.equals(sign);
	}


}
