) testdata).get("cert");
327 |
328 | Assert.assertNotNull(publicKey);
329 | Assert.assertNotNull(privateKey);
330 | Assert.assertNotNull(cert);
331 | }
332 |
333 | /**
334 | * 测试从从字符串加载私钥对象
335 | *
336 | *
337 | * javagm:
338 | * testdata:
339 | * private-key: |
340 | * -----BEGIN PRIVATE KEY-----
341 | * MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgc0UCgfELjC0V+xUm
342 | * ELYFmy0J0cee42ZpKyQ4FRTBlJSgCgYIKoEcz1UBgi2hRANCAATJbIFbxcAaDxMk
343 | * 7XExTRU/bBnGEu6YfaleJxnLZS40NDNjZV+ztveWfLZk2+oWieykM3/yZ/6IieJk
344 | * 5uuohUjD
345 | * -----END PRIVATE KEY-----
346 | * public-key: |
347 | * -----BEGIN PUBLIC KEY-----
348 | * MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEyWyBW8XAGg8TJO1xMU0VP2wZxhLu
349 | * mH2pXicZy2UuNDQzY2Vfs7b3lny2ZNvqFonspDN/8mf+iIniZObrqIVIww==
350 | * -----END PUBLIC KEY-----
351 | * cert: |
352 | * -----BEGIN CERTIFICATE-----
353 | * MIIBdzCCAR2gAwIBAgIJAfA3Qnph7CieMAoGCCqBHM9VAYN1MBIxEDAOBgNVBAMM
354 | * B1Jvb3QgQ0EwHhcNMjMxMjAyMTQzMjMxWhcNODkxMjI3MDAwMDAwWjASMRAwDgYD
355 | * VQQDEwdSb290IENBMFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEyWyBW8XAGg8T
356 | * JO1xMU0VP2wZxhLumH2pXicZy2UuNDQzY2Vfs7b3lny2ZNvqFonspDN/8mf+iIni
357 | * ZObrqIVIw6NcMFowHQYDVR0OBBYEFOMvj2LPGlkOw1M1Pj34klVi8SFgMA8GA1Ud
358 | * EwEB/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgEGMBgGA1UdEQQRMA+BDXRlc3RAdHdn
359 | * Yy5jb20wCgYIKoEcz1UBg3UDSAAwRQIgTeoLjt+eP3kwQg17G+l12wj4MQNed1hW
360 | * aZZkJe43rkICIQCdI3WhnrvzhbEijsTXL1woIwnFgY9MIci7BmKLMpMM6w==
361 | * -----END CERTIFICATE-----
362 | *
363 | */
364 | @Test
365 | public void testLoadPrivFromString() throws Exception {
366 | Map javagm = this.configMap.get("javagm");
367 | Object testdata = javagm.get("testdata");
368 | String privateKey = (String) ((Map) testdata).get("private-key");
369 |
370 | PrivateKey privKey = SM2Util.loadPrivFromString(privateKey, "");
371 | Assert.assertNotNull(privKey);
372 | }
373 |
374 | /**
375 | * 测试从从字符串加载公钥对象
376 | *
377 | * @throws Exception
378 | */
379 | @Test
380 | public void testLoadPublicFromString() throws Exception {
381 | Map javagm = this.configMap.get("javagm");
382 | Object testdata = javagm.get("testdata");
383 |
384 | String publicKey = (String) ((Map) testdata).get("public-key");
385 | PublicKey pubKey = SM2Util.loadPublicFromString(publicKey);
386 | Assert.assertNotNull(pubKey);
387 | }
388 |
389 | /**
390 | * 测试从字符串加载密钥对并测试加解密
391 | *
392 | * @throws Exception
393 | */
394 | @Test
395 | public void testLoadPublicAndPrivFromString() throws Exception {
396 | Map javagm = this.configMap.get("javagm");
397 | Object testdata = javagm.get("testdata");
398 |
399 | String publicKey = (String) ((Map) testdata).get("public-key");
400 | String privateKey = (String) ((Map) testdata).get("private-key");
401 |
402 | PublicKey pubKey = SM2Util.loadPublicFromString(publicKey);
403 | PrivateKey privKey = SM2Util.loadPrivFromString(privateKey, "");
404 |
405 | Assert.assertNotNull(pubKey);
406 | Assert.assertNotNull(privKey);
407 |
408 | SM2EnginePool sm2EnginePool = new SM2EnginePool(1, SM2Engine.Mode.C1C3C2);
409 | SM2Engine sm2Engine = null;
410 |
411 | try {
412 | SM2Util instance = new SM2Util();
413 | sm2Engine = sm2EnginePool.borrowObject();
414 | byte[] encrypted = instance.encrypt(sm2Engine, pubKey, message);
415 | byte[] rs = instance.decrypt(sm2Engine, privKey, encrypted);
416 | Assert.assertEquals(new String(message), new String(rs));
417 |
418 | byte[] encrypted2 = instance.encrypt(sm2Engine, pubKey, "msg".getBytes());
419 | rs = instance.decrypt(sm2Engine, privKey, encrypted2);
420 | Assert.assertNotEquals(new String(message), new String(rs));
421 | } catch (Exception e) {
422 | e.printStackTrace();
423 | Assert.fail(exceptionHappened);
424 | } finally {
425 | if (sm2Engine != null) {
426 | sm2EnginePool.returnObject(sm2Engine);
427 | }
428 | }
429 | }
430 |
431 | /**
432 | * 测试从字符串加载证书对象
433 | *
434 | * @throws Exception
435 | */
436 | @Test
437 | public void testLoadX509CertificateFromString() throws Exception {
438 | Map javagm = this.configMap.get("javagm");
439 | Object testdata = javagm.get("testdata");
440 |
441 | String cert = (String) ((Map) testdata).get("cert");
442 | Assert.assertNotNull(cert);
443 |
444 | X509Certificate certificate = SM2Util.loadX509CertificateFromString(cert);
445 | Assert.assertNotNull(certificate);
446 | Assert.assertEquals("SM3WITHSM2", certificate.getSigAlgName());
447 | }
448 |
449 | static {
450 | try {
451 | Security.addProvider(new BouncyCastleProvider());
452 | } catch (Exception e) {
453 | e.printStackTrace();
454 | }
455 | }
456 | }
--------------------------------------------------------------------------------
/src/test/java/SM2UtilThreadTest.java:
--------------------------------------------------------------------------------
1 | import java.io.IOException;
2 | import java.security.KeyPair;
3 | import java.security.PrivateKey;
4 | import java.security.PublicKey;
5 | import java.time.Duration;
6 | import java.time.Instant;
7 | import java.util.Queue;
8 | import java.util.concurrent.ConcurrentLinkedQueue;
9 | import java.util.concurrent.CountDownLatch;
10 |
11 | import org.apache.commons.lang3.RandomStringUtils;
12 | import org.bouncycastle.crypto.engines.SM2Engine;
13 | import org.junit.Assert;
14 | import org.junit.FixMethodOrder;
15 | import org.junit.Test;
16 | import org.junit.runners.MethodSorters;
17 | import twgc.gm.pool.SM2EnginePool;
18 | import twgc.gm.sm2.SM2Util;
19 |
20 |
21 | /**
22 | * @author Sam
23 | * @description 国密算法SM2工具类线程安全测试
24 | * @date 2021/1/10
25 | */
26 | @FixMethodOrder(MethodSorters.JVM)
27 | public class SM2UtilThreadTest {
28 |
29 | static String exceptionHappened = "Exception happened";
30 | static int randomData = 128;
31 | static byte[] message = RandomStringUtils.random(randomData).getBytes();
32 | static SM2EnginePool sm2EnginePool;
33 |
34 | static {
35 | try {
36 | sm2EnginePool = new SM2EnginePool(SM2Engine.Mode.C1C3C2);
37 | } catch (IOException e) {
38 | e.printStackTrace();
39 | }
40 | }
41 |
42 | PublicKey pubKey;
43 | PrivateKey privKey;
44 | KeyPair keyPair;
45 |
46 | @Test
47 | public void threadSafeEncryptDecrypt() {
48 | try {
49 | Queue results = new ConcurrentLinkedQueue<>();
50 | Queue ex = new ConcurrentLinkedQueue<>();
51 | SM2Util instance = new SM2Util();
52 | this.keyPair = instance.generatekeyPair();
53 | this.pubKey = keyPair.getPublic();
54 | this.privKey = keyPair.getPrivate();
55 |
56 | SM2Engine sm2Engine = sm2EnginePool.borrowObject();
57 | byte[] encrypted = instance.encrypt(sm2Engine, this.pubKey, message);
58 | byte[] rs = instance.decrypt(sm2Engine, this.privKey, encrypted);
59 | Assert.assertEquals(new String(message), new String(rs));
60 | sm2EnginePool.returnObject(sm2Engine);
61 |
62 | int threadNum = 300;
63 | final CountDownLatch startGate = new CountDownLatch(1);
64 | final CountDownLatch endGate = new CountDownLatch(threadNum);
65 | for (int i = 0; i < threadNum; i++) {
66 | new Thread(() -> {
67 | try {
68 | startGate.await();
69 | SM2Engine sm2Engine1 = null;
70 | try {
71 | sm2Engine1 = sm2EnginePool.borrowObject();
72 | results.add(instance.decrypt(sm2Engine1, this.privKey, encrypted));
73 | } catch (Exception e) {
74 | ex.add(e);
75 | } finally {
76 | if (sm2Engine1 != null) {
77 | sm2EnginePool.returnObject(sm2Engine1);
78 | }
79 | endGate.countDown();
80 | }
81 | } catch (InterruptedException e) {
82 | e.printStackTrace();
83 | }
84 | }).start();
85 | }
86 |
87 | long start = System.currentTimeMillis();
88 | startGate.countDown();
89 | try {
90 | endGate.await();
91 | } catch (InterruptedException e) {
92 | e.printStackTrace();
93 | }
94 | long end = System.currentTimeMillis();
95 | System.out.println("cost times :" + (end - start));
96 |
97 | while (!ex.isEmpty()) {
98 | Exception e = ex.poll();
99 | e.printStackTrace();
100 | Assert.fail(exceptionHappened);
101 | }
102 | Assert.assertEquals(threadNum, results.size());
103 | while (!results.isEmpty()) {
104 | rs = results.poll();
105 | Assert.assertEquals(new String(message), new String(rs));
106 | }
107 | } catch (Exception e) {
108 | e.printStackTrace();
109 | Assert.fail(exceptionHappened);
110 | }
111 | }
112 |
113 | @Test
114 | public void threadSafeSignVerify() {
115 | try {
116 | Queue results = new ConcurrentLinkedQueue<>();
117 | Queue ex = new ConcurrentLinkedQueue<>();
118 | SM2Util instance = new SM2Util();
119 | this.keyPair = instance.generatekeyPair();
120 | this.pubKey = keyPair.getPublic();
121 | this.privKey = keyPair.getPrivate();
122 | byte[] signbyte = instance.sign(this.privKey, message);
123 | boolean rs = instance.verify(this.pubKey, message, signbyte);
124 | Assert.assertTrue(rs);
125 | for (int i = 0; i < 300; i++) {
126 | new Thread(() -> {
127 | try {
128 | results.add(instance.verify(this.pubKey, message, signbyte));
129 | } catch (Exception e) {
130 | ex.add(e);
131 | }
132 | }).start();
133 | }
134 | Thread.sleep(5000);
135 | while (!ex.isEmpty()) {
136 | Exception e = ex.poll();
137 | e.printStackTrace();
138 | Assert.fail(exceptionHappened);
139 | }
140 | Assert.assertEquals(300, results.size());
141 | while (!results.isEmpty()) {
142 | Assert.assertTrue(results.poll());
143 | }
144 | } catch (Exception e) {
145 | e.printStackTrace();
146 | Assert.fail(exceptionHappened);
147 | }
148 | }
149 |
150 | @Test
151 | public void testKeyGen() {
152 | try {
153 | SM2Util instance = new SM2Util();
154 | Instant start = Instant.now();
155 | for (int i = 0; i < 1000; i++) {
156 | instance.generatekeyPair();
157 | }
158 | System.out.println("cost ms: " + Duration.between(start, Instant.now()).toMillis());
159 | } catch (Exception e) {
160 | e.printStackTrace();
161 | }
162 | }
163 | }
--------------------------------------------------------------------------------
/src/test/java/SM3UtilTest.java:
--------------------------------------------------------------------------------
1 | import org.apache.commons.lang3.RandomStringUtils;
2 | import org.bouncycastle.crypto.digests.SM3Digest;
3 | import org.junit.Assert;
4 | import org.junit.Test;
5 | import twgc.gm.pool.SM3DigestPool;
6 | import twgc.gm.sm3.SM3Util;
7 |
8 | /**
9 | * @author Sean
10 | * @Description: 国密算法SM3工具类测试
11 | * @date 2020/9/18
12 | */
13 | public class SM3UtilTest {
14 |
15 | static int randomData = 128;
16 | static byte[] message = RandomStringUtils.random(randomData).getBytes();
17 |
18 | static SM3DigestPool sm3DigestPool = new SM3DigestPool(1);
19 |
20 | @Test
21 | public void hashAndVerify() throws Exception {
22 | SM3Digest sm3Digest = null;
23 | try {
24 | sm3Digest = sm3DigestPool.borrowObject();
25 | byte[] hashVal = SM3Util.hash(sm3Digest, message);
26 | Assert.assertTrue(SM3Util.verify(sm3Digest, message, hashVal));
27 | } finally {
28 | if (sm3Digest != null) {
29 | sm3DigestPool.returnObject(sm3Digest);
30 | }
31 | }
32 | }
33 | }
--------------------------------------------------------------------------------
/src/test/java/SM3UtilThreadTest.java:
--------------------------------------------------------------------------------
1 | import java.io.IOException;
2 | import java.util.Queue;
3 | import java.util.concurrent.ConcurrentLinkedQueue;
4 |
5 | import org.apache.commons.lang3.RandomStringUtils;
6 | import org.bouncycastle.crypto.digests.SM3Digest;
7 | import org.junit.Assert;
8 | import org.junit.Test;
9 | import twgc.gm.pool.SM3DigestPool;
10 | import twgc.gm.sm3.SM3Util;
11 |
12 |
13 | /**
14 | * @author Sam
15 | * @Description: 国密算法SM3工具类线程安全测试
16 | * @date 2021/1/10
17 | */
18 | public class SM3UtilThreadTest {
19 |
20 | static int randomData = 128;
21 | static byte[] message = RandomStringUtils.random(randomData).getBytes();
22 |
23 | static SM3DigestPool sm3DigestPool;
24 |
25 | static {
26 | try {
27 | sm3DigestPool = new SM3DigestPool();
28 | } catch (IOException e) {
29 | e.printStackTrace();
30 | }
31 | }
32 |
33 | static String exceptionHappened = "Exception happened";
34 |
35 | @Test
36 | public void hashAndVerify() throws Exception {
37 | Queue results = new ConcurrentLinkedQueue<>();
38 | Queue ex = new ConcurrentLinkedQueue<>();
39 | SM3Digest sm3Digest = null;
40 | try {
41 | sm3Digest = sm3DigestPool.borrowObject();
42 | byte[] hashVal = SM3Util.hash(sm3Digest, message);
43 | Assert.assertTrue(SM3Util.verify(sm3Digest, message, hashVal));
44 | } finally {
45 | if (sm3Digest != null) {
46 | sm3DigestPool.returnObject(sm3Digest);
47 | }
48 | }
49 |
50 | for (int i = 0; i < 300; i++) {
51 | new Thread(() -> {
52 | SM3Digest sm3DigestInThead = null;
53 | try {
54 | sm3DigestInThead = sm3DigestPool.borrowObject();
55 | byte[] hashValInThread = SM3Util.hash(sm3DigestInThead, message);
56 | results.add(SM3Util.verify(sm3DigestInThead, message, hashValInThread));
57 | } catch (Exception e) {
58 | ex.add(e);
59 | } finally {
60 | if (sm3DigestInThead != null) {
61 | sm3DigestPool.returnObject(sm3DigestInThead);
62 | }
63 | }
64 | }).start();
65 | }
66 | while (!ex.isEmpty()) {
67 | Exception e = ex.poll();
68 | e.printStackTrace();
69 | Assert.fail(exceptionHappened);
70 | }
71 | Thread.sleep(5000);
72 | Assert.assertEquals(300, results.size());
73 | while (!results.isEmpty()) {
74 | Assert.assertTrue(results.poll());
75 | }
76 | }
77 | }
--------------------------------------------------------------------------------
/src/test/java/SM4InterationTest.java:
--------------------------------------------------------------------------------
1 | import javax.crypto.Cipher;
2 | import javax.crypto.spec.SecretKeySpec;
3 |
4 | import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
5 | import org.junit.Assert;
6 | import org.junit.Test;
7 |
8 | import twgc.gm.pool.SM4CipherPool;
9 | import twgc.gm.sm4.SM4Cipher;
10 | import twgc.gm.sm4.SM4ModeAndPaddingEnum;
11 | import twgc.gm.sm4.SM4Util;
12 |
13 |
14 | public class SM4InterationTest {
15 |
16 | @Test
17 | public void sm4Interaction() throws Exception {
18 | SM4CipherPool sm4CipherPool = new SM4CipherPool(10);
19 | SM4Cipher sm4Cipher = null;
20 | try {
21 | sm4Cipher = sm4CipherPool.borrowObject();
22 | Cipher cipher = sm4Cipher.getCipher(SM4ModeAndPaddingEnum.SM4_CBC_PKCS7Padding);
23 |
24 | byte[] key = "1234567890abcdef".getBytes();
25 | SecretKeySpec sm4Key = new SecretKeySpec(key, SM4ModeAndPaddingEnum.SM4_CBC_PKCS7Padding.getName());
26 | byte[] iv = "ilovegolangjava.".getBytes();
27 | SM4Util instance = new SM4Util();
28 | //解密校验
29 | String cryptText = "8781d981f7ffd6c1a780f8b213f596aa535c8bb6389923f8329f79a1707966e2";
30 | byte[] b = instance.decrypt(cipher, ByteUtils.fromHexString(cryptText), sm4Key, iv);
31 | Assert.assertEquals("I am encrypted by golang SM4.", new String(b));
32 |
33 | //加密校验,SM4加密以下明文以供Go SM4进行解密验证
34 | byte[] msg = "I am encrypted by java SM4.".getBytes();
35 | byte[] cryptData = instance.encrypt(cipher, msg, sm4Key, iv);
36 | String cryptStr = ByteUtils.toHexString(cryptData);
37 | System.out.println(cryptStr);
38 | } finally {
39 | if (sm4Cipher != null) {
40 | sm4CipherPool.returnObject(sm4Cipher);
41 | }
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/test/java/SM4UtilTest.java:
--------------------------------------------------------------------------------
1 | import java.io.IOException;
2 | import java.nio.charset.StandardCharsets;
3 | import java.security.NoSuchAlgorithmException;
4 | import java.security.NoSuchProviderException;
5 | import java.util.Arrays;
6 | import java.util.Collection;
7 | import java.util.Queue;
8 | import java.util.concurrent.ConcurrentLinkedQueue;
9 | import javax.crypto.Cipher;
10 | import javax.crypto.spec.SecretKeySpec;
11 |
12 | import org.apache.commons.lang3.RandomStringUtils;
13 | import org.junit.Assert;
14 | import org.junit.Test;
15 | import org.junit.runner.RunWith;
16 | import org.junit.runners.Parameterized;
17 | import org.junit.runners.Parameterized.Parameters;
18 | import twgc.gm.pool.SM4CipherPool;
19 | import twgc.gm.sm4.SM4Cipher;
20 | import twgc.gm.sm4.SM4ModeAndPaddingEnum;
21 | import twgc.gm.sm4.SM4Util;
22 |
23 | /**
24 | * @author Sean
25 | * @Description: 国密算法SM4工具类测试
26 | * @date 2020/9/18
27 | */
28 |
29 | @RunWith(Parameterized.class)
30 | public class SM4UtilTest {
31 | private static byte[] content = null;
32 | private static final byte[] content16 = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
33 | private static byte[] iv = null;
34 | private static SM4ModeAndPaddingEnum type;
35 | static int randomData = 128;
36 | static String message = RandomStringUtils.random(randomData);
37 | static String exceptionHappened = "Exception happened";
38 | SM4CipherPool sm4CipherPool;
39 | {
40 | try {
41 | sm4CipherPool = new SM4CipherPool();
42 | } catch (IOException e) {
43 | e.printStackTrace();
44 | }
45 | }
46 |
47 | @SuppressWarnings("rawtypes")
48 | @Parameters(name = "{index}: sm4({1})")
49 | public static Collection prepareData() {
50 | Object[][] object = {
51 | {content16, SM4ModeAndPaddingEnum.SM4_ECB_NoPadding, false},
52 | {message.getBytes(StandardCharsets.UTF_8), SM4ModeAndPaddingEnum.SM4_ECB_PKCS5Padding, false},
53 | {message.getBytes(StandardCharsets.UTF_8), SM4ModeAndPaddingEnum.SM4_ECB_PKCS7Padding, false},
54 | {content16, SM4ModeAndPaddingEnum.SM4_CBC_NoPadding, true},
55 | {message.getBytes(StandardCharsets.UTF_8), SM4ModeAndPaddingEnum.SM4_CBC_PKCS5Padding, true},
56 | {message.getBytes(StandardCharsets.UTF_8), SM4ModeAndPaddingEnum.SM4_CBC_PKCS7Padding, true},
57 | {message.getBytes(StandardCharsets.UTF_8), SM4ModeAndPaddingEnum.SM4_CFB_NoPadding, true},
58 | {message.getBytes(StandardCharsets.UTF_8), SM4ModeAndPaddingEnum.SM4_OFB_NoPadding, true},
59 | {message.getBytes(StandardCharsets.UTF_8), SM4ModeAndPaddingEnum.SM4_CTR_NoPadding, true}
60 | };
61 | return Arrays.asList(object);
62 | }
63 |
64 | public SM4UtilTest(byte[] content, SM4ModeAndPaddingEnum type, boolean flag) throws NoSuchProviderException, NoSuchAlgorithmException {
65 | SM4UtilTest.content = content;
66 | SM4UtilTest.type = type;
67 | if (flag) {
68 | SM4Util instance = new SM4Util();
69 | iv = instance.generateKey();
70 | }
71 | }
72 |
73 | @Test
74 | public void testingSM4() throws Exception {
75 | SM4Cipher sm4Cipher = null;
76 | try {
77 | sm4Cipher = sm4CipherPool.borrowObject();
78 | Cipher cipher = sm4Cipher.getCipher(type);
79 | SM4Util instance = new SM4Util();
80 | byte[] key = instance.generateKey();
81 | SecretKeySpec sm4Key = new SecretKeySpec(key, type.getName());
82 | System.out.println("===== " + type + " =====");
83 | // 加密
84 | byte[] v = instance.encrypt(cipher, content, sm4Key, iv);
85 | // 解密
86 | byte[] c = instance.decrypt(cipher, v, sm4Key, iv);
87 |
88 | System.out.println("解密内容:" + new String(c));
89 | Assert.assertArrayEquals(c, content);
90 | } finally {
91 | if (sm4Cipher != null) {
92 | sm4CipherPool.returnObject(sm4Cipher);
93 | }
94 | }
95 | }
96 |
97 | @Test
98 | public void threadsafe() throws Exception {
99 | SM4Cipher sm4Cipher;
100 | sm4Cipher = sm4CipherPool.borrowObject();
101 | Queue results = new ConcurrentLinkedQueue<>();
102 | Queue ex = new ConcurrentLinkedQueue<>();
103 | SM4Util instance = new SM4Util();
104 | byte[] key = instance.generateKey();
105 | SecretKeySpec sm4Key = new SecretKeySpec(key, type.getName());
106 | byte[] v;
107 | try {
108 | Cipher cipher = sm4Cipher.getCipher(type);
109 | System.out.println("===== " + type + " =====");
110 | // 加密
111 | v = instance.encrypt(cipher, content, sm4Key, iv);
112 | // 解密
113 | byte[] c = instance.decrypt(cipher, v, sm4Key, iv);
114 |
115 | System.out.println("解密内容:" + new String(c));
116 | Assert.assertArrayEquals(c, content);
117 | } finally {
118 | if (sm4Cipher != null) {
119 | sm4CipherPool.returnObject(sm4Cipher);
120 | }
121 | }
122 | for (int i = 0; i < 300; i++) {
123 | new Thread(() -> {
124 | SM4Cipher sm4Ciphertest = null;
125 | try {
126 | sm4Ciphertest = sm4CipherPool.borrowObject();
127 | Cipher ciphertest = sm4Ciphertest.getCipher(type);
128 | results.add(instance.decrypt(ciphertest, v, sm4Key, iv));
129 | } catch (Exception e) {
130 | ex.add(e);
131 | } finally {
132 | if (sm4Ciphertest != null) {
133 | sm4CipherPool.returnObject(sm4Ciphertest);
134 | }
135 | }
136 | }).start();
137 | }
138 | Thread.sleep(5000);
139 | while (!ex.isEmpty()) {
140 | Exception e = ex.poll();
141 | e.printStackTrace();
142 | Assert.fail(exceptionHappened);
143 | }
144 | Assert.assertEquals(300, results.size());
145 | while (!results.isEmpty()) {
146 | Assert.assertArrayEquals(results.poll(), content);
147 | }
148 | }
149 | }
150 |
--------------------------------------------------------------------------------
/src/test/resources/pool-config.yaml:
--------------------------------------------------------------------------------
1 | default: &default
2 | maxTotal: 10
3 | maxIdle: 8
4 | minIdle: 2
5 | maxWaitMillis: 1000
6 |
7 | sm2:
8 | <<: *default
9 | maxTotal: 30
10 |
11 | sm3:
12 | <<: *default
13 | maxTotal: 30
14 |
15 | sm4:
16 | <<: *default
17 | minIdle: 4
--------------------------------------------------------------------------------
/src/test/resources/testdata.yml:
--------------------------------------------------------------------------------
1 | javagm:
2 | testdata:
3 | private-key: |
4 | -----BEGIN PRIVATE KEY-----
5 | MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgc0UCgfELjC0V+xUm
6 | ELYFmy0J0cee42ZpKyQ4FRTBlJSgCgYIKoEcz1UBgi2hRANCAATJbIFbxcAaDxMk
7 | 7XExTRU/bBnGEu6YfaleJxnLZS40NDNjZV+ztveWfLZk2+oWieykM3/yZ/6IieJk
8 | 5uuohUjD
9 | -----END PRIVATE KEY-----
10 | public-key: |
11 | -----BEGIN PUBLIC KEY-----
12 | MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEyWyBW8XAGg8TJO1xMU0VP2wZxhLu
13 | mH2pXicZy2UuNDQzY2Vfs7b3lny2ZNvqFonspDN/8mf+iIniZObrqIVIww==
14 | -----END PUBLIC KEY-----
15 | cert: |
16 | -----BEGIN CERTIFICATE-----
17 | MIIBdzCCAR2gAwIBAgIJAfA3Qnph7CieMAoGCCqBHM9VAYN1MBIxEDAOBgNVBAMM
18 | B1Jvb3QgQ0EwHhcNMjMxMjAyMTQzMjMxWhcNODkxMjI3MDAwMDAwWjASMRAwDgYD
19 | VQQDEwdSb290IENBMFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEyWyBW8XAGg8T
20 | JO1xMU0VP2wZxhLumH2pXicZy2UuNDQzY2Vfs7b3lny2ZNvqFonspDN/8mf+iIni
21 | ZObrqIVIw6NcMFowHQYDVR0OBBYEFOMvj2LPGlkOw1M1Pj34klVi8SFgMA8GA1Ud
22 | EwEB/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgEGMBgGA1UdEQQRMA+BDXRlc3RAdHdn
23 | Yy5jb20wCgYIKoEcz1UBg3UDSAAwRQIgTeoLjt+eP3kwQg17G+l12wj4MQNed1hW
24 | aZZkJe43rkICIQCdI3WhnrvzhbEijsTXL1woIwnFgY9MIci7BmKLMpMM6w==
25 | -----END CERTIFICATE-----
--------------------------------------------------------------------------------