package com.platform.yijia.utils;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.*;


/**
 * RSA签名验签类
 */
public class RSASignature {

    /**
     * 签名算法
     */
    public static final String SIGN_ALGORITHMS = "SHA1WithRSA";
    
	/**
	 * 针对参数进行排序拼装
	 * @param
	 * @return
	 */
	public static String getOrderContent(Map<String, Object> requestParam) {
		Map<String, Object> sortedParams = new TreeMap<String, Object>();
		if ((requestParam != null) && (requestParam.size() > 0)) {
			sortedParams.putAll(requestParam);
		}
		StringBuffer content = new StringBuffer();
		List<String> keys = new ArrayList<String>(sortedParams.keySet());
		Collections.sort(keys);
		int index = 0;
		for (int i = 0; i < keys.size(); i++) {
			String key = keys.get(i);
			Object value = sortedParams.get(key);
			if (key!=null &&!"".equals(key)&& value != null) {
				content.append((index == 0 ? "" : "&") + key + "=" + value);
				index++;
			}
		}
		return content.toString();
	}

    /**
     * rsa 签名
     * @param content 待签名内容
     * @param privateKey  私钥
     * @return
     */
    public static byte[] sign(String content, String privateKey) {
        try {
            PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(decryptBASE64(privateKey));
            KeyFactory keyf = KeyFactory.getInstance("RSA");
            PrivateKey priKey = keyf.generatePrivate(priPKCS8);
            java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);
            signature.initSign(priKey);
            signature.update(content.getBytes("UTF-8"));
            byte[] signed = signature.sign();
            return signed;
        } catch (Exception e) {
        	e.printStackTrace();
            return null;
        }
    }

    /**
     * RSA验签名检查
     * @param content 待签名数据
     * @param sign 签名值
     * @param publicKey 分配给开发商公钥
     * @return 布尔值
     */
    public static boolean doCheck(String content, String sign, String publicKey) {
        try {
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            byte[] encodedKey = decryptBASE64(publicKey);
            PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));


            java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);

            signature.initVerify(pubKey);
            signature.update(content.getBytes("UTF-8"));

            boolean bverify = signature.verify(decryptBASE64(sign));
            return bverify;

        } catch (Exception e) {
        	e.printStackTrace();
            return false;
        }
    }

    public static String encryptBASE64(byte[] key) throws Exception {
        System.out.println("key:"+key);
        String base64encodedString = Base64.getEncoder().encodeToString(key);
        return base64encodedString.replaceAll("[\\s*\t\n\r]", "");
    }
    public static byte[] decryptBASE64(String key) throws Exception {
        byte[] base64decodedBytes = Base64.getDecoder().decode(key.replaceAll("[\\s*\t\n\r]", ""));
        return base64decodedBytes;
    }

}
