Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

murmur3 source pure

Package murmur3 implements the MurmurHash3 hash algorithm.

Readme View source

MurmurHash3 Package

Package implements Austin Appleby's MurmurHash3 algorithm.

MurmurHash3 is a fast, non-cryptographic hash function well suited for hash tables, bloom filters, data partitioning, and deduplication where speed and good distribution matter more than cryptographic security.

Repository can be found at jeronimoalbi/gnome, as part of jeronimoalbi's Gno smart contracts monorepo.

Usage

 1package main
 2
 3import "gno.land/p/g17khqpukees4237dtn3astzapmp462vjhsz6st4/murmur3"
 4
 5func main() {
 6	data := []byte("Hello, world!")
 7	seed := uint32(42)
 8
 9	// Hash32 without seed
10	h32 := murmur3.New32()
11	h32.Write(data)
12	sum32 := uint64(h32.Sum32())
13	println(murmur3.EncodeToString(sum32))
14
15	// Hash32 with seed
16	h32 = murmur3.NewWithSeed32(seed)
17	h32.Write(data)
18	sum32 = uint64(h32.Sum32())
19	println(murmur3.EncodeToString(sum32))
20
21	// Hash32 without seed using a helper function
22	sum32 = uint64(murmur3.Sum32(data))
23	println(murmur3.EncodeToString(sum32))
24
25	// Hash32 with seed using a helper function
26	sum32 = uint64(murmur3.Sum32WithSeed(data, seed))
27	println(murmur3.EncodeToString(sum32))
28
29	// Hash64 without seed
30	h64 := murmur3.New64()
31	h64.Write(data)
32	sum64 := h64.Sum64()
33	println(murmur3.EncodeToString(sum64))
34
35	// Hash64 with seed
36	h64 = murmur3.NewWithSeed64(0, seed)
37	h64.Write(data)
38	sum64 = h64.Sum64()
39	println(murmur3.EncodeToString(sum64))
40
41	// Hash64 without seed using a helper function
42	sum64 = murmur3.Sum64(data)
43	println(murmur3.EncodeToString(sum64))
44
45	// Hash64 with seed using a helper function
46	sum64 = murmur3.Sum64WithSeed(data, 0, seed)
47	println(murmur3.EncodeToString(sum64))
48}
49
50// Output:
51// c0363e43
52// 2c8c8533
53// c0363e43
54// 2c8c8533
55// c0363e43aa5dc85b
56// c0363e432c8c8533
57// c0363e43aa5dc85b
58// c0363e432c8c8533

Overview

Package murmur3 implements the MurmurHash3 hash algorithm.

Package implements Austin Appleby's MurmurHash3 algorithm.

MurmurHash3 is a fast, non-cryptographic hash function well suited for hash tables, bloom filters, data partitioning, and deduplication where speed and good distribution matter more than cryptographic security.

References:

Example
1https://en.wikipedia.org/wiki/MurmurHash#Algorithm
2https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp

Functions 9

func EncodeToString

1func EncodeToString(sum uint64) string
source

EncodeToString returns the hexadecimal encoding of a hash value.

func New32

1func New32() hash.Hash32
source

New32 returns a new hash.Hash32 computing the MurmurHash3 hash. It can generate 2 to the power of 32 unique 32-bit values ranging from 0 to 4_294_967_296. Hash is not collision safe, so it's recommended to handle potential collisions when dealing with more than 65_536 values which is when probability raises.

func New64

1func New64() hash.Hash64
source

New64 returns a new hash.Hash64 computing a 64-bit MurmurHash3 hash using two 32-bit hashes with different seeds. It can generate 2 to the power of 64 unique 64-bit values. Hash is not collision safe, so it's recommended to handle potential collisions when dealing with more than 4_294_967_296 values which is when probability raises.

func NewWithSeed32

1func NewWithSeed32(seed uint32) hash.Hash32
source

NewWithSeed32 returns a new hash.Hash32 computing the MurmurHash3 hash with the given seed. It can generate 2 to the power of 32 unique 32-bit values ranging from 0 to 4_294_967_296. Hash is not collision safe, so it's recommended to handle potential collisions when dealing with more than 65_536 values which is when provability raises.

func NewWithSeed64

1func NewWithSeed64(seed1, seed2 uint32) hash.Hash64
source

NewWithSeed64 returns a new hash.Hash64 computing a 64-bit MurmurHash3 hash. The first 32-bit hash uses seed1 and the second uses seed2. The two seeds should ideally be different. It can generate 2 to the power of 64 unique 64-bit values. Hash is not collision safe, so it's recommended to handle potential collisions when dealing with more than 4_294_967_296 values which is when provability raises.

func Sum32

1func Sum32(data []byte) uint32
source

Sum32 returns the MurmurHash3 hash of the specified data.

func Sum32WithSeed

1func Sum32WithSeed(data []byte, seed uint32) uint32
source

Sum32WithSeed returns the MurmurHash3 hash of the specified data using the given seed.

func Sum64

1func Sum64(data []byte) uint64
source

Sum64 returns the 64-bit MurmurHash3 hash of the specified data.

func Sum64WithSeed

1func Sum64WithSeed(data []byte, seed1, seed2 uint32) uint64
source

Sum64WithSeed returns the 64-bit MurmurHash3 hash of the specified data using the given seeds.

Imports 3

  • hash stdlib
  • math/bits stdlib
  • strconv stdlib

Source Files 5