diff options
Diffstat (limited to 'devdocs/go/hash%2Fmaphash%2Findex.html')
| -rw-r--r-- | devdocs/go/hash%2Fmaphash%2Findex.html | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/devdocs/go/hash%2Fmaphash%2Findex.html b/devdocs/go/hash%2Fmaphash%2Findex.html new file mode 100644 index 00000000..290b0289 --- /dev/null +++ b/devdocs/go/hash%2Fmaphash%2Findex.html @@ -0,0 +1,106 @@ +<h1> Package maphash </h1> <ul id="short-nav"> +<li><code>import "hash/maphash"</code></li> +<li><a href="#pkg-overview" class="overviewLink">Overview</a></li> +<li><a href="#pkg-index" class="indexLink">Index</a></li> +<li><a href="#pkg-examples" class="examplesLink">Examples</a></li> +</ul> <h2 id="pkg-overview">Overview </h2> <p>Package maphash provides hash functions on byte sequences. These hash functions are intended to be used to implement hash tables or other data structures that need to map arbitrary strings or byte sequences to a uniform distribution on unsigned 64-bit integers. Each different instance of a hash table or data structure should use its own <a href="#Seed">Seed</a>. </p> +<p>The hash functions are not cryptographically secure. (See crypto/sha256 and crypto/sha512 for cryptographic use.) </p> <h4 id="example_"> <span class="text">Example</span> +</h4> <p>Code:</p> <pre class="code" data-language="go"> +// The zero Hash value is valid and ready to use; setting an +// initial seed is not necessary. +var h maphash.Hash + +// Add a string to the hash, and print the current hash value. +h.WriteString("hello, ") +fmt.Printf("%#x\n", h.Sum64()) + +// Append additional data (in the form of a byte array). +h.Write([]byte{'w', 'o', 'r', 'l', 'd'}) +fmt.Printf("%#x\n", h.Sum64()) + +// Reset discards all data previously added to the Hash, without +// changing its seed. +h.Reset() + +// Use SetSeed to create a new Hash h2 which will behave +// identically to h. +var h2 maphash.Hash +h2.SetSeed(h.Seed()) + +h.WriteString("same") +h2.WriteString("same") +fmt.Printf("%#x == %#x\n", h.Sum64(), h2.Sum64()) +</pre> <h2 id="pkg-index">Index </h2> <ul id="manual-nav"> +<li><a href="#Bytes">func Bytes(seed Seed, b []byte) uint64</a></li> +<li><a href="#String">func String(seed Seed, s string) uint64</a></li> +<li><a href="#Hash">type Hash</a></li> +<li> <a href="#Hash.BlockSize">func (h *Hash) BlockSize() int</a> +</li> +<li> <a href="#Hash.Reset">func (h *Hash) Reset()</a> +</li> +<li> <a href="#Hash.Seed">func (h *Hash) Seed() Seed</a> +</li> +<li> <a href="#Hash.SetSeed">func (h *Hash) SetSeed(seed Seed)</a> +</li> +<li> <a href="#Hash.Size">func (h *Hash) Size() int</a> +</li> +<li> <a href="#Hash.Sum">func (h *Hash) Sum(b []byte) []byte</a> +</li> +<li> <a href="#Hash.Sum64">func (h *Hash) Sum64() uint64</a> +</li> +<li> <a href="#Hash.Write">func (h *Hash) Write(b []byte) (int, error)</a> +</li> +<li> <a href="#Hash.WriteByte">func (h *Hash) WriteByte(b byte) error</a> +</li> +<li> <a href="#Hash.WriteString">func (h *Hash) WriteString(s string) (int, error)</a> +</li> +<li><a href="#Seed">type Seed</a></li> +<li> <a href="#MakeSeed">func MakeSeed() Seed</a> +</li> +</ul> <div id="pkg-examples"> <h3>Examples</h3> <dl> <dd><a class="exampleLink" href="#example_">Package</a></dd> </dl> </div> <h3>Package files</h3> <p> <span>maphash.go</span> <span>maphash_runtime.go</span> </p> <h2 id="Bytes">func <span>Bytes</span> <span title="Added in Go 1.19">1.19</span> </h2> <pre data-language="go">func Bytes(seed Seed, b []byte) uint64</pre> <p>Bytes returns the hash of b with the given seed. </p> +<p>Bytes is equivalent to, but more convenient and efficient than: </p> +<pre data-language="go">var h Hash +h.SetSeed(seed) +h.Write(b) +return h.Sum64() +</pre> <h2 id="String">func <span>String</span> <span title="Added in Go 1.19">1.19</span> </h2> <pre data-language="go">func String(seed Seed, s string) uint64</pre> <p>String returns the hash of s with the given seed. </p> +<p>String is equivalent to, but more convenient and efficient than: </p> +<pre data-language="go">var h Hash +h.SetSeed(seed) +h.WriteString(s) +return h.Sum64() +</pre> <h2 id="Hash">type <span>Hash</span> <span title="Added in Go 1.14">1.14</span> </h2> <p>A Hash computes a seeded hash of a byte sequence. </p> +<p>The zero Hash is a valid Hash ready to use. A zero Hash chooses a random seed for itself during the first call to a Reset, Write, Seed, or Sum64 method. For control over the seed, use SetSeed. </p> +<p>The computed hash values depend only on the initial seed and the sequence of bytes provided to the Hash object, not on the way in which the bytes are provided. For example, the three sequences </p> +<pre data-language="go">h.Write([]byte{'f','o','o'}) +h.WriteByte('f'); h.WriteByte('o'); h.WriteByte('o') +h.WriteString("foo") +</pre> <p>all have the same effect. </p> +<p>Hashes are intended to be collision-resistant, even for situations where an adversary controls the byte sequences being hashed. </p> +<p>A Hash is not safe for concurrent use by multiple goroutines, but a Seed is. If multiple goroutines must compute the same seeded hash, each can declare its own Hash and call SetSeed with a common Seed. </p> +<pre data-language="go">type Hash struct { + // contains filtered or unexported fields +} +</pre> <h3 id="Hash.BlockSize">func (*Hash) <span>BlockSize</span> <span title="Added in Go 1.14">1.14</span> </h3> <pre data-language="go">func (h *Hash) BlockSize() int</pre> <p>BlockSize returns h's block size. </p> +<h3 id="Hash.Reset">func (*Hash) <span>Reset</span> <span title="Added in Go 1.14">1.14</span> </h3> <pre data-language="go">func (h *Hash) Reset()</pre> <p>Reset discards all bytes added to h. (The seed remains the same.) </p> +<h3 id="Hash.Seed">func (*Hash) <span>Seed</span> <span title="Added in Go 1.14">1.14</span> </h3> <pre data-language="go">func (h *Hash) Seed() Seed</pre> <p>Seed returns h's seed value. </p> +<h3 id="Hash.SetSeed">func (*Hash) <span>SetSeed</span> <span title="Added in Go 1.14">1.14</span> </h3> <pre data-language="go">func (h *Hash) SetSeed(seed Seed)</pre> <p>SetSeed sets h to use seed, which must have been returned by <a href="#MakeSeed">MakeSeed</a> or by another <a href="#Hash.Seed">Hash.Seed</a> method. Two <a href="#Hash">Hash</a> objects with the same seed behave identically. Two <a href="#Hash">Hash</a> objects with different seeds will very likely behave differently. Any bytes added to h before this call will be discarded. </p> +<h3 id="Hash.Size">func (*Hash) <span>Size</span> <span title="Added in Go 1.14">1.14</span> </h3> <pre data-language="go">func (h *Hash) Size() int</pre> <p>Size returns h's hash value size, 8 bytes. </p> +<h3 id="Hash.Sum">func (*Hash) <span>Sum</span> <span title="Added in Go 1.14">1.14</span> </h3> <pre data-language="go">func (h *Hash) Sum(b []byte) []byte</pre> <p>Sum appends the hash's current 64-bit value to b. It exists for implementing <span>hash.Hash</span>. For direct calls, it is more efficient to use <a href="#Hash.Sum64">Hash.Sum64</a>. </p> +<h3 id="Hash.Sum64">func (*Hash) <span>Sum64</span> <span title="Added in Go 1.14">1.14</span> </h3> <pre data-language="go">func (h *Hash) Sum64() uint64</pre> <p>Sum64 returns h's current 64-bit value, which depends on h's seed and the sequence of bytes added to h since the last call to <a href="#Hash.Reset">Hash.Reset</a> or <a href="#Hash.SetSeed">Hash.SetSeed</a>. </p> +<p>All bits of the Sum64 result are close to uniformly and independently distributed, so it can be safely reduced by using bit masking, shifting, or modular arithmetic. </p> +<h3 id="Hash.Write">func (*Hash) <span>Write</span> <span title="Added in Go 1.14">1.14</span> </h3> <pre data-language="go">func (h *Hash) Write(b []byte) (int, error)</pre> <p>Write adds b to the sequence of bytes hashed by h. It always writes all of b and never fails; the count and error result are for implementing <span>io.Writer</span>. </p> +<h3 id="Hash.WriteByte">func (*Hash) <span>WriteByte</span> <span title="Added in Go 1.14">1.14</span> </h3> <pre data-language="go">func (h *Hash) WriteByte(b byte) error</pre> <p>WriteByte adds b to the sequence of bytes hashed by h. It never fails; the error result is for implementing <span>io.ByteWriter</span>. </p> +<h3 id="Hash.WriteString">func (*Hash) <span>WriteString</span> <span title="Added in Go 1.14">1.14</span> </h3> <pre data-language="go">func (h *Hash) WriteString(s string) (int, error)</pre> <p>WriteString adds the bytes of s to the sequence of bytes hashed by h. It always writes all of s and never fails; the count and error result are for implementing <span>io.StringWriter</span>. </p> +<h2 id="Seed">type <span>Seed</span> <span title="Added in Go 1.14">1.14</span> </h2> <p>A Seed is a random value that selects the specific hash function computed by a <a href="#Hash">Hash</a>. If two Hashes use the same Seeds, they will compute the same hash values for any given input. If two Hashes use different Seeds, they are very likely to compute distinct hash values for any given input. </p> +<p>A Seed must be initialized by calling <a href="#MakeSeed">MakeSeed</a>. The zero seed is uninitialized and not valid for use with <a href="#Hash">Hash</a>'s SetSeed method. </p> +<p>Each Seed value is local to a single process and cannot be serialized or otherwise recreated in a different process. </p> +<pre data-language="go">type Seed struct { + // contains filtered or unexported fields +} +</pre> <h3 id="MakeSeed">func <span>MakeSeed</span> <span title="Added in Go 1.14">1.14</span> </h3> <pre data-language="go">func MakeSeed() Seed</pre> <p>MakeSeed returns a new random seed. </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/hash/maphash/" class="_attribution-link">http://golang.org/pkg/hash/maphash/</a> + </p> +</div> |
