blob: e08e77192154ab8d265f78eb16a9e9b7cff0f158 (
plain)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 | <h1> Package hmac  </h1>     <ul id="short-nav">
<li><code>import "crypto/hmac"</code></li>
<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
<li><a href="#pkg-index" class="indexLink">Index</a></li>
</ul>     <h2 id="pkg-overview">Overview </h2> <p>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. </p>
<p>Receivers should be careful to use Equal to compare MACs in order to avoid timing side-channels: </p>
<pre data-language="go">// 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)
}
</pre>      <h2 id="pkg-index">Index </h2>  <ul id="manual-nav">
<li><a href="#Equal">func Equal(mac1, mac2 []byte) bool</a></li>
<li><a href="#New">func New(h func() hash.Hash, key []byte) hash.Hash</a></li>
</ul> <h3>Package files</h3> <p>  <span>hmac.go</span>  </p>   <h2 id="Equal">func <span>Equal</span>  <span title="Added in Go 1.1">1.1</span> </h2> <pre data-language="go">func Equal(mac1, mac2 []byte) bool</pre> <p>Equal compares two MACs for equality without leaking timing information. </p>
<h2 id="New">func <span>New</span>  </h2> <pre data-language="go">func New(h func() hash.Hash, key []byte) hash.Hash</pre> <p>New returns a new HMAC hash using the given <span>hash.Hash</span> type and key. New functions like sha256.New from <span>crypto/sha256</span> 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 <span>encoding.BinaryMarshaler</span> or <span>encoding.BinaryUnmarshaler</span>. </p><div class="_attribution">
  <p class="_attribution-p">
    © Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
    <a href="http://golang.org/pkg/crypto/hmac/" class="_attribution-link">http://golang.org/pkg/crypto/hmac/</a>
  </p>
</div>
 |