/*
 *@ClassName   PemUtil
 *@Description create class
 *@Author      kong
 *@Date        2021/6/1 上午9:27
 *@Version     1.0
 */
package com.bootdo.datas.tools.gm;

import java.io.*;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemReader;
import org.bouncycastle.util.io.pem.PemWriter;

public class PemUtil extends GMBaseUtil {
    public static String getPemPublicKey(PublicKey publicKey) throws IOException {
        if (publicKey == null)
            return "";
        PemObject pemObject = new PemObject("PUBLIC KEY", publicKey.getEncoded());
        StringWriter stringWriter = new StringWriter();
        PemWriter pemWriter = new PemWriter(stringWriter);
        pemWriter.writeObject(pemObject);
        pemWriter.close();
        String pemString = stringWriter.toString();
        return pemString;
    }

    public static String getPemPrivateKey(PrivateKey privateKey) throws IOException {
        if (privateKey == null)
            return "";
        PemObject pemObject = new PemObject("PRIVATE KEY", privateKey.getEncoded());
        StringWriter stringWriter = new StringWriter();
        PemWriter pemWriter = new PemWriter(stringWriter);
        pemWriter.writeObject(pemObject);
        pemWriter.close();
        String pemString = stringWriter.toString();
        return pemString;
    }

    public static PublicKey getPublicKey(String pubStr, String algorithm) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
        if (pubStr == null || "".equals(pubStr))
            return null;
        if (algorithm == null || "".equals(algorithm))
            return null;
        Reader reader = new InputStreamReader(new ByteArrayInputStream(pubStr.getBytes()));
        PemReader pemReader = new PemReader(reader);
        PemObject pemObject = pemReader.readPemObject();
        X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pemObject.getContent());
        KeyFactory factory = KeyFactory.getInstance(algorithm);
        PublicKey publicKey = factory.generatePublic(pubKeySpec);
        return publicKey;
    }

    public static PrivateKey getKeyPair(String priStr, String algorithm) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
        if (priStr == null || "".equals(priStr))
            return null;
        if (algorithm == null || "".equals(algorithm))
            return null;
        Reader reader = new InputStreamReader(new ByteArrayInputStream(priStr.getBytes()));
        PemReader pemReader = new PemReader(reader);
        PemObject pemObject = pemReader.readPemObject();
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pemObject.getContent());
        KeyFactory factory = KeyFactory.getInstance(algorithm);
        PrivateKey privateKey = factory.generatePrivate(keySpec);
        return privateKey;
    }

    public static KeyPair getKeyPair(String priKeyPath) {
        try {
            File priKeyFile = new File(priKeyPath);
            PEMParser pemParser = new PEMParser(new FileReader(priKeyFile));
            Object object = pemParser.readObject();
            PEMKeyPair pemKeyPair = (PEMKeyPair) pemParser.readObject();
            JcaPEMKeyConverter jcaPEMKeyConverter = new JcaPEMKeyConverter();
            jcaPEMKeyConverter.setProvider(new BouncyCastleProvider());
            return jcaPEMKeyConverter.getKeyPair(pemKeyPair);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
