package com.demo.util;

import org.springframework.util.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;
import java.util.Map;

/**
 * RSA 公钥与私钥的处理类
 */
public class RSAUtil {

    public static final String KEY_ALGORITHM = "RSA";

    private static final String PUBLIC_KEY = "RSAPublicKey";

    private static final String PRIVATE_KEY = "RSAPrivateKey";

    public static String getPublicKey(Map<String, Object> keyMap) throws Exception {

        Key key = (Key) keyMap.get(PUBLIC_KEY);

        byte[] publicKey = key.getEncoded();
        String publicKeyStr = encryptBASE64(publicKey);

        if(!StringUtils.isEmpty(publicKeyStr)){
            publicKeyStr = publicKeyStr.replaceAll("\r", "");
            publicKeyStr = publicKeyStr.replaceAll("\n", "");
        }

        return publicKeyStr;

    }

    public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {

        Key key = (Key) keyMap.get(PRIVATE_KEY);

        byte[] privateKey =key.getEncoded();

        String privateKeyStr = encryptBASE64(privateKey);
        if(!StringUtils.isEmpty(privateKeyStr)){
            privateKeyStr = privateKeyStr.replaceAll("\r", "");
            privateKeyStr = privateKeyStr.replaceAll("\n", "");
        }

        return privateKeyStr;

    }

    public static byte[] decryptBASE64(String key) throws Exception {

        return (new BASE64Decoder()).decodeBuffer(key);

    }



    public static String encryptBASE64(byte[] key) throws Exception {

        return (new BASE64Encoder()).encodeBuffer(key);

    }

    /**
     *
     * 生成对应的公钥私钥
     * @param password（加密种子 --/可以为空/---）
     * @return
     * @throws Exception
     */
    public static Map<String, Object> initKey(String password) throws Exception {

        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);

        keyPairGen.initialize(1024, new SecureRandom(password.getBytes()));

        KeyPair keyPair = keyPairGen.generateKeyPair();

        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

        Map<String, Object> keyMap = new HashMap<String, Object>(2);

        keyMap.put(PUBLIC_KEY, publicKey);

        keyMap.put(PRIVATE_KEY, privateKey);

        return keyMap;

    }


//    public static void main(String args[]) throws Exception{
//
//        Map<String, Object> keyMap;
//
//        try {
//
//            keyMap = initKey("test234");
//
//            String publicKey =  getPublicKey(keyMap);
//
//            System.out.println(publicKey);
//
//            String privateKey =  getPrivateKey(keyMap);
//
//            System.out.println(privateKey);
//
//        } catch (Exception e) {
//
//            e.printStackTrace();
//
//        }
//
//
//    }

}
