Hmac-Sm3
Hmac-Sm3
是一种基于 sm3
的消息认证码算法,dongle
支持标准和流式 sm3
消息认证码算法,提供多种输出格式。
注意:
WithKey
方法必须在BySm3
之前调用
输入数据
go
// 输入字符串
hasher := dongle.Hash.FromString("hello world").WithKey([]byte("dongle")).BySm3()
// 输入字节切片
hasher := dongle.Hash.FromBytes([]byte("hello world")).WithKey([]byte("dongle")).BySm3()
// 输入文件流
file, _ := os.Open("test.txt")
hasher := dongle.Hash.FromFile(file).WithKey([]byte("dongle")).BySm3()
// 检查 HMAC 错误
if hasher.Error != nil {
fmt.Printf("HMAC 错误: %v\n", hasher.Error)
return
}
输出数据
go
// 输出 Hex 编码字符串
hasher.ToHexString() // 8c733aae1d553c466a08c3e9e5daac3e99ae220181c7c1bc8c2564961de751b3
// 输出 Hex 编码字节切片
hasher.ToHexBytes() // []byte("8c733aae1d553c466a08c3e9e5daac3e99ae220181c7c1bc8c2564961de751b3")
// 输出 Base64 编码字符串
hasher.ToBase64String() // jHM6rh1VPEZqCMPp5dqsPpmuIgGBx8G8jCVklh3nUbM=
// 输出 Base64 编码字节切片
hasher.ToBase64Bytes() // []byte("jHM6rh1VPEZqCMPp5dqsPpmuIgGBx8G8jCVklh3nUbM=")
// 输出未编码原始字符串
hasher.ToRawString()
// 输出未编码原始字节切片
hasher.ToRawBytes()