package cn.unitid.matrix.pkiservice.test.kms;

import cn.unitid.matrix.pkiservice.sdk.ApiException;
import cn.unitid.matrix.pkiservice.sdk.PkiClient;
import cn.unitid.matrix.pkiservice.sdk.PkiOkhttpClient;
import cn.unitid.matrix.pkiservice.sdk.kms.request.CertImportRequest;
import cn.unitid.matrix.pkiservice.sdk.kms.response.CertImportResponse;

import java.util.HashSet;
import java.util.Set;

/**
 * 导入证书
 */
public class CertImporTest {

    public static void main(String[] args) {
        // 提供的URL
        String url = "http://localhost:9200";

        // 您的appKey
        String appKey = "CLxUIrYIO0";

        // 您的appSecret
        String appSecret = "Rsl7kjMrqHrxhDk9uou23IYRnCVjRKJC";

        // 1.原客户端
        PkiClient pkiClient = new PkiOkhttpClient(url, appKey, appSecret);

        CertImportRequest request = new CertImportRequest();

        //证书请求csrId
        request.setCsrId("SM2");

        //签名证书cer或pem文件
        request.setSignCert("MIICPDCCAeOgAwIBAgINAPqtsPTS5HURANTFszAKBggqgRzPVQGDdTBJMQswCQYDVQQGEwJDTjESMBAGA1UECAwJ5rGf6IuP55yBMRIwEAYDVQQHDAnljZfkuqzluIIxEjAQBgNVBAMMCXRlc3RTTTJDQTAeFw0yMjEwMjAxMTEzNDBaFw0yMzEwMjAxMTEzNDBaME0xCzAJBgNVBAYTAkNOMTAwLgYDVQQtDCcyMWQyMWNiZDRkZWU0ZTBmYTExYjJhZmRhYzk2MDAwZTIwMjIxMDcxDDAKBgNVBAMMA2xoeTBZMBMGByqGSM49AgEGCCqBHM9VAYItA0IABEapdO5TpFC8HU4vOtrmsnaAfEl2BQpON06RWknxtal06svRpJews3xGnaP75ukpMQxE9VxVtRcpCsDDaRxqQhGjgaswgagwCQYDVR0TBAIwADAdBgNVHQ4EFgQUhXnqr5tI0CT2zc9p/+dh32k/1VIwSwYDVR0lAQH/BEEwPwYIKwYBBQUHAwEGCCsGAQUFBwMCBggrBgEFBQcDBAYIKwYBBQUHAwgGCisGAQQBgjcKAwMGCWCGSAGG+EIEATAOBgNVHQ8BAf8EBAMCBsAwHwYDVR0jBBgwFoAUE3GRQ7iygyrxFixllDZ6SqWNJicwCgYIKoEcz1UBg3UDRwAwRAIgTha13q9hlIKk4qsX4t7aKiP4oPkflyHUOhMXocmPlH8CIHjj+wU2CYEasXDaTe436Eho/vLv4vT2jpnmMW1DO0gx");

        //加密证书cer或pem文件
        request.setEncCert("MIICPDCCAeOgAwIBAgINAPOSWUEhi2SJYD6RwDAKBggqgRzPVQGDdTBJMQswCQYDVQQGEwJDTjESMBAGA1UECAwJ5rGf6IuP55yBMRIwEAYDVQQHDAnljZfkuqzluIIxEjAQBgNVBAMMCXRlc3RTTTJDQTAeFw0yMjEwMjAxMTEzNDBaFw0yMzEwMjAxMTEzNDBaME0xCzAJBgNVBAYTAkNOMTAwLgYDVQQtDCcyMWQyMWNiZDRkZWU0ZTBmYTExYjJhZmRhYzk2MDAwZTIwMjIxMDcxDDAKBgNVBAMMA2xoeTBZMBMGByqGSM49AgEGCCqBHM9VAYItA0IABCdnuQPWilQZkt3g+YBjFEJq9KaRz1b+8o8jxL+N6dxVvL658fWMooarFl6QLvvjcO6bDRMEeZNMhubXYD5rbCijgaswgagwCQYDVR0TBAIwADAdBgNVHQ4EFgQUsGGpSH5V0k8n5c2PAxR5//KevAgwSwYDVR0lAQH/BEEwPwYIKwYBBQUHAwEGCCsGAQUFBwMCBggrBgEFBQcDBAYIKwYBBQUHAwgGCisGAQQBgjcKAwMGCWCGSAGG+EIEATAOBgNVHQ8BAf8EBAMCAzgwHwYDVR0jBBgwFoAUE3GRQ7iygyrxFixllDZ6SqWNJicwCgYIKoEcz1UBg3UDRwAwRAIfBHU+jqgREq6AOKWhVOl3IHrMvcemRyEdnzSiydTiIgIhAMX3VgreEM9M4akeIWSrOO9k60aapZigQNi3vDaSTMAt");

        //加密证书私钥key
        request.setPrivateKey("bs4PSVKe0c8SD4US/14/ZEPuHnG2h4u6z7nfUUsVQAs=");

        //私钥加密格式 0-未加密 1-Sm2Cipher 2-Sm4Cipher
        request.setEnc(0);

        //密钥主人
        request.setManagerId("123456");

        //密钥主人名称
        request.setManagerName("test");

        Set<String> set = new HashSet<>();
        set.add("BKH4KjNJRpHO3mL7tk9/+v0O+QJZJb0xlOxlODHICPF+s9DHESicyAVqQbpYZRS+2MTf1Pru+cYC2psQrxetxAo=");
        //密钥主人公钥列表
        request.setAuthPublicKeys(set);

        try {
            CertImportResponse response = pkiClient.execute(request);
            // 后续业务
            if (response.isSuccess()) {
                System.out.println("成功响应：" + response.getResult());
            } else {
                System.out.println("失败响应：" + response.getBody());
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }

    }
}
