package org.fouram.core.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.fouram.core.base.model.PageData;

/**
 * API解密
 */
public class APIDecryptUtil {
	
	private static final String SPLIT = "!!!";
	private static final String XIAN_KEY_01 = "XIAN01"; // 会员系统api key=MD5

	/**
	 * 解密
	 * @param encodeToken
	 * @param encodeParam
	 * @return
	 */
	public static PageData decrypt(String encodeToken, String encodeParam){
		PageData pd = new PageData();
		try {
			String[] dataStr = encodeToken.split(SPLIT);
			String skey = new String(RSAOperator.decryptPriKey(Base64.decodeBase64(dataStr[1])));
			String resAappKey = AESOperator.getInstance().decrypt(dataStr[0], skey);
			String appKey = new String(RSAOperator.decryptPubKey(Base64.decodeBase64(resAappKey)));
			if(appKey.equals(new Md5Hash(XIAN_KEY_01).toString())){
				pd.put("result", true);
				if(StringUtils.isNotBlank(encodeParam)){
					pd.put("param", AESOperator.getInstance().decrypt(encodeParam, skey));
				}
			}else{
				pd.put("result", false);
			}
		} catch (Exception e) {
			pd.put("result", false);
		}
		return pd;
	}
}