HMAC使用对称加密密钥,基于散列函数(如MD5, SHA1, SHA224, SHA256, SHA384, SHA512, SHA3-256, SHA3-512 等哈希算法)的消息认证码算法。
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
)
func main() {
// SHA256哈希算法,并设置密钥
mac := hmac.New(sha256.New, []byte("secret"))
// 设置明文
io.WriteString(mac, "Hello world")
sum := mac.Sum(nil)
fmt.Println(hex.EncodeToString(sum[:]))
// 0d5548fb7450e619b0753725068707519ed41cd212b0500bc20427e3ef66e08e
}
参考
https://www.packetizer.com/rfc/rfc2104/https://syaning.github.io/go-pkgs/crypto/hmac.htmlhttps://www.lddgo.net/encrypt/hmachttp://doc.golang.ltd/pkg/crypto_hmac.htm