From 0ba56c7c25b02ea15fd26e35e1464c0d2c30db4f Mon Sep 17 00:00:00 2001 From: liulu Date: Fri, 20 Dec 2024 11:03:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/test/java/api/SymKeyTest.java | 27 +++++++++++++++---- .../src/test/java/sdf/SdfApiAdapterTest.java | 16 ++++++----- .../src/test/java/sdf/SdfApiServiceTest.java | 12 ++++----- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/chsm-web-server/src/test/java/api/SymKeyTest.java b/chsm-web-server/src/test/java/api/SymKeyTest.java index dbfc5ae..fae573b 100644 --- a/chsm-web-server/src/test/java/api/SymKeyTest.java +++ b/chsm-web-server/src/test/java/api/SymKeyTest.java @@ -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); - - - } diff --git a/chsm-web-server/src/test/java/sdf/SdfApiAdapterTest.java b/chsm-web-server/src/test/java/sdf/SdfApiAdapterTest.java index 72179fa..8c9e825 100644 --- a/chsm-web-server/src/test/java/sdf/SdfApiAdapterTest.java +++ b/chsm-web-server/src/test/java/sdf/SdfApiAdapterTest.java @@ -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); diff --git a/chsm-web-server/src/test/java/sdf/SdfApiServiceTest.java b/chsm-web-server/src/test/java/sdf/SdfApiServiceTest.java index b58e069..2965a8e 100644 --- a/chsm-web-server/src/test/java/sdf/SdfApiServiceTest.java +++ b/chsm-web-server/src/test/java/sdf/SdfApiServiceTest.java @@ -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);