xxhash

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2025 License: MIT Imports: 4 Imported by: 2

README

xxhash (Go)

This package provides Go implementations / wrappers for the xxHash family:

  • XXH32 — 32-bit variant (xxhash32.go, xxhash32_asm.go)
  • XXH64 — 64-bit variant (xxhash64.go, xxhash64_asm.go)
  • XXH128 (XXH3/128) — wrapped from zeebo/xxh3 (xxhash128.go)

All code lives under the xxhash package. Tests are provided for validating correctness and incremental behavior.

Import

Use the package path for your project. Example (local repo):

import "github.com/harsh16coder/xxhash"

Provided APIs

XXH32 (32-bit)

Convenience:

  • Sum32(b []byte) uint32

Incremental (hash.Hash32-like):

  • type Digest32
    • New32() *Digest32
    • New32WithSeed(seed uint32) *Digest32
    • (*Digest32) Reset()
    • (*Digest32) ResetWithSeed(seed uint32)
    • (*Digest32) Write(b []byte) (int, error)
    • (*Digest32) Sum(b []byte) []byte // appends big-endian 4 bytes
    • (*Digest32) Sum32() uint32
    • MarshalBinary / UnmarshalBinary

Example:

var h uint32 = xxhash.Sum32([]byte("hello"))
// Incremental:
d := xxhash.New32()
d.Write([]byte("hello "))
d.Write([]byte("world"))
sum := d.Sum32()
XXH64 (64-bit)

Convenience:

  • Sum64(b []byte) uint64

Incremental (hash.Hash64-like):

  • type Digest64
    • New64() *Digest64
    • NewWithSeed64(seed uint64) *Digest64
    • (*Digest64) Reset()
    • (*Digest64) ResetWithSeed(seed uint64)
    • (*Digest64) Write(b []byte) (int, error)
    • (*Digest64) Sum(b []byte) []byte // appends big-endian 8 bytes
    • (*Digest64) Sum64() uint64
    • MarshalBinary / UnmarshalBinary

Example:

// One-shot
sum64 := xxhash.Sum64([]byte("some data"))

// Incremental
d := xxhash.NewWithSeed64(0x1234)
d.Write([]byte("part1"))
d.Write([]byte("part2"))
final := d.Sum64()

Notes:

  • Digest64 stores internal block state and can be marshaled/unmarshaled to persist state.
  • Use Sum(b []byte) when you want the 8-byte big-endian representation appended to a slice.
XXH128 / XXH3 (128-bit)

This package wraps the github.com/zeebo/xxh3 implementation to provide convenient functions:

  • type Uint128 { Hi, Lo uint64 }
  • Sum128(b []byte) Uint128
  • Sum128WithSeed(b []byte, seed uint64) Uint128
  • Sum128String(s string) Uint128
  • Sum128StringWithSeed(s string, seed uint64) Uint128

Uint128 contains two uint64 words; to format as canonical 128-bit hex use:

h := xxhash.Sum128([]byte("hello"))
hex := fmt.Sprintf("%016x%016x", h.Hi, h.Lo)

Use the WithSeed variants to provide a non-zero seed.

Testing

From the repository root (Windows Terminal / PowerShell):

cd "C:\username\projectpath\xxhash\"
go test -v

Run a single package or test with go test ./xxhash -run TestName -v.

The test suite validates:

  • One-shot vs incremental equivalence
  • Deterministic outputs
  • Known test vectors (where provided)

Thread-safety

  • The one-shot convenience functions (Sum32, Sum64, Sum128, etc.) are safe to call concurrently.
  • Digest types (Digest32, Digest64) are stateful and not goroutine-safe — protect with your own synchronization if accessed by multiple goroutines.

Notes / Caveats

  • The XXH128 code in this repo wraps zeebo/xxh3 for correctness and performance. The high/low word order is exposed as Hi and Lo. Format accordingly when producing canonical hex.
  • The package implements marshaling for Digest32/Digest64 to persist intermediate state (useful for streaming workflows).
  • For production use, prefer the convenience functions for simple hashing and the Digest types for incremental hashing across blocks.

Contribution

This project welcomes your PR and issues. For example, refactoring, adding features, correcting English, etc.

If you need any help, you can contact me on X.

Thanks to all the people who already contributed!

License

MIT

Documentation

Overview

Package xxhash implements the 32-bit variant of xxHash (XXH32) as described at https://xxhash.com/.

Package xxhash implements the 64-bit variant of xxHash (XXH64) as described at https://xxhash.com/.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sum32

func Sum32(b []byte) uint32

Sum32 computes the 32-bit xxHash digest of b with a zero seed.

func Sum64

func Sum64(b []byte) uint64

Sum64 computes the 64-bit xxHash digest of b with a zero seed.

Types

type Digest32

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

Digest32 implements hash.Hash32.

Note that a zero-valued Digest32 is not ready to receive writes. Call Reset or create a Digest32 using New32 before calling other methods.

func New32

func New32() *Digest32

New32 creates a new Digest32 with a zero seed.

func New32WithSeed

func New32WithSeed(seed uint32) *Digest32

New32WithSeed creates a new Digest32 with the given seed.

func (*Digest32) BlockSize

func (d *Digest32) BlockSize() int

BlockSize always returns 16 bytes.

func (*Digest32) MarshalBinary

func (d *Digest32) MarshalBinary() ([]byte, error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (*Digest32) Reset

func (d *Digest32) Reset()

Reset clears the Digest32's state so that it can be reused. It uses a seed value of zero.

func (*Digest32) ResetWithSeed

func (d *Digest32) ResetWithSeed(seed uint32)

ResetWithSeed clears the Digest32's state so that it can be reused. It uses the given seed to initialize the state.

func (*Digest32) Size

func (d *Digest32) Size() int

Size always returns 4 bytes.

func (*Digest32) Sum

func (d *Digest32) Sum(b []byte) []byte

Sum appends the current hash to b and returns the resulting slice.

func (*Digest32) Sum32

func (d *Digest32) Sum32() uint32

Sum32 returns the current hash.

func (*Digest32) UnmarshalBinary

func (d *Digest32) UnmarshalBinary(b []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.

func (*Digest32) Write

func (d *Digest32) Write(b []byte) (n int, err error)

Write adds more data to d. It always returns len(b), nil.

type Digest64

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

Digest64 implements hash.Hash64.

Note that a zero-valued Digest64 is not ready to receive writes. Call Reset or create a Digest64 using New before calling other methods.

func New64

func New64() *Digest64

New64 creates a new Digest64 with a zero seed.

func NewWithSeed64

func NewWithSeed64(seed uint64) *Digest64

NewWithSeed64 creates a new Digest64 with the given seed.

func (*Digest64) BlockSize

func (d *Digest64) BlockSize() int

BlockSize always returns 32 bytes.

func (*Digest64) MarshalBinary

func (d *Digest64) MarshalBinary() ([]byte, error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (*Digest64) Reset

func (d *Digest64) Reset()

Reset clears the Digest's state so that it can be reused. It uses a seed value of zero.

func (*Digest64) ResetWithSeed

func (d *Digest64) ResetWithSeed(seed uint64)

ResetWithSeed clears the Digest's state so that it can be reused. It uses the given seed to initialize the state.

func (*Digest64) Size

func (d *Digest64) Size() int

Size always returns 8 bytes.

func (*Digest64) Sum

func (d *Digest64) Sum(b []byte) []byte

Sum appends the current hash to b and returns the resulting slice.

func (*Digest64) Sum64

func (d *Digest64) Sum64() uint64

Sum64 returns the current hash.

func (*Digest64) UnmarshalBinary

func (d *Digest64) UnmarshalBinary(b []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.

func (*Digest64) Write

func (d *Digest64) Write(b []byte) (n int, err error)

Write adds more data to d. It always returns len(b), nil.

type Uint128

type Uint128 struct {
	Hi, Lo uint64
}

func Sum128

func Sum128(b []byte) Uint128

func Sum128String

func Sum128String(s string) Uint128

func Sum128StringWithSeed

func Sum128StringWithSeed(s string, seed uint64) Uint128

func Sum128WithSeed

func Sum128WithSeed(b []byte, seed uint64) Uint128

Jump to

Keyboard shortcuts

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