add SM4CMAC

This commit is contained in:
Looly 2022-03-31 15:30:18 +08:00
parent 77d3015bc0
commit 4cee569d22
3 changed files with 24 additions and 7 deletions

View File

@ -12,6 +12,7 @@
* 【core 】 ZipWriter增加add方法重载
* 【core 】 IterUtil增加filtered增加FilterIterissue#2228
* 【core 】 增加NodeListIter、ResettableIter
* 【crypto 】 HmacAlgorithm增加SM4CMACissue#2206@Github
### 🐞Bug修复
* 【core 】 IdcardUtil#getCityCodeByIdCard位数问题issue#2224@Github

View File

@ -13,7 +13,9 @@ public enum HmacAlgorithm {
HmacSHA384("HmacSHA384"),
HmacSHA512("HmacSHA512"),
/** HmacSM3算法实现需要BouncyCastle库支持 */
HmacSM3("HmacSM3");
HmacSM3("HmacSM3"),
/** SM4 CMAC模式实现需要BouncyCastle库支持 */
SM4CMAC("SM4CMAC");
private final String value;

View File

@ -83,4 +83,18 @@ public class HmacTest {
String macHex1 = mac.digestHex(testStr);
Assert.assertEquals("d9ad618357c1bfb1d9d1200a763d5eaa", macHex1);
}
@Test
public void sm4CMACTest(){
// https://github.com/dromara/hutool/issues/2206
final byte[] key = new byte[16];
HMac mac = new HMac(HmacAlgorithm.SM4CMAC,
KeyUtil.generateKey("SM4", key));
// 原文
String testStr = "test中文";
String macHex1 = mac.digestHex(testStr);
Assert.assertEquals("58a0d231315664af51b858a174eabc21", macHex1);
}
}