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.KeyPairGenerateRequest;
import cn.unitid.matrix.pkiservice.sdk.ces.response.KeyPairGenerateResponse;
import cn.unitid.matrix.pkiservice.sdk.consts.Algorithm;
import cn.unitid.matrix.pkiservice.sdk.consts.CryptogramEngineType;

/**
 * 申请密钥对
 */
public class KeyPairGenerateTest {

    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);

        KeyPairGenerateRequest request = new KeyPairGenerateRequest();

        // 密钥长度
        request.setKeySize(256);

        //文件加密引擎
        request.setCryptogramEngineType(CryptogramEngineType.FILE);

        //密钥算法 SM2
        request.setAlgorithm(Algorithm.SM2);

//        // 私钥base64 不传则自动生成
//        request.setPrivateKey("");
//        // 公钥base64 不传则自动生成
//        request.setPublicKey("");

        try {
            KeyPairGenerateResponse response = pkiClient.execute(request);
            // 后续业务
            if (response.isSuccess()) {
//                System.out.println("成功：" + response.getResult());
                KeyPairGenerateResponse.Result result = response.getResult();
                System.out.println("虚拟索引: " + result.getVirtualIndex());
                System.out.println("签名公钥: " + result.getSignPublicKey());

            } else {
                System.out.println("失败：" + response.getBody());
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }

    }
}
