Skip to content
This repository has been archived by the owner on May 2, 2018. It is now read-only.

crypto/hmac: 翻译完成。 #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions src/crypto/hmac/hmac.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,31 @@
// license that can be found in the LICENSE file.

/*
Package hmac implements the Keyed-Hash Message Authentication Code (HMAC) as
defined in U.S. Federal Information Processing Standards Publication 198.
An HMAC is a cryptographic hash that uses a key to sign a message.
The receiver verifies the hash by recomputing it using the same key.
Package hmac implements the Keyed-Hash Message Authentication Code (HMAC) as
defined in U.S. Federal Information Processing Standards Publication 198.
An HMAC is a cryptographic hash that uses a key to sign a message.
The receiver verifies the hash by recomputing it using the same key.

Receivers should be careful to use Equal to compare MACs in order to avoid
timing side-channels:

// CheckMAC returns true if messageMAC is a valid HMAC tag for message.
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)
}
*/

/*
hmac 包实现了基于密钥的哈希消息认证码(HMAC),其在 198 号美国联邦信息处理标准中定义。
HMAC 是使用密钥对一段消息进行签名的密码学意义的哈希值。接收者使用同样的密钥重新计算
一遍来验证该哈希值。

Receivers should be careful to use Equal to compare MACs in order to avoid
timing side-channels:
为了避免时间相关的旁道攻击,接收者需要非常小心地使用 Equal 方法来比较 MAC 值:

// CheckMAC returns true if messageMAC is a valid HMAC tag for message.
// CheckMAC messageMAC 是 message 的正确的 HAMC 标记时返回 true。
func CheckMAC(message, messageMAC, key []byte) bool {
mac := hmac.New(sha256.New, key)
mac.Write(message)
Expand All @@ -26,12 +42,20 @@ import (
"hash"
)

// FIPS 198:
// FIPS 198:
// http://csrc.nist.gov/publications/fips/fips198/fips-198a.pdf

// key is zero padded to the block size of the hash function
// ipad = 0x36 byte repeated for key length
// opad = 0x5c byte repeated for key length
// hmac = H([key ^ opad] H([key ^ ipad] text))

// 198 号美国联邦信息处理标准:
// http://csrc.nist.gov/publications/fips/fips198/fips-198a.pdf

// key is zero padded to the block size of the hash function
// ipad = 0x36 byte repeated for key length
// opad = 0x5c byte repeated for key length
// key 会用 0 来填充以满足哈希函数的块大小
// ipad = 0x36 重复的字节,个数与 key 的长度相同
// opad = 0x5c 重复的字节,个数与 key 的长度相同
// hmac = H([key ^ opad] H([key ^ ipad] text))

type hmac struct {
Expand Down Expand Up @@ -74,7 +98,9 @@ func (h *hmac) Reset() {
h.inner.Write(h.tmp[:h.blocksize])
}

// New returns a new HMAC hash using the given hash.Hash type and key.
// New returns a new HMAC hash using the given hash.Hash type and key.

// New 构造并返回一个使用指定的 hash.Hash 类型和 key 的 HMAC 哈希接口。
func New(h func() hash.Hash, key []byte) hash.Hash {
hm := new(hmac)
hm.outer = h()
Expand All @@ -93,7 +119,9 @@ func New(h func() hash.Hash, key []byte) hash.Hash {
return hm
}

// Equal compares two MACs for equality without leaking timing information.
// Equal compares two MACs for equality without leaking timing information.

// Equal 比较两个 MAC 是否相等,并且不会泄露CPU耗时信息。
func Equal(mac1, mac2 []byte) bool {
// We don't have to be constant time if the lengths of the MACs are
// different as that suggests that a completely different hash function
Expand Down