修改测试

This commit is contained in:
liulu 2024-12-20 11:03:01 +08:00
parent b982446dab
commit 0ba56c7c25
3 changed files with 38 additions and 17 deletions

View File

@ -1,16 +1,22 @@
package api;
import com.sunyard.chsm.enums.AlgMode;
import com.sunyard.chsm.param.KeyCreateReq;
import com.sunyard.chsm.param.KeyManageReq;
import com.sunyard.chsm.param.SymDecryptReq;
import com.sunyard.chsm.param.SymDecryptResp;
import com.sunyard.chsm.param.SymEncryptReq;
import com.sunyard.chsm.param.SymEncryptResp;
import com.sunyard.chsm.utils.CodecUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.util.StringUtils;
import java.util.Collections;
/**
* @author liulu
* @since 2024/12/18
@ -20,10 +26,24 @@ public class SymKeyTest extends BaseTest {
private static final String plain = "hjsu234127qikqwndqqw13412as324";
private final static byte[] iv = "ghwikdhj1234713v".getBytes();
private static Long keyId;
@BeforeAll
public static void beforeAll() {
keyId = execute("/key/gen", KeyCreateReq.builder().keyTemplateCode(keyTemplate).genNumber(1).build(), Long.class);
Assertions.assertTrue(keyId > 0);
}
@AfterAll
public static void afterAll() {
KeyManageReq keyManageReq = new KeyManageReq();
keyManageReq.setIds(Collections.singletonList(keyId));
execute("/key/disable", keyManageReq, Void.class);
execute("/key/destroy", keyManageReq, Void.class);
}
@Test
public void testEncrypt() {
Long keyId = 1869666111835049985L;
SymEncryptReq symEncryptReq = new SymEncryptReq();
symEncryptReq.setKeyId(keyId);
@ -47,11 +67,8 @@ public class SymKeyTest extends BaseTest {
SymDecryptResp decryptResp = execute("/sym/decrypt", decryptReq, SymDecryptResp.class);
String calPlain = new String(CodecUtils.decodeBase64(decryptResp.getPlainData()));
log.info("SymDecryptResp: {}, {}", calPlain, decryptResp);
Assertions. assertNotNull(decryptResp);
Assertions.assertNotNull(decryptResp);
Assertions.assertEquals(plain, calPlain);
}

View File

@ -27,7 +27,11 @@ import java.util.Optional;
class SdfApiAdapterTest {
private final static byte[] symKey = "nhkdhaksd4678787".getBytes();
private final static byte[] iv = "hjdashde83252i23".getBytes();
private static byte[] iv() {
return "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;
@ -72,9 +76,9 @@ class SdfApiAdapterTest {
log.info("ecb_cipher: {}", CodecUtils.encodeHex(ecbCipher));
Assertions.assertEquals(plain, new String(ecbPlain));
byte[] cbcCipher = sdfAdapter.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));
byte[] cbcPlain = sdfAdapter.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));
sdfAdapter.destroyKey(hs, hk);
@ -87,7 +91,7 @@ class SdfApiAdapterTest {
log.info("bc_ecb_cipher: {}", CodecUtils.encodeHex(bcEcbCipher));
Assertions.assertArrayEquals(ecbCipher, bcEcbCipher);
byte[] bcCbcCipher = bcAdapter.symEncrypt("", bchk, AlgId.SGD_SM4_CBC, iv, plain.getBytes());
byte[] bcCbcCipher = bcAdapter.symEncrypt("", bchk, AlgId.SGD_SM4_CBC, iv(), plain.getBytes());
log.info("bc_cbc_cipher: {}", CodecUtils.encodeHex(bcCbcCipher));
bcAdapter.destroyKey(hs, bchk);
Assertions.assertArrayEquals(cbcCipher, bcCbcCipher);
@ -146,12 +150,12 @@ class SdfApiAdapterTest {
@Test
public void testSm4Mac() {
String hk = sdfAdapter.importKey(hs, symKey);
byte[] sdfMac = sdfAdapter.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));
sdfAdapter.destroyKey(hs, hk);
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());
log.info("bc mac: {}", CodecUtils.encodeHex(bcMac));
bcAdapter.destroyKey("", bchk);
Assertions.assertArrayEquals(sdfMac, bcMac);

View File

@ -2,6 +2,7 @@ package sdf;
import com.sunyard.chsm.WebServerApp;
import com.sunyard.chsm.auth.AppUser;
import com.sunyard.chsm.constant.CryptoConst;
import com.sunyard.chsm.constant.SecurityConstant;
import com.sunyard.chsm.enums.Padding;
import com.sunyard.chsm.sdf.SdfApiService;
@ -37,7 +38,6 @@ import java.util.UUID;
public class SdfApiServiceTest {
private final static byte[] symKey = CodecUtils.decodeHex("a00f10444a727d09b94e2112cd662ea4");
private final static byte[] iv = "hjdashde83252i23".getBytes();
private final static String plain = "hjsu234127qikqwndqqw13412as324";
// private final static String ip1 = "172.16.18.41";
// private final static int port = 8889;
@ -132,9 +132,9 @@ public class SdfApiServiceTest {
log.info("ecb_cipher: {}", CodecUtils.encodeHex(ecbCipher));
Assertions.assertEquals(plain, new String(ecbPlain));
byte[] cbcCipher = sdfService.symEncrypt(AlgId.SGD_SM4_CBC, Padding.PCKS7Padding, symKey, iv, plain.getBytes());
byte[] cbcCipher = sdfService.symEncrypt(AlgId.SGD_SM4_CBC, Padding.PCKS7Padding, symKey, CryptoConst.iv(), plain.getBytes());
log.info("cbc_cipher: {}", CodecUtils.encodeHex(cbcCipher));
byte[] cbcPlain = sdfService.symDecrypt(AlgId.SGD_SM4_CBC, Padding.PCKS7Padding, symKey, iv, cbcCipher);
byte[] cbcPlain = sdfService.symDecrypt(AlgId.SGD_SM4_CBC, Padding.PCKS7Padding, symKey, CryptoConst.iv(), cbcCipher);
Assertions.assertEquals(plain, new String(cbcPlain));
Assertions.assertArrayEquals(ecbPlain, cbcPlain);
@ -144,18 +144,18 @@ public class SdfApiServiceTest {
byte[] bcEcbCipher = bcService.symEncrypt(AlgId.SGD_SM4_ECB, Padding.PCKS7Padding, symKey, null, plain.getBytes());
log.info("bc_ecb_cipher: {}", CodecUtils.encodeHex(bcEcbCipher));
Assertions.assertArrayEquals(ecbCipher, bcEcbCipher);
byte[] bcCbcCipher = bcService.symEncrypt(AlgId.SGD_SM4_CBC, Padding.PCKS7Padding, symKey, iv, plain.getBytes());
byte[] bcCbcCipher = bcService.symEncrypt(AlgId.SGD_SM4_CBC, Padding.PCKS7Padding, symKey, CryptoConst.iv(), plain.getBytes());
log.info("bc_cbc_cipher: {}", CodecUtils.encodeHex(bcCbcCipher));
Assertions.assertArrayEquals(cbcCipher, bcCbcCipher);
}
@Test
public void testSm4Mac() {
byte[] sdfMac = sdfService.calculateMAC(AlgId.SGD_SM4_MAC, Padding.PCKS7Padding, symKey, iv, plain.getBytes());
byte[] sdfMac = sdfService.calculateMAC(AlgId.SGD_SM4_MAC, Padding.PCKS7Padding, symKey, CryptoConst.iv(), plain.getBytes());
log.info("sdf mac: {}", CodecUtils.encodeHex(sdfMac));
Assertions.assertEquals(16, sdfMac.length);
byte[] bcMac = bcService.calculateMAC(AlgId.SGD_SM4_MAC, Padding.PCKS7Padding, symKey, iv, plain.getBytes());
byte[] bcMac = bcService.calculateMAC(AlgId.SGD_SM4_MAC, Padding.PCKS7Padding, symKey, CryptoConst.iv(), plain.getBytes());
log.info("bc mac: {}", CodecUtils.encodeHex(bcMac));
Assertions.assertEquals(16, bcMac.length);