sdf api test
This commit is contained in:
parent
0a8c49f533
commit
1a6f527fbd
@ -58,7 +58,7 @@ public class SdfApiAdapterFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SdfApiAdapter getProxyRcpAdapter(String ip, Integer port) {
|
private static SdfApiAdapter getProxyRcpAdapter(String ip, Integer port) {
|
||||||
return (SdfApiAdapter) Proxy.newProxyInstance(ClassUtils.getDefaultClassLoader(),
|
return (SdfApiAdapter) Proxy.newProxyInstance(ClassUtils.getDefaultClassLoader(),
|
||||||
new Class[]{SdfApiAdapter.class},
|
new Class[]{SdfApiAdapter.class},
|
||||||
(proxy, method, args) -> {
|
(proxy, method, args) -> {
|
||||||
|
@ -121,6 +121,7 @@ public abstract class DeviceDTO {
|
|||||||
* 管理端口
|
* 管理端口
|
||||||
*/
|
*/
|
||||||
private Integer managePort;
|
private Integer managePort;
|
||||||
|
private Integer encKeyIdx;
|
||||||
/**
|
/**
|
||||||
* 访问凭证
|
* 访问凭证
|
||||||
*/
|
*/
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package sdf;
|
package sdf;
|
||||||
|
|
||||||
|
import com.sunyard.chsm.enums.ManufacturerModelEnum;
|
||||||
import com.sunyard.chsm.sdf.SdfApiService;
|
import com.sunyard.chsm.sdf.SdfApiService;
|
||||||
import com.sunyard.chsm.sdf.SingleSdfApiService;
|
import com.sunyard.chsm.sdf.SingleSdfApiService;
|
||||||
import com.sunyard.chsm.sdf.adapter.SdfApiAdapterFactory;
|
import com.sunyard.chsm.sdf.adapter.SdfApiAdapterFactory;
|
||||||
@ -30,7 +31,7 @@ public class SdfApiServiceTest {
|
|||||||
private final static String ip1 = "172.16.18.41";
|
private final static String ip1 = "172.16.18.41";
|
||||||
private final static int port = 8889;
|
private final static int port = 8889;
|
||||||
private static final SdfApiService bcService = new SingleSdfApiService(SdfApiAdapterFactory.getBcAdapter());
|
private static final SdfApiService bcService = new SingleSdfApiService(SdfApiAdapterFactory.getBcAdapter());
|
||||||
private static final SdfApiService sdfService = new SingleSdfApiService(SdfApiAdapterFactory.getProxyRcpAdapter(ip1, port));
|
private static final SdfApiService sdfService = new SingleSdfApiService(SdfApiAdapterFactory.newInstance(ManufacturerModelEnum.enc001.getModel(), ip1, port));
|
||||||
|
|
||||||
private static EccPubKey pubKey;
|
private static EccPubKey pubKey;
|
||||||
private static EccPriKey priKey;
|
private static EccPriKey priKey;
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package com.sunyard.chsm.sdf;
|
package com.sunyard.chsm.sdf;
|
||||||
|
|
||||||
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
|
import com.sunyard.chsm.enums.ManufacturerModelEnum;
|
||||||
import com.googlecode.jsonrpc4j.ProxyUtil;
|
|
||||||
import com.sunyard.chsm.sdf.adapter.RpcSdfAdapter;
|
|
||||||
import com.sunyard.chsm.sdf.adapter.SdfApiAdapter;
|
import com.sunyard.chsm.sdf.adapter.SdfApiAdapter;
|
||||||
import com.sunyard.chsm.sdf.adapter.SdfApiAdapterFactory;
|
import com.sunyard.chsm.sdf.adapter.SdfApiAdapterFactory;
|
||||||
import com.sunyard.chsm.sdf.context.AlgId;
|
import com.sunyard.chsm.sdf.context.AlgId;
|
||||||
@ -13,15 +11,12 @@ import com.sunyard.chsm.sdf.model.EccPriKey;
|
|||||||
import com.sunyard.chsm.sdf.model.EccPubKey;
|
import com.sunyard.chsm.sdf.model.EccPubKey;
|
||||||
import com.sunyard.chsm.sdf.model.EccSignature;
|
import com.sunyard.chsm.sdf.model.EccSignature;
|
||||||
import com.sunyard.chsm.utils.CodecUtils;
|
import com.sunyard.chsm.utils.CodecUtils;
|
||||||
import com.sunyard.chsm.utils.JsonUtils;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.jupiter.api.AfterAll;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,8 +32,8 @@ class SdfApiAdapterTest {
|
|||||||
private final static String ip1 = "172.16.18.41";
|
private final static String ip1 = "172.16.18.41";
|
||||||
private final static int port = 8889;
|
private final static int port = 8889;
|
||||||
private static final SdfApiAdapter bcAdapter = SdfApiAdapterFactory.getBcAdapter();
|
private static final SdfApiAdapter bcAdapter = SdfApiAdapterFactory.getBcAdapter();
|
||||||
|
private static final SdfApiAdapter sdfAdapter = SdfApiAdapterFactory.newInstance(ManufacturerModelEnum.enc001.getModel(), ip1, port);
|
||||||
|
|
||||||
private static RpcSdfAdapter rpcSdfAdapter;
|
|
||||||
private static String hd;
|
private static String hd;
|
||||||
private static String hs;
|
private static String hs;
|
||||||
private static EccPubKey pubKey;
|
private static EccPubKey pubKey;
|
||||||
@ -47,13 +42,9 @@ class SdfApiAdapterTest {
|
|||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void before() throws Exception {
|
public static void before() throws Exception {
|
||||||
JsonRpcHttpClient client = new JsonRpcHttpClient(JsonUtils.objectMapper(),
|
hd = sdfAdapter.openDevice();
|
||||||
new URL("http://172.16.18.46:9989/sdf/adapter"), Collections.emptyMap());
|
hs = sdfAdapter.openSession(hd);
|
||||||
rpcSdfAdapter = ProxyUtil.createClientProxy(
|
EccKey eccKey = sdfAdapter.generateKeyPairECC(hs, AlgId.SGD_SM2_3);
|
||||||
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 = com.sunyard.chsm.sdf.model.EccPubKey.fromBytes(eccKey.getPubKey());
|
pubKey = com.sunyard.chsm.sdf.model.EccPubKey.fromBytes(eccKey.getPubKey());
|
||||||
priKey = com.sunyard.chsm.sdf.model.EccPriKey.fromBytes(eccKey.getPriKey());
|
priKey = com.sunyard.chsm.sdf.model.EccPriKey.fromBytes(eccKey.getPriKey());
|
||||||
log.info("public key: {}", pubKey.getPubKeyHex());
|
log.info("public key: {}", pubKey.getPubKeyHex());
|
||||||
@ -62,30 +53,30 @@ class SdfApiAdapterTest {
|
|||||||
|
|
||||||
@AfterAll
|
@AfterAll
|
||||||
public static void after() {
|
public static void after() {
|
||||||
Optional.ofNullable(hs).ifPresent(rpcSdfAdapter::closeSession);
|
Optional.ofNullable(hs).ifPresent(sdfAdapter::closeSession);
|
||||||
Optional.ofNullable(hd).ifPresent(rpcSdfAdapter::closeDevice);
|
Optional.ofNullable(hd).ifPresent(sdfAdapter::closeDevice);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getDeviceInfo() {
|
public void getDeviceInfo() {
|
||||||
DeviceInfo deviceInfo = rpcSdfAdapter.getDeviceInfo(hs);
|
DeviceInfo deviceInfo = sdfAdapter.getDeviceInfo(hs);
|
||||||
Assertions.assertNotNull(deviceInfo);
|
Assertions.assertNotNull(deviceInfo);
|
||||||
log.info("DeviceInfo: {}", deviceInfo);
|
log.info("DeviceInfo: {}", deviceInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSymEncAndDec() {
|
public void testSymEncAndDec() {
|
||||||
String hk = rpcSdfAdapter.importKey(hs, symKey);
|
String hk = sdfAdapter.importKey(hs, symKey);
|
||||||
byte[] ecbCipher = rpcSdfAdapter.symEncrypt(hs, hk, AlgId.SGD_SM4_ECB, null, plain.getBytes());
|
byte[] ecbCipher = sdfAdapter.symEncrypt(hs, hk, AlgId.SGD_SM4_ECB, null, plain.getBytes());
|
||||||
byte[] ecbPlain = rpcSdfAdapter.symDecrypt(hs, hk, AlgId.SGD_SM4_ECB, null, ecbCipher);
|
byte[] ecbPlain = sdfAdapter.symDecrypt(hs, hk, AlgId.SGD_SM4_ECB, null, ecbCipher);
|
||||||
log.info("ecb_cipher: {}", CodecUtils.encodeHex(ecbCipher));
|
log.info("ecb_cipher: {}", CodecUtils.encodeHex(ecbCipher));
|
||||||
Assertions.assertEquals(plain, new String(ecbPlain));
|
Assertions.assertEquals(plain, new String(ecbPlain));
|
||||||
|
|
||||||
byte[] cbcCipher = rpcSdfAdapter.symEncrypt(hs, hk, AlgId.SGD_SM4_CBC, iv, plain.getBytes());
|
byte[] cbcCipher = sdfAdapter.symEncrypt(hs, hk, AlgId.SGD_SM4_CBC, iv, plain.getBytes());
|
||||||
log.info("cbc_cipher: {}", CodecUtils.encodeHex(cbcCipher));
|
log.info("cbc_cipher: {}", CodecUtils.encodeHex(cbcCipher));
|
||||||
byte[] cbcPlain = rpcSdfAdapter.symDecrypt(hs, hk, AlgId.SGD_SM4_CBC, iv, cbcCipher);
|
byte[] cbcPlain = sdfAdapter.symDecrypt(hs, hk, AlgId.SGD_SM4_CBC, iv, cbcCipher);
|
||||||
Assertions.assertEquals(plain, new String(cbcPlain));
|
Assertions.assertEquals(plain, new String(cbcPlain));
|
||||||
rpcSdfAdapter.destroyKey(hs, hk);
|
sdfAdapter.destroyKey(hs, hk);
|
||||||
|
|
||||||
Assertions.assertArrayEquals(ecbPlain, cbcPlain);
|
Assertions.assertArrayEquals(ecbPlain, cbcPlain);
|
||||||
Assertions.assertNotEquals(CodecUtils.encodeHex(ecbCipher), CodecUtils.encodeHex(cbcCipher));
|
Assertions.assertNotEquals(CodecUtils.encodeHex(ecbCipher), CodecUtils.encodeHex(cbcCipher));
|
||||||
@ -104,7 +95,7 @@ class SdfApiAdapterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSM2EncAndDec() {
|
public void testSM2EncAndDec() {
|
||||||
EccCipher sdfCipher = rpcSdfAdapter.externalEncryptECC(hs, pubKey, plain.getBytes());
|
EccCipher sdfCipher = sdfAdapter.externalEncryptECC(hs, pubKey, plain.getBytes());
|
||||||
log.info("sdf sm2 cipher: {}", sdfCipher.getC1C3C2Hex());
|
log.info("sdf sm2 cipher: {}", sdfCipher.getC1C3C2Hex());
|
||||||
byte[] bcPlain = bcAdapter.externalDecryptECC("", priKey, sdfCipher);
|
byte[] bcPlain = bcAdapter.externalDecryptECC("", priKey, sdfCipher);
|
||||||
log.info("bc sm2 plain: {}", new String(bcPlain));
|
log.info("bc sm2 plain: {}", new String(bcPlain));
|
||||||
@ -112,7 +103,7 @@ class SdfApiAdapterTest {
|
|||||||
|
|
||||||
EccCipher bcCipher = bcAdapter.externalEncryptECC("", pubKey, plain.getBytes());
|
EccCipher bcCipher = bcAdapter.externalEncryptECC("", pubKey, plain.getBytes());
|
||||||
log.info("bc sm2 cipher: {}", bcCipher.getC1C3C2Hex());
|
log.info("bc sm2 cipher: {}", bcCipher.getC1C3C2Hex());
|
||||||
byte[] sm2Plain = rpcSdfAdapter.externalDecryptECC(hs, priKey, bcCipher);
|
byte[] sm2Plain = sdfAdapter.externalDecryptECC(hs, priKey, bcCipher);
|
||||||
log.info("sdf sm2 plain: {}", new String(sm2Plain));
|
log.info("sdf sm2 plain: {}", new String(sm2Plain));
|
||||||
|
|
||||||
Assertions.assertEquals(plain, new String(sm2Plain));
|
Assertions.assertEquals(plain, new String(sm2Plain));
|
||||||
@ -122,11 +113,11 @@ class SdfApiAdapterTest {
|
|||||||
public void testSM2SignAndVerify() {
|
public void testSM2SignAndVerify() {
|
||||||
EccSignature bcSign = bcAdapter.externalSignECC("", priKey, plain.getBytes());
|
EccSignature bcSign = bcAdapter.externalSignECC("", priKey, plain.getBytes());
|
||||||
log.info("bc sm2 signature: {}", bcSign.getRawSignHex());
|
log.info("bc sm2 signature: {}", bcSign.getRawSignHex());
|
||||||
boolean sdfVerified = rpcSdfAdapter.externalVerifyECC(hs, pubKey, plain.getBytes(), bcSign);
|
boolean sdfVerified = sdfAdapter.externalVerifyECC(hs, pubKey, plain.getBytes(), bcSign);
|
||||||
log.info("sdf sm2 verified: {}", sdfVerified);
|
log.info("sdf sm2 verified: {}", sdfVerified);
|
||||||
Assertions.assertTrue(sdfVerified);
|
Assertions.assertTrue(sdfVerified);
|
||||||
|
|
||||||
EccSignature sdfSign = rpcSdfAdapter.externalSignECC(hs, priKey, plain.getBytes());
|
EccSignature sdfSign = sdfAdapter.externalSignECC(hs, priKey, plain.getBytes());
|
||||||
log.info("sdf sm2 signature: {}", sdfSign.getRawSignHex());
|
log.info("sdf sm2 signature: {}", sdfSign.getRawSignHex());
|
||||||
boolean bcVerified = bcAdapter.externalVerifyECC("", pubKey, plain.getBytes(), sdfSign);
|
boolean bcVerified = bcAdapter.externalVerifyECC("", pubKey, plain.getBytes(), sdfSign);
|
||||||
log.info("bc sm2 verified: {}", bcVerified);
|
log.info("bc sm2 verified: {}", bcVerified);
|
||||||
@ -135,12 +126,12 @@ class SdfApiAdapterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHash() {
|
public void testHash() {
|
||||||
String newSession = rpcSdfAdapter.openSession(hd);
|
String newSession = sdfAdapter.openSession(hd);
|
||||||
rpcSdfAdapter.hashInit(newSession, AlgId.SGD_SM3, null, new byte[0]);
|
sdfAdapter.hashInit(newSession, AlgId.SGD_SM3, null, new byte[0]);
|
||||||
rpcSdfAdapter.hashUpdate(newSession, plain.getBytes());
|
sdfAdapter.hashUpdate(newSession, plain.getBytes());
|
||||||
byte[] hash = rpcSdfAdapter.hashFinish(newSession);
|
byte[] hash = sdfAdapter.hashFinish(newSession);
|
||||||
log.info("sdf hash: {}", CodecUtils.encodeHex(hash));
|
log.info("sdf hash: {}", CodecUtils.encodeHex(hash));
|
||||||
rpcSdfAdapter.closeSession(newSession);
|
sdfAdapter.closeSession(newSession);
|
||||||
|
|
||||||
String bcNewSession = bcAdapter.openSession("");
|
String bcNewSession = bcAdapter.openSession("");
|
||||||
bcAdapter.hashInit(bcNewSession, AlgId.SGD_SM3, null, new byte[0]);
|
bcAdapter.hashInit(bcNewSession, AlgId.SGD_SM3, null, new byte[0]);
|
||||||
@ -154,10 +145,10 @@ class SdfApiAdapterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSm4Mac() {
|
public void testSm4Mac() {
|
||||||
String hk = rpcSdfAdapter.importKey(hs, symKey);
|
String hk = sdfAdapter.importKey(hs, symKey);
|
||||||
byte[] sdfMac = rpcSdfAdapter.calculateMAC(hs, hk, AlgId.SGD_SM4_MAC, iv, plain.getBytes());
|
byte[] sdfMac = sdfAdapter.calculateMAC(hs, hk, AlgId.SGD_SM4_MAC, iv, plain.getBytes());
|
||||||
log.info("sdf mac: {}", CodecUtils.encodeHex(sdfMac));
|
log.info("sdf mac: {}", CodecUtils.encodeHex(sdfMac));
|
||||||
rpcSdfAdapter.destroyKey(hs, hk);
|
sdfAdapter.destroyKey(hs, hk);
|
||||||
|
|
||||||
String bchk = bcAdapter.importKey("", symKey);
|
String bchk = bcAdapter.importKey("", symKey);
|
||||||
byte[] bcMac = bcAdapter.calculateMAC("", bchk, AlgId.SGD_SM4_MAC, iv, plain.getBytes());
|
byte[] bcMac = bcAdapter.calculateMAC("", bchk, AlgId.SGD_SM4_MAC, iv, plain.getBytes());
|
||||||
@ -168,7 +159,7 @@ class SdfApiAdapterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRandom() {
|
public void testRandom() {
|
||||||
byte[] r = rpcSdfAdapter.generateRandom(hs, 64);
|
byte[] r = sdfAdapter.generateRandom(hs, 64);
|
||||||
Assertions.assertEquals(64, r.length);
|
Assertions.assertEquals(64, r.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user