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

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.consts.CertSearchEnum;
import cn.unitid.matrix.pkiservice.sdk.consts.DataType;
import cn.unitid.matrix.pkiservice.sdk.consts.SignatureAlgorithm;
import cn.unitid.matrix.pkiservice.sdk.svs.request.P1VerifyRequest;
import cn.unitid.matrix.pkiservice.sdk.svs.response.P1VerifyResponse;

/**
 * 作者： 徐霁
 * <p>
 * 时间： 2021/5/24 10:18
 * <p>
 * 描述信息：  P1签名验证测试
 */
public class P1VerifyTest {

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

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

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

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


        String certId = "6392958eb7f1a56b0535245f";

        String data = "MC8wCwYJYIZIAWUDBAIBBCAB13RlckLBHIdeiN20353jSVYH1eEXXJfd8/upNDEEtg==";

        String signature = "MEUCIH2eIxHO3UVWDkY8Du3h+ptUx387aOgtpIOlu1ET3jxiAiEAhLUL+ofXwHHUVXEix+kcKSBfCGFyi42EVMlmJcXdrWU=";

        P1VerifyRequest request = new P1VerifyRequest();

        //通过何种方式进行签名 CERT_ID：证书ID，KEY_INDEX：密钥索引， SN_HEX：证书序列号(十六进制格式)，SN_DEC：证书序列号(十进制格式)，TN_HASH：证书证书指纹，CERT_CN：证书CN ，CERT_DN：证书主题项(DN)
        request.setSignType(CertSearchEnum.CERT_ID);
        //签名项目的值
        request.setSignItemValue(certId);
        //签名项目的值
        request.setDataType(DataType.ASN1_BASE64);
        // 签名数据
        request.setData(data);
        // 签名值
        request.setSignature(signature);
        // 摘要算法，SM2证书签名固定为SM3，RSA默认SHA256
//        request.setSignatureAlgorithm(SignatureAlgorithm.SHA1withRSA);
        // 是否返回证书
        request.setReturnCert(true);
        try {
            P1VerifyResponse response = pkiClient.execute(request);
            // 后续业务
            if (response.isSuccess()) {
                System.out.println("成功响应：" + response.getBody());
            } else {
                System.out.println("失败响应：" + response.getBody());
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }

    }


//    @Test
//    public void verifyP1(){
//        String certBase64 = "MIIBuTCCAWCgAwIBAgINAMl5+kv9IFg8cQ1W/TAKBggqgRzPVQGDdTAvMQswCQYDVQQGEwJDTjER" +
//                "MA8GA1UEChMIQUJDIGx0ZC4xDTALBgNVBAMTBGhvc3QwIhgPMjAxOTA2MDIxNjAwMDBaGA8yMDI5" +
//                "MDUzMDE2MDAwMFowLzELMAkGA1UEBhMCQ04xETAPBgNVBAoTCEFCQyBsdGQuMQ0wCwYDVQQDEwRo" +
//                "b3N0MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEmFoJJqqUQ6ZbPKdxZ3rmt/+zU/qX79xqgcqb" +
//                "y/r8zH8cvETA7jjd8jvZT+8jqHYeaNcYsdbOzLbuwfGW+a0YRqNdMFswDAYDVR0TBAUwAwEB/zAd" +
//                "BgNVHQ4EFgQUCphciCnwBiCnxvsMhwUk8ZyqjskwHwYDVR0jBBgwFoAUCphciCnwBiCnxvsMhwUk" +
//                "8ZyqjskwCwYDVR0PBAQDAgGGMAoGCCqBHM9VAYN1A0cAMEQCICHL2LYAc/6HleUIdozIZh9z+pqV" +
//                "0ib4g2I9mZ9s47utAiAib3DcVPCAwEm9Lq5yhA9yTYmr3v/xsjFKnPDOOQ6PZw==";
//
//        String original = "1e02c332bebd46bf99788af76babfb16:1637839027891";
//        String signature = "MEQCIAqOql70omZW2Xf6bAg9P2wYsqax8gWWrbir88U8msJdAiB7gz6SClLzS4OoBxcbgZJGAV2i+/vrnnB9WF9N8RocJw==";
//
//        byte[] signatureByte = Base64.decodeBase64(signature);
//
//        X509Certificate x509Certificate = PKIUtil.base64ToX509(certBase64);
//        PublicKey publicKey = x509Certificate.getPublicKey();
//        ECPublicKey ecPublicKey = (ECPublicKey) publicKey;
//        try {
//            SM2Signature verifier = new SM2Signature();
//            verifier.initVerify(ecPublicKey);
//            verifier.update(original.getBytes());
//
//            boolean result = verifier.verify(signatureByte);
//            System.out.println(result);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }

}
