From 98512fa1f2c319de6af835ec8b5ce2f78b12fa00 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 21 Oct 2021 13:18:53 +0800 Subject: [PATCH] fix bug --- CHANGELOG.md | 1 + README-EN.md | 6 +-- README.md | 12 ++--- .../java/cn/hutool/core/codec/Base64.java | 2 +- .../crypto/symmetric/SymmetricCrypto.java | 8 ++- .../java/cn/hutool/crypto/test/SmTest.java | 2 + .../crypto/test/symmetric/Sm4StreamTest.java | 51 +++++++++++++++++++ 7 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 hutool-crypto/src/test/java/cn/hutool/crypto/test/symmetric/Sm4StreamTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index cd6f79271..5ea43ec0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ * 【cache 】 修复LRUCache线程安全问题(issue#1895@Github) * 【crypto 】 修复KeyUtil异常信息参数丢失问题(issue#1902@Github) * 【core 】 修复StrUtil.split和splittoArray不一致问题(issue#I4ELU5@Github) +* 【core 】 修复SymmetricCrypto未关闭CipherOutputStream导致的问题(issue#I4EMST@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/README-EN.md b/README-EN.md index 9783a1a9f..5576237e4 100644 --- a/README-EN.md +++ b/README-EN.md @@ -201,13 +201,11 @@ If you think Hutool is good, you can donate to buy tshe author a pack of chili~, ## 👕shop -We provide the T-Shirt with Hutool Logo, please visit the shop: +We provide the T-Shirt and Sweater with Hutool Logo, please visit the shop: [Hutool T-Shirt](https://m.tb.cn/h.f47W8zc?sm=7d2b95) -
- -
+[Hutool Sweater](https://m.tb.cn/h.fUM4d6B?sm=4c0a5f) ## 📌WeChat Official Account diff --git a/README.md b/README.md index f9ea97fd9..99967c3f0 100644 --- a/README.md +++ b/README.md @@ -202,21 +202,19 @@ Hutool欢迎任何人为Hutool添砖加瓦,贡献代码,不过维护者是 如果你觉得Hutool不错,可以捐赠请维护者吃包辣条~,在此表示感谢^_^。 -点击以下链接,将页面拉到最下方点击“捐赠”即可。 - [Gitee上捐赠](https://gitee.com/dromara/hutool) [捐赠给Dromara组织](https://dromara.gitee.io/donate.html) ## 👕周边 -我们提供了印有Hutool Logo的主题T恤,欢迎点击购买: +你也可以通过购买Hutool的周边商品来支持Hutool维护哦! -[HutoolT恤商店](https://m.tb.cn/h.f47W8zc?sm=7d2b95) +我们提供了印有Hutool Logo的周边商品,欢迎点击购买支持: -
- -
+[Hutool周边商店-T恤](https://m.tb.cn/h.f47W8zc?sm=7d2b95) + +[Hutool周边商店-卫衣](https://m.tb.cn/h.fUM4d6B?sm=4c0a5f) ## 📌公众号 diff --git a/hutool-core/src/main/java/cn/hutool/core/codec/Base64.java b/hutool-core/src/main/java/cn/hutool/core/codec/Base64.java index 1b77c2ba6..ec5b018f6 100644 --- a/hutool-core/src/main/java/cn/hutool/core/codec/Base64.java +++ b/hutool-core/src/main/java/cn/hutool/core/codec/Base64.java @@ -299,7 +299,7 @@ public class Base64 { * base64解码 * * @param base64 被解码的base64字符串 - * @return 被加密后的字符串 + * @return 解码后的bytes */ public static byte[] decode(CharSequence base64) { return Base64Decoder.decode(base64); diff --git a/hutool-crypto/src/main/java/cn/hutool/crypto/symmetric/SymmetricCrypto.java b/hutool-crypto/src/main/java/cn/hutool/crypto/symmetric/SymmetricCrypto.java index abc31d182..ba92ab553 100644 --- a/hutool-crypto/src/main/java/cn/hutool/crypto/symmetric/SymmetricCrypto.java +++ b/hutool-crypto/src/main/java/cn/hutool/crypto/symmetric/SymmetricCrypto.java @@ -300,9 +300,11 @@ public class SymmetricCrypto implements SymmetricEncryptor, SymmetricDecryptor, throw new CryptoException(e); } finally { lock.unlock(); + // issue#I4EMST@Gitee + // CipherOutputStream必须关闭,才能完全写出 + IoUtil.close(cipherOutputStream); if (isClose) { IoUtil.close(data); - IoUtil.close(cipherOutputStream); } } } @@ -351,9 +353,11 @@ public class SymmetricCrypto implements SymmetricEncryptor, SymmetricDecryptor, throw new CryptoException(e); } finally { lock.unlock(); + // issue#I4EMST@Gitee + // CipherOutputStream必须关闭,才能完全写出 + IoUtil.close(cipherInputStream); if (isClose) { IoUtil.close(data); - IoUtil.close(cipherInputStream); } } } diff --git a/hutool-crypto/src/test/java/cn/hutool/crypto/test/SmTest.java b/hutool-crypto/src/test/java/cn/hutool/crypto/test/SmTest.java index 80c7995fe..c98dd21ee 100644 --- a/hutool-crypto/src/test/java/cn/hutool/crypto/test/SmTest.java +++ b/hutool-crypto/src/test/java/cn/hutool/crypto/test/SmTest.java @@ -81,4 +81,6 @@ public class SmTest { String digest = hMac.digestHex(content); Assert.assertEquals("493e3f9a1896b43075fbe54658076727960d69632ac6b6ed932195857a6840c6", digest); } + + } diff --git a/hutool-crypto/src/test/java/cn/hutool/crypto/test/symmetric/Sm4StreamTest.java b/hutool-crypto/src/test/java/cn/hutool/crypto/test/symmetric/Sm4StreamTest.java new file mode 100644 index 000000000..87ec2675c --- /dev/null +++ b/hutool-crypto/src/test/java/cn/hutool/crypto/test/symmetric/Sm4StreamTest.java @@ -0,0 +1,51 @@ +package cn.hutool.crypto.test.symmetric; + +import cn.hutool.crypto.symmetric.SM4; +import org.junit.Ignore; +import org.junit.Test; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * https://gitee.com/dromara/hutool/issues/I4EMST + */ +public class Sm4StreamTest { + + private static final SM4 sm4 = new SM4(); + + private static final boolean IS_CLOSE = false; + + @Test + @Ignore + public void sm4Test(){ + String source = "d:/test/sm4_1.txt"; + String target = "d:/test/sm4_2.data"; + String target2 = "d:/test/sm4_3.txt"; + encrypt(source, target); + decrypt(target, target2); + } + + public static void encrypt(String source, String target) { + try (InputStream input = new FileInputStream(source); + OutputStream out = new FileOutputStream(target)) { + sm4.encrypt(input, out, IS_CLOSE); + System.out.println("============encrypt end"); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void decrypt(String source, String target) { + try (InputStream input = new FileInputStream(source); + OutputStream out = new FileOutputStream(target)) { + sm4.decrypt(input, out, IS_CLOSE); + System.out.println("============decrypt end"); + } catch (IOException e) { + e.printStackTrace(); + } + } +}