package com.uas.sso;

import java.util.Map;

import com.uas.sso.common.util.Base64Util;
//import org.junit.BeforeClass;
//import org.junit.Test;
import com.uas.sso.common.encrypt.RSA;

public class RsaTest {

	static String publicKey;

	static String privateKey;

	// @BeforeClass
	public static void generate() {
		try {
			Map<String, Object> keyMap = RSA.genKeyPair();
			publicKey = RSA.getPublicKey(keyMap);
			privateKey = RSA.getPrivateKey(keyMap);
			System.out.println("公钥: \n\r" + publicKey);
			System.out.println("私钥： \n\r" + privateKey);

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

	// @Test
	public void test() throws Exception {
		System.out.println("公钥加密——私钥解密");
		String source = "静夜思-床前明月光，疑是地上霜。举头望明月，低头思故乡。";
		System.out.println("\r加密前文字：\r\n" + source);
		byte[] data = source.getBytes();
		byte[] encodedData = RSA.encryptByPublicKey(data, publicKey);
		System.out.println("加密后文字：\r\n" + new String(encodedData));
		System.out.println("bs::" + Base64Util.encode(encodedData));
		byte[] decodedData = RSA.decryptByPrivateKey(encodedData, privateKey);
		String target = new String(decodedData);
		System.out.println("解密后文字: \r\n" + target);
	}

	// @Test
	public void testSign() throws Exception {
		System.out.println("私钥加密——公钥解密");
		String source = "这是一行测试RSA数字签名的无意义文字";
		System.out.println("原文字：\r\n" + source);
		byte[] data = source.getBytes();
		byte[] encodedData = RSA.encryptByPrivateKey(data, privateKey);
		System.out.println("加密后：\r\n" + new String(encodedData));
		byte[] decodedData = RSA.decryptByPublicKey(encodedData, publicKey);
		String target = new String(decodedData);
		System.out.println("解密后: \r\n" + target);
		System.out.println("私钥签名——公钥验证签名");
		String sign = RSA.sign(encodedData, privateKey);
		System.out.println("签名:\r" + sign);
		boolean status = RSA.verify(encodedData, publicKey, sign);
		System.out.println("验证结果:" + status);
	}

}
