Search Apps Documentation Source Content File Folder Download Copy Actions Download

/p/jeronimoalbi/murmur3

Directory · 8 Files
README.md Open

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.

API

  • New32() — Returns a hash.Hash32 with seed zero
  • NewWithSeed32(seed) — Returns a hash.Hash32 with the given seed
  • Sum32(data) — One-shot hash with seed zero
  • Sum32WithSeed(data, seed) — One-shot hash with the given seed
  • New64() — Returns a hash.Hash64 using seeds zero and one
  • NewWithSeed64(seed1, seed2) — Returns a hash.Hash64 with the given seeds
  • Sum64(data) — One-shot 64-bit hash using seeds zero and one
  • Sum64WithSeed(data, seed1, seed2) — One-shot 64-bit hash with the given seeds
  • EncodeToString(sum64) — Returns the hexadecimal encoding of a hash value

Usage

 1package main
 2
 3import "gno.land/p/jeronimoalbi/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