package cn.superdesk.uniorder.common.security.crypt.rsa;

import cn.superdesk.uniorder.common.security.Base64Utils;

import java.util.Map;

public class SignTools {

	private static String publicKey  = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCyzKhXkP76cx1PWwUz3n4G3JjFAeJGlZhiV/GEn1ZMGgWioy42GTjFBNq71jnQd8ycErP8uZUPaoleuCYGWSG0Rr9NIi3GNUsQQHpDkdH7IPwxMUH+1jKDGcV6fRW5knUTKw6vqYfBagbIUSNeST3efRdVWn0wLaF/Or0lYhmZBwIDAQAB";
	private static String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALLMqFeQ/vpzHU9bBTPefgbcmMUB4kaVmGJX8YSfVkwaBaKjLjYZOMUE2rvWOdB3zJwSs/y5lQ9qiV64JgZZIbRGv00iLcY1SxBAekOR0fsg/DExQf7WMoMZxXp9FbmSdRMrDq+ph8FqBshRI15JPd59F1VafTAtoX86vSViGZkHAgMBAAECgYAmcM24ldnb6qfgiccikbEytHa71F3bownyCspIIyGglu+jOcYTp3SHFzYXsEUZxsfcvkCoExYzDEqgbKLwNnWCOcLUbSWpuWDykTomJ5Q2jvGX+Sy7/IkGAquLkvJS0Agqdl9Q35hZZZi5W64MeCeyn/Svl7evJpfb9QKcCovsoQJBAPd6vSiMf8hs3sC6xRb8tnDw5H1K8slyXbB2ZlNpb8Blci9y0ACN708PcLItf5e6+XwdkAt1mjgkjv6QpIRY/f8CQQC49JRWCROR2P9+t7FSRfxauFwrP8+GNacbTLZdrJ6ptVICzsvyysgTu5Xlig2HdySsigD31Pzc1SgbWbmRjXT5AkEA7L9fS/Y0nCECa6+Asj/prFvWycG0R2CAHjr2It+fx5eEx713HMCraZ+kuN9ry1RULRb+4OeNNB5NKVxn9jBIMwJAEEioTrLZ4DtkszllCUNUJoCYzM3/bWeH4OVuIO0zwulN4SVn22ZzGxmm5uCytDyPJAm6cBd2c9Lohw/tQJ1dsQJBAKs6h3jhGSR6IwZp/tWMe96rQGh+d/Br6yzH11rQ6amht7d56dMSDnUSBiXJgVoA9lgYkFgvbgnKJwtfrYv+CwE=";

	public static String makeSign(String param) throws Exception {
		byte[] encodedData = RSAUtils.encryptByPrivateKey(param.getBytes(), privateKey);
		String sign        = RSAUtils.sign(encodedData, privateKey);
		return sign;
	}

	public static Boolean checkSign(String param, String sign) throws Exception {

		byte[]  encodedData = RSAUtils.encryptByPrivateKey(param.getBytes(), privateKey);
		boolean status      = RSAUtils.verify(encodedData, publicKey, sign);
		return status;
	}

	private static String publicKeypwd  = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALYEMGZfimJ+g4zXf+Fk1nkm9hw1ly+5EXFv/H2kCtbxXJu/rdao15Oaxx8w0IIzLguW04ntcx0njdGvs2N0TkkCAwEAAQ==";
	private static String privateKeypwd = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAtgQwZl+KYn6DjNd/4WTWeSb2HDWXL7kRcW/8faQK1vFcm7+t1qjXk5rHHzDQgjMuC5bTie1zHSeN0a+zY3ROSQIDAQABAkApqZHOjQXm/6Bq55Tc6bDx8EobMOUXRlxIw+Jee+MDSb3nph1STQWvE50GPLwx8+3GqXaUXGxSwxoz59hSdTd1AiEA/IxbwQ2hPeyvqwAuWDwAiMSdZwUvXa7p25FUqCazft8CIQC4gQviZv/aS2hv93Sy98eUw8maJ52qIvDMs+wurWBf1wIhAIVGKNGbOMEuG5I5OTvVBk8muw33vYRJ3x3A9xT5v3WvAiA3ykW2syCAeLbezHvMNiJyeHV4mKwTKzeXHny3bJOsxwIgfGl5WNrGD2xPdr9ibqevyJYQB1u4Ce7XnYJ2MhzwfRE=";

	public static void main(String[] args) throws Exception {
		//testSign();
		String param       = "aslkjflaksjdfkjj";
		byte[] encodedData = RSAUtils.encryptByPrivateKey(param.getBytes(), privateKeypwd);
		System.out.println("加密后:" + new String(encodedData, "utf-8"));
		System.out.println("加密后:" + Base64Utils.encode(encodedData).length());
		byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKeypwd);
		System.out.println("解密后:" + new String(decodedData));
//
//	        String sign = RSAUtils.sign(encodedData, privateKey);
//	        System.err.println("签名:" + sign);
//	 //       String sign ="SA+ixQKSdsGa86wT8YitHp0veyy+Kep44/Tjo7Yq2NjZDp5iKhhemKlrVZzzlu3BYRbdH0wIml0jFJL5USG0u+VGYJL40GGZ33V5o9lmGsuLcLxEKgibdCL68ZbrfKArbVo1h2gYVAeZvuZ9qYdhk4oeJndVNhFO9Hx8ncG6bDc=";
//	        boolean status = RSAUtils.verify(encodedData, publicKey, sign);
//	        System.err.println("签名验证结果:" + status);


	}
//	static String publicKey;
//    static String privateKey;

	static {
		try {

			Map<String, Object> keyMap = RSAUtils.genKeyPair();

			publicKey = RSAUtils.getPublicKey(keyMap);
			privateKey = RSAUtils.getPrivateKey(keyMap);

			System.err.println("公钥: \n\r" + publicKey);
			System.err.println("私钥: \n\r" + privateKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
//	static void testSign() throws Exception {
//        System.err.println("私钥加密——公钥解密");
//        String source = "<_>";
//        System.out.println("原文字:\r\n" + source);
//        byte[] data = source.getBytes();
//        byte[] encodedData = RSAUtils.encryptByPrivateKey(data, privateKey);
//        System.out.println("加密后:\r\n" + new String(encodedData));
//        byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKey);
//        String target = new String(decodedData);
//        System.out.println("解密后: \r\n" + target);
//        System.err.println("私钥签名——公钥验证签名");
//        String sign = RSAUtils.sign(encodedData, privateKey);
//        //String sign="HEoWgpDA7uXSnxL7+imQ50MMckxo++SURVW2dLbx903DD/Kps9ITwf8Zg8uuh5Z1b9E7+BFtidT6MvASm/mcQF6y3KVEEoiWouW6OfL77j16chOjD5qrmf0hB3MIMb7VbOHA8Os402icg6ykNWaXG7i/KKNQKHuejqT8zzTy6vU=";
//        System.err.println("签名:\r" + sign);
//        boolean status = RSAUtils.verify(encodedData, publicKey, sign);
//        System.err.println("验证结果:\r" + status);
//    }
}
