Documentation ¶
Overview ¶
Package bytes/hash 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 of integers. The hash functions are collision-resistant but are not cryptographically secure (use one of the hash functions in crypto/* if you need that).
The produced hashes depend only on the sequence of bytes provided to the Hash object, not on the way in which they are provided. For example, the calls
h.AddString("foo") h.AddBytes([]byte{'f','o','o'}) h.AddByte('f'); h.AddByte('o'); h.AddByte('o')
will all have the same effect.
Two Hash instances in the same process using the same seed behave identically.
Two Hash instances with the same seed in different processes are not guaranteed to behave identically, even if the processes share the same binary.
Hashes are intended to be collision-resistant, even for situations where an adversary controls the byte sequences being hashed. All bits of the Hash result are close to uniformly and independently distributed, so can be safely restricted to a range using bit masking, shifting, or modular arithmetic.
Index ¶
- type Hash
- func (h *Hash) AddByte(b byte)
- func (h *Hash) AddBytes(b []byte)
- func (h *Hash) AddString(s string)
- func (h *Hash) BlockSize() int
- func (h *Hash) Hash() uint64
- func (h *Hash) Reset()
- func (h *Hash) Seed() Seed
- func (h *Hash) SetSeed(seed Seed)
- func (h *Hash) Size() int
- func (h *Hash) Sum(b []byte) []byte
- func (h *Hash) Sum64() uint64
- func (h *Hash) Write(b []byte) (int, error)
- type Seed
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Hash ¶
type Hash struct {
// contains filtered or unexported fields
}
A Hash object is used to compute the hash of a byte sequence.
func New ¶
func New() *Hash
New returns a new Hash object. Different hash objects allocated by this function will very likely have different seeds.
func (*Hash) Hash ¶
Hash returns a value which depends on h's seed and the sequence of bytes added to h (since the last call to Reset or SetSeed).
func (*Hash) Reset ¶
func (h *Hash) Reset()
Reset discards all bytes added to h. (The seed remains the same.)
func (*Hash) Seed ¶
Seed returns the seed value specified in the most recent call to SetSeed, or the initial seed if SetSeed was never called.