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

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.ces.request.P1SignVerifyRequest;
import cn.unitid.matrix.pkiservice.sdk.ces.response.P1SignVerifyResponse;
import cn.unitid.matrix.pkiservice.sdk.consts.DataType;
import cn.unitid.matrix.pkiservice.sdk.consts.SignatureAlgorithm;

/**
 * p1签名验证
 */
public class P1SignVerifyTest {

    public static void main(String[] args) {

        // 提供的URL
        String url = "http://172.16.3.232:25115";

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

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

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

        P1SignVerifyRequest request = new P1SignVerifyRequest();

        Integer virtualIndex = 20010;

        String signature = "MEYCIQDJzuY4v5PZ0EBe93XL90ruMHf90EXHpXdGza0JeR2vYwIhAJdM7q/1zXGCyyY0wDIU/ra+w4iFIiEZlODxy4LmKVBK";

        //String digst = "/GJzLVxZk4ymyfxnBkdQuJ733rNwQMY034coNSDbIqs=";

        // 虚拟索引
        request.setVirtualIndex(virtualIndex);

        // 签名数据 base64
        request.setSignature(signature);

        // 摘要数据
        //request.setDigst(digst);

        // 原文数据
        request.setData("MTIz");

        // 原文数据格式
        request.setDataType(DataType.BASE64);

        // 摘要算法 SM2签名的摘要算法必须为SM3
        request.setSignatureAlgorithm(SignatureAlgorithm.SM3withSM2);

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

    }
}
