?
This document uses PHP Chinese website manual Release
import "crypto/hmac"
概述
索引
hmac 包實(shí)現(xiàn)了美國聯(lián)邦信息處理標(biāo)準(zhǔn)出版物198中定義的 Keyed-Hash Message Authentication Code(HMAC)。HMAC 是使用密鑰簽署消息的加密散列。接收器通過使用相同的密鑰重新計(jì)算它來驗(yàn)證散列。
接收者應(yīng)小心使用 Equal 來比較 MAC,以避免定時(shí)副信道:
// CheckMAC 報(bào)告 messageMAC 是否是消息的有效 HMAC 標(biāo)記。func CheckMAC(message, messageMAC, key []byte) bool { mac := hmac.New(sha256.New, key) mac.Write(message) expectedMAC := mac.Sum(nil)return hmac.Equal(messageMAC, expectedMAC)}
func Equal(mac1, mac2 []byte) bool
func New(h func() hash.Hash, key []byte) hash.Hash
hmac.go
func Equal(mac1, mac2 []byte) bool
平等地比較兩個(gè) MAC 平等而不泄露時(shí)間信息。
func New(h func() hash.Hash, key []byte) hash.Hash
New 使用給定的 hash.Hash 類型和密鑰返回一個(gè)新的 HMAC 散列。