hash

package
v0.0.0-...-f89cf20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2020 License: MIT Imports: 1 Imported by: 0

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

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) AddByte

func (h *Hash) AddByte(b byte)

AddByte adds b to the sequence of bytes hashed by h.

func (*Hash) AddBytes

func (h *Hash) AddBytes(b []byte)

AddBytes adds b to the sequence of bytes hashed by h.

func (*Hash) AddString

func (h *Hash) AddString(s string)

AddString adds the bytes of s to the sequence of bytes hashed by h.

func (*Hash) BlockSize

func (h *Hash) BlockSize() int

func (*Hash) Hash

func (h *Hash) Hash() uint64

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

func (h *Hash) Seed() Seed

Seed returns the seed value specified in the most recent call to SetSeed, or the initial seed if SetSeed was never called.

func (*Hash) SetSeed

func (h *Hash) SetSeed(seed Seed)

SetSeed sets the seed used by h. Two Hash objects with the same seed in the same process will behave identically. Two Hash objects with different seeds will very likely behave differently. Any bytes added to h previous to this call will be discarded.

func (*Hash) Size

func (h *Hash) Size() int

func (*Hash) Sum

func (h *Hash) Sum(b []byte) []byte

func (*Hash) Sum64

func (h *Hash) Sum64() uint64

func (*Hash) Write

func (h *Hash) Write(b []byte) (int, error)

type Seed

type Seed struct {
	// contains filtered or unexported fields
}

A Seed controls the behavior of a Hash. Two Hash objects with the same seed in the same process will behave identically. Two Hash objects with different seeds will very likely behave differently.

func MakeSeed

func MakeSeed(s uint64) Seed

MakeSeed returns a Seed initialized using the bits in s. Two seeds generated with the same s are guaranteed to be equal. Two seeds generated with different s are very likely to be different. TODO: disallow this? See Alan's comment in the issue.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL