````// Copyright 2009 The Go Authors. All rights reserved.`
`// Use of this source code is governed by a BSD-style`
`// 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.`

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

`	// ValidMAC reports whether messageMAC is a valid HMAC tag for message.`
`	func ValidMAC(message, messageMAC, key []byte) bool {`
`		mac := hmac.New(sha256.New, key)`
`		mac.Write(message)`
`		expectedMAC := mac.Sum(nil)`
`		return hmac.Equal(messageMAC, expectedMAC)`
`	}`
`*/``package hmac`

`import (`
`	"crypto/subtle"`
`	"hash"`
`)`

`// FIPS 198-1:`
`// https://csrc.nist.gov/publications/fips/fips198-1/FIPS-198-1_final.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))`

`// Marshalable is the combination of encoding.BinaryMarshaler and`
`// encoding.BinaryUnmarshaler. Their method definitions are repeated here to`
`// avoid a dependency on the encoding package.`
`type marshalable interface {`
`	MarshalBinary() ([]byte, error)`
`	UnmarshalBinary([]byte) error`
`}`

`type hmac struct {`
`	opad, ipad   []byte`
`	outer, inner hash.Hash`

`	// If marshaled is true, then opad and ipad do not contain a padded`
`	// copy of the key, but rather the marshaled state of outer/inner after`
`	// opad/ipad has been fed into it.`
`	marshaled bool`
`}`

`func (h *hmac) Sum(in []byte) []byte {`
`	origLen := len(in)`
`	in = h.inner.Sum(in)`

`	if h.marshaled {`
`		if err := h.outer.(marshalable).UnmarshalBinary(h.opad); err != nil {`
`			panic(err)`
`		}`
`	} else {`
`		h.outer.Reset()`
`		h.outer.Write(h.opad)`
`	}`
`	h.outer.Write(in[origLen:])`
`	return h.outer.Sum(in[:origLen])`
`}`

`func (h *hmac) Write(p []byte) (n int, err error) {`
`	return h.inner.Write(p)`
`}`

`func (h *hmac) Size() int      { return h.outer.Size() }`
`func (h *hmac) BlockSize() int { return h.inner.BlockSize() }`

`func (h *hmac) Reset() {`
`	if h.marshaled {`
`		if err := h.inner.(marshalable).UnmarshalBinary(h.ipad); err != nil {`
`			panic(err)`
`		}`
`		return`
`	}`

`	h.inner.Reset()`
`	h.inner.Write(h.ipad)`

`	// If the underlying hash is marshalable, we can save some time by`
`	// saving a copy of the hash state now, and restoring it on future`
`	// calls to Reset and Sum instead of writing ipad/opad every time.`
`	//`
`	// If either hash is unmarshalable for whatever reason,`
`	// it's safe to bail out here.`
`	marshalableInner, innerOK := h.inner.(marshalable)`
`	if !innerOK {`
`		return`
`	}`
`	marshalableOuter, outerOK := h.outer.(marshalable)`
`	if !outerOK {`
`		return`
`	}`

`	imarshal, err := marshalableInner.MarshalBinary()`
`	if err != nil {`
`		return`
`	}`

`	h.outer.Reset()`
`	h.outer.Write(h.opad)`
`	omarshal, err := marshalableOuter.MarshalBinary()`
`	if err != nil {`
`		return`
`	}`

`	// Marshaling succeeded; save the marshaled state for later`
`	h.ipad = imarshal`
`	h.opad = omarshal`
`	h.marshaled = true`
`}`

`// New returns a new HMAC hash using the given hash.Hash type and key.`
`// New functions like sha256.New from crypto/sha256 can be used as h.`
`// h must return a new Hash every time it is called.`
`// Note that unlike other hash implementations in the standard library,`
`// the returned Hash does not implement encoding.BinaryMarshaler`
`// or encoding.BinaryUnmarshaler.`
`func New(h func() hash.Hash, key []byte) hash.Hash {`
`	hm := new(hmac)`
`	hm.outer = h()`
`	hm.inner = h()`
`	unique := true`
`	func() {`
`		defer func() {`
`			// The comparison might panic if the underlying types are not comparable.`
`			_ = recover()`
`		}()`
`		if hm.outer == hm.inner {`
`			unique = false`
`		}`
`	}()`
`	if !unique {`
`		panic("crypto/hmac: hash generation function does not produce unique values")`
`	}`
`	blocksize := hm.inner.BlockSize()`
`	hm.ipad = make([]byte, blocksize)`
`	hm.opad = make([]byte, blocksize)`
`	if len(key) > blocksize {`
`		// If key is too big, hash it.`
`		hm.outer.Write(key)`
`		key = hm.outer.Sum(nil)`
`	}`
`	copy(hm.ipad, key)`
`	copy(hm.opad, key)`
`	for i := range hm.ipad {`
`		hm.ipad[i] ^= 0x36`
`	}`
`	for i := range hm.opad {`
`		hm.opad[i] ^= 0x5c`
`	}`
`	hm.inner.Write(hm.ipad)`

`	return hm`
`}`

`// Equal compares two MACs for equality without leaking timing information.`
`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`
`	// was used.`
`	return subtle.ConstantTimeCompare(mac1, mac2) == 1`
`}`
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.62903154,"math_prob":0.85980546,"size":4383,"snap":"2021-43-2021-49","text_gpt3_token_len":1165,"char_repetition_ratio":0.12011875,"word_repetition_ratio":0.051317614,"special_character_ratio":0.3016199,"punctuation_ratio":0.20656136,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98663056,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-03T19:41:00Z\",\"WARC-Record-ID\":\"<urn:uuid:434065a8-0531-49fd-ad90-839e48310aea>\",\"Content-Length\":\"33148\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fe1ec35e-47b9-4744-bf72-cbb336210b6f>\",\"WARC-Concurrent-To\":\"<urn:uuid:6e6bed76-17f7-4b61-985b-999d93dccd5e>\",\"WARC-IP-Address\":\"172.67.172.179\",\"WARC-Target-URI\":\"https://docs.go101.org/std/src/crypto/hmac/hmac.go.html\",\"WARC-Payload-Digest\":\"sha1:2X4S5GL7EL53VN6BDTGKPJYDVKL527R3\",\"WARC-Block-Digest\":\"sha1:CNSKOVVGLB6Q4LQRRYFQT2672EOJ4SY3\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964362918.89_warc_CC-MAIN-20211203182358-20211203212358-00621.warc.gz\"}"}