xxh3 Go Port
repository·master·Indexed 20 days ago
https://github.com/zeebo/xxh3A high-performance Go port of the xxHash XXH3 algorithm, matching the output of the upstream library as of v0.8.0. It provides 64-bit and 128-bit hashing capabilities for byte slices and strings, including support for seeded hashing. The library includes Hasher and Hasher128 types that implement the standard Go hash.Hash and hash.Hash64 interfaces.
What's inside xxh3
- XXH3 is a Go port of the xxh3 library. This implementation matches the output of the upstream library (as of v0.8.0). It provides high-performance hashing with support for 64-bit and 128-bit variants, as well as seeded hashing.
Use Hasher128 to implement hash.Hash
masterThe
Hasher128struct implements the standard libraryhash.Hashinterface. This allows you to use it in any Go function or library that expects a standard hash implementation.Key interface methods implemented:
Size() int: Returns16(the number of bytes in a 128-bit hash).BlockSize() int: Returns the underlying block size. For optimal performance, try to write data in multiples of this size.Reset(): Resets the hasher to its initial state.Write(p []byte) (n int, err error): Adds data to the hash state. It never returns an error.Sum(b []byte) []byte: Appends the current 16-byte hash to the provided sliceband returns it.
Hash a byte slice with Hash()
masterUse the
Hashfunction to compute a 64-bit XXH3 hash of a byte slice ([]byte). This is the primary way to hash raw binary data.import "github.com/zeebo/zeebo/xxh3" hash := xxh3.Hash([]byte("example data"))Compute 128-bit hashes with Hash128 and HashString128
masterThe
xxh3package provides two primary functions for computing 128-bit XXH3 hashes. UseHash128when working with byte slices ([]byte) andHashString128when working with strings. Both functions return aUint128type, which represents the resulting 128-bit hash value.import "github.com/zeebo/zeebo/xxh3" // Hashing a byte slice data := []byte("example data") hashBytes := xxh3.Hash128(data) // Hashing a string text := "example string" hashString := xxh3.HashString128(text)Hash a string with a seed using HashStringSeed
masterUse
HashStringSeedto compute a 64-bit XXH3 hash of a string using a provided 64-bit seed. This provides a direct way to hash string data without manual conversion to a byte slice.import "github.com/zeebo/zeebo/xxh3" input := "example string" seed := uint64(12345) hash := xxh3.HashStringSeed(input, seed)Compute 128-bit hashes with a seed
masterThe
xxh3package provides functions to compute 128-bit hashes for byte slices and strings using auint64seed. These functions return aUint128type, which represents the 128-bit hash result.import "zeebo/zeebo/xxh3" // For byte slices hash := xxh3.Hash128Seed(data, seed) // For strings hash := xxh3.HashString128Seed("my string", seed)Initialize a new Hasher
masterYou can create a new
Hasherinstance usingNew()for default hashing orNewSeed(seed uint64)to use a specific seed. TheHasherimplements the standard Gohash.Hashandhash.Hash64interfaces, making it compatible with most Go standard library functions that expect a hasher.import "github.com/zeebo/zeebo/xxh3" // Default hasher h := xxh3.New() // Hasher with a specific seed h := xxh3.NewSeed(12345)Retrieve the 128-bit hash result
masterThere are two ways to retrieve the hash result from a
Hasher128:Sum(b []byte) []byte: This follows thehash.Hashpattern. It appends the 16-byte hash to the sliceband returns the resulting slice. It does not modify the internal state of the hasher.Sum128() Uint128: Returns the 128-bit hash as aUint128type.
Hash a byte slice with a seed using HashSeed
masterUse
HashSeedto compute a 64-bit XXH3 hash of a byte slice ([]byte) using a provided 64-bit seed. This is useful for randomized hashing or creating multiple hash values for the same input data.import "github.com/zeebo/zeebo/xxh3" data := []byte("example data") seed := uint64(12345) hash := xxh3.HashSeed(data, seed)Initialize a 128-bit hasher with New128 or NewSeed128
masterTo perform 128-bit hashing, use the
Hasher128type which implements the standard Gohash.Hashinterface.- Use
New128()to create a new hasher with the default seed. - Use
NewSeed128(seed uint64)to create a new hasher initialized with a specificuint64seed.
Hasher128provides methods to write data viaWrite([]byte)orWriteString(string), and retrieve the result viaSum(b []byte)orSum128().import "github.com/zeebo/xxh3" // Default seed h := xxh3.New128() h.Write([]byte("data")) sum := h.Sum(nil) // Custom seed hSeed := xxh3.NewSeed128(42) hSeed.WriteString("more data") sumSeed := hSeed.Sum128()- Use
Reset a Hasher state
masterUse
Reset()to clear the current hash state and return the hasher to its initial state. If you need to change the seed while resetting, useResetSeed(seed uint64). Note thatResetSeedwill update the internal seed used for subsequent operations.h := xxh3.NewSeed(1) h.Write([]byte("some data")) // Reset to default state (seed 0) h.Reset() // Reset with a new seed h.ResetSeed(2)Reset or change the seed of a Hasher128
masterYou can reset a
Hasher128instance to its initial state or change its seed using the following methods:Reset(): Returns the hasher to its initial state (using the current seed).ResetSeed(seed uint64): Resets the hash and sets a new seed. Note that this changes the seed used by subsequent calls toReset().