This commit is contained in:
liulu
2024-12-13 14:32:58 +08:00
parent f2720fdb83
commit 8f997d2a00
14 changed files with 230 additions and 132 deletions
@@ -11,12 +11,13 @@ import com.sunyard.chsm.sdf.model.EccCipher;
import com.sunyard.chsm.sdf.model.EccKey;
import com.sunyard.chsm.sdf.model.EccPriKey;
import com.sunyard.chsm.sdf.model.EccPubKey;
import com.sunyard.chsm.sdf.model.EccSignature;
import com.sunyard.chsm.utils.CodecUtils;
import com.sunyard.chsm.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.net.URL;
@@ -28,36 +29,39 @@ import java.util.Optional;
* @since 2024/12/12
*/
@Slf4j
class SdfApiTest {
class SdfApiAdapterTest {
private final byte[] symKey = "nhkdhaksd4678787".getBytes();
private final byte[] iv = "hjdashde83252i23".getBytes();
private final String plain = "hello sdf api ,hello sdf api !&!";
private final String ip1 = "172.16.18.41";
private final int port = 8889;
private final static byte[] symKey = "nhkdhaksd4678787".getBytes();
private final static byte[] iv = "hjdashde83252i23".getBytes();
private final static String plain = "hello sdf api ,hello sdf api !&!";
private final static String ip1 = "172.16.18.41";
private final static int port = 8889;
private static final SdfApiAdapter bcAdapter = SdfApiAdapterFactory.getBcAdapter();
private RpcSdfAdapter rpcSdfAdapter;
private String hd;
private String hs;
private EccPubKey pubKey;
private EccPriKey priKey;
private SdfApiAdapter bcAdapter = SdfApiAdapterFactory.getBcAdapter();
private static RpcSdfAdapter rpcSdfAdapter;
private static String hd;
private static String hs;
private static EccPubKey pubKey;
private static EccPriKey priKey;
@BeforeEach
public void before() throws Exception {
@BeforeAll
public static void before() throws Exception {
JsonRpcHttpClient client = new JsonRpcHttpClient(JsonUtils.objectMapper(),
new URL("http://172.16.18.46:9989/sdf/adapter"), Collections.emptyMap());
rpcSdfAdapter = ProxyUtil.createClientProxy(
getClass().getClassLoader(), RpcSdfAdapter.class, client);
this.hd = rpcSdfAdapter.openDevice(ip1, port, 3000, 3000, 0);
this.hs = rpcSdfAdapter.openSession(hd);
SdfApiAdapterTest.class.getClassLoader(), RpcSdfAdapter.class, client);
hd = rpcSdfAdapter.openDevice(ip1, port, 3000, 3000, 0);
hs = rpcSdfAdapter.openSession(hd);
EccKey eccKey = rpcSdfAdapter.generateKeyPairECC(hs, AlgId.SGD_SM2_3);
pubKey = EccPubKey.fromBytes(eccKey.getPubKey());
priKey = EccPriKey.fromBytes(eccKey.getPriKey());
pubKey = com.sunyard.chsm.sdf.model.EccPubKey.fromBytes(eccKey.getPubKey());
priKey = com.sunyard.chsm.sdf.model.EccPriKey.fromBytes(eccKey.getPriKey());
log.info("public key: {}", pubKey.getPubKeyHex());
log.info("private key: {}", CodecUtils.encodeHex(priKey.getD()));
}
@AfterEach
public void after() {
@AfterAll
public static void after() {
Optional.ofNullable(hs).ifPresent(rpcSdfAdapter::closeSession);
Optional.ofNullable(hd).ifPresent(rpcSdfAdapter::closeDevice);
}
@@ -114,6 +118,21 @@ class SdfApiTest {
Assertions.assertEquals(plain, new String(sm2Plain));
}
@Test
public void testSM2SignAndVerify() {
EccSignature bcSign = bcAdapter.externalSignECC("", priKey, plain.getBytes());
log.info("bc sm2 signature: {}", bcSign.getRawSignHex());
boolean sdfVerified = rpcSdfAdapter.externalVerifyECC(hs, pubKey, plain.getBytes(), bcSign);
log.info("sdf sm2 verified: {}", sdfVerified);
Assertions.assertTrue(sdfVerified);
EccSignature sdfSign = rpcSdfAdapter.externalSignECC(hs, priKey, plain.getBytes());
log.info("sdf sm2 signature: {}", sdfSign.getRawSignHex());
boolean bcVerified = bcAdapter.externalVerifyECC("", pubKey, plain.getBytes(), sdfSign);
log.info("bc sm2 verified: {}", bcVerified);
Assertions.assertTrue(bcVerified);
}
@Test
public void testHash() {
String newSession = rpcSdfAdapter.openSession(hd);
@@ -147,5 +166,11 @@ class SdfApiTest {
Assertions.assertArrayEquals(sdfMac, bcMac);
}
@Test
public void testRandom() {
byte[] r = rpcSdfAdapter.generateRandom(hs, 64);
Assertions.assertEquals(64, r.length);
}
}
@@ -1,11 +0,0 @@
package com.sunyard.chsm.sdf.adapter;
/**
* @author liulu
* @since 2024/12/12
*/
public interface RpcSdfAdapter extends SdfApiAdapter {
String openDevice(String ip, int port, int connTimeout, int dealTimeout, int ipMode);
}