Go · Hashing and Checksums

Hashing and Checksums

import "github.com/ashvardanian/stringzilla/golang" 6.0.0-rc.1 · 91a27bdd

A fast 64-bit additive checksum and a seeded 64-bit non-cryptographic hash:

func Bytesum(str string) uint64
func Hash(str string, seed uint64) uint64
sum := sz.Bytesum("hello")
h := sz.Hash("hello", 42)

For incremental hashing, Hasher implements hash.Hash64 and io.Writer:

func NewHasher(seed uint64) *Hasher
func (h *Hasher) Write(p []byte) (n int, err error)
func (h *Hasher) Sum(b []byte) []byte
func (h *Hasher) Sum64() uint64
func (h *Hasher) Digest() uint64
func (h *Hasher) Reset()
func (h *Hasher) Size() int
func (h *Hasher) BlockSize() int

Sum64 and its alias Digest return the current digest without consuming the state. Create a Hasher with NewHasher(seed); its zero value is not ready to use.

h := sz.NewHasher(0)
h.Write([]byte("hello "))
h.Write([]byte("world"))
digest := h.Sum64()

§ SHA-256

A cryptographic SHA-256 is available both as a one-shot and as a streaming hasher implementing hash.Hash and io.Writer:

func HashSha256(data []byte) [32]byte

func NewSha256() *Sha256
func (h *Sha256) Write(p []byte) (n int, err error)
func (h *Sha256) Sum(b []byte) []byte
func (h *Sha256) Digest() [32]byte
func (h *Sha256) Hexdigest() string
func (h *Sha256) Reset()
func (h *Sha256) Size() int
func (h *Sha256) BlockSize() int

Digest returns the raw 32-byte hash and Hexdigest returns its lowercase hex string, both without consuming the state. Create a Sha256 with NewSha256(); its zero value is not ready to use.

sum := sz.HashSha256([]byte("hello"))

h := sz.NewSha256()
h.Write([]byte("hello"))
fmt.Println(h.Hexdigest())

API

StructHasherHasher is a streaming 64-bit non-cryptographic hasher that implements hash.Hash64 and io.Writer.7
hash.go:36 ↗
type Hasher struct {
    // contains filtered or unexported fields
}

Create one with NewHasher; the zero value is not ready to use.

NewHasherNewHasher creates a new streaming hasher with the given seed.
hash.go:45 ↗
func NewHasher(seed uint64) *Hasher
Block*BlockSize× 2BlockSize returns the hash's underlying block size.
hash.go:82 ↗
func (h *Hasher) BlockBlockSize() int

Implements hash.Hash.

*× 2Digest returns the current 64-bit hash without consuming the state.
hash.go:95 ↗
func (h *Hasher) Digest() uint64
Symbol
Digest
Sum64

This is an alias for Hasher.Sum64.

Implements hash.Hash64.

*Reset× 2Reset resets the hasher to its initial state.
hash.go:72 ↗
func (h *Hasher) Reset()

Implements hash.Hash.

*Size× 2Size returns the number of bytes Sum will return.
hash.go:77 ↗
func (h *Hasher) Size() int

Implements hash.Hash.

*Sum× 2Sum appends the current hash to b and returns the resulting slice.
hash.go:64 ↗
func (h *Hasher) Sum(b []byte) []byte

It does not change the underlying hash state. Implements hash.Hash.

WriteWrite adds data to the streaming hasher.
hash.go:52 ↗
func (h *Hasher) Write(p []byte) (n int, err error)

Implements io.Writer.

StructSha256Sha256 is a streaming SHA-256 hasher that implements hash.Hash and io.Writer.4
sha256.go:32 ↗
type Sha256 struct {
    // contains filtered or unexported fields
}

Create one with NewSha256; the zero value is not ready to use.

NewSha256NewSha256 creates a new streaming SHA-256 hasher.
sha256.go:40 ↗
func NewSha256() *Sha256
DigestDigest returns the current SHA-256 hash as a 32-byte array without consuming the state.
sha256.go:81 ↗
func (h *Sha256) Digest() [32]byte

This is a convenience method in addition to the standard hash.Hash interface.

HexdigestHexdigest returns the current SHA-256 hash as a lowercase hexadecimal string.
sha256.go:89 ↗
func (h *Sha256) Hexdigest() string

This is a convenience wrapper over Sha256.Digest.

WriteWrite adds data to the streaming SHA-256 hasher.
sha256.go:47 ↗
func (h *Sha256) Write(p []byte) (n int, err error)

Implements io.Writer.

Functions

BytesumBytesum computes a simple 64-bit checksum by summing bytes.
sz.go:17 ↗
func Bytesum(str string) uint64
HashHash computes a 64-bit non-cryptographic hash with a seed.
sz.go:26 ↗
func Hash(str string, seed uint64) uint64
HashSha256HashSha256 computes the SHA-256 cryptographic hash of the input data.
sz.go:18 ↗
func HashSha256(data []byte) [32]byte