xxh3 Go Port

repository·master·Indexed 20 days ago

https://github.com/zeebo/xxh3

A 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.

Tokens
2.5K
Snippets
15
Records
21
Agent score
69%

What's inside xxh3

  1. Overview of XXH3 Go port

    master
    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.
  2. Use Hasher128 to implement hash.Hash

    master

    The Hasher128 struct implements the standard library hash.Hash interface. This allows you to use it in any Go function or library that expects a standard hash implementation.

    Key interface methods implemented:

    • Size() int: Returns 16 (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 slice b and returns it.
  3. Hash a byte slice with Hash()

    master

    Use the Hash function 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"))
  4. Compute 128-bit hashes with Hash128 and HashString128

    master

    The xxh3 package provides two primary functions for computing 128-bit XXH3 hashes. Use Hash128 when working with byte slices ([]byte) and HashString128 when working with strings. Both functions return a Uint128 type, 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)
  5. Hash a string with a seed using HashStringSeed

    master

    Use HashStringSeed to 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)
  6. Compute 128-bit hashes with a seed

    master

    The xxh3 package provides functions to compute 128-bit hashes for byte slices and strings using a uint64 seed. These functions return a Uint128 type, 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)
  7. Initialize a new Hasher

    master

    You can create a new Hasher instance using New() for default hashing or NewSeed(seed uint64) to use a specific seed. The Hasher implements the standard Go hash.Hash and hash.Hash64 interfaces, 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)
  8. Retrieve the 128-bit hash result

    master

    There are two ways to retrieve the hash result from a Hasher128:

    1. Sum(b []byte) []byte: This follows the hash.Hash pattern. It appends the 16-byte hash to the slice b and returns the resulting slice. It does not modify the internal state of the hasher.
    2. Sum128() Uint128: Returns the 128-bit hash as a Uint128 type.
  9. Hash a byte slice with a seed using HashSeed

    master

    Use HashSeed to 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)
  10. Initialize a 128-bit hasher with New128 or NewSeed128

    master

    To perform 128-bit hashing, use the Hasher128 type which implements the standard Go hash.Hash interface.

    • Use New128() to create a new hasher with the default seed.
    • Use NewSeed128(seed uint64) to create a new hasher initialized with a specific uint64 seed.

    Hasher128 provides methods to write data via Write([]byte) or WriteString(string), and retrieve the result via Sum(b []byte) or Sum128().

    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()
  11. Reset a Hasher state

    master

    Use Reset() to clear the current hash state and return the hasher to its initial state. If you need to change the seed while resetting, use ResetSeed(seed uint64). Note that ResetSeed will 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)
  12. Reset or change the seed of a Hasher128

    master

    You can reset a Hasher128 instance 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 to Reset().