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

@ -3,17 +3,19 @@ package cn.hutool.crypto.digest;
/**
* HMAC算法类型<br>
* see: https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Mac
*
*
* @author Looly
*/
public enum HmacAlgorithm {
HmacMD5("HmacMD5"),
HmacSHA1("HmacSHA1"),
HmacSHA256("HmacSHA256"),
HmacSHA384("HmacSHA384"),
HmacMD5("HmacMD5"),
HmacSHA1("HmacSHA1"),
HmacSHA256("HmacSHA256"),
HmacSHA384("HmacSHA384"),
HmacSHA512("HmacSHA512"),
/** HmacSM3算法实现需要BouncyCastle库支持 */
HmacSM3("HmacSM3");
HmacSM3("HmacSM3"),
/** SM4 CMAC模式实现需要BouncyCastle库支持 */
SM4CMAC("SM4CMAC");
private final String value;
@ -24,4 +26,4 @@ public enum HmacAlgorithm {
public String getValue() {
return this.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);
}
}