Golang hash.Hash32 type examples

package hash

Hash32 is the common interface implemented by all 32-bit hash functions.

Golang hash.Hash32 type usage examples

Example 1:

 type marvin struct {
 seed uint64
 lo, hi uint32
 t [4]byte // as-yet-unprocessed bytes
 rem int // how many bytes in t[] are valid
 }

 // NewMarvin32 returns a new hash.Hash32 object computing Microsoft's InternalMarvin32HashString seeded hash.
 func NewMarvin32(seed uint64) hash.Hash32 {
 m := new(marvin)
 m.seed = seed
 m.Reset()
 return m
 }

Example 2:

 package main

 import (
 "fmt"
 "hash"
 )

 // demo for hash.Hash32 purpose.
 // ** -- you need to write your own Write(), reset(), Size, Blocksize and Sum32 inner functions -- **

 // fakeHash32 is a dummy Hash32 that always returns 0.
 type fakeHash32 struct {
 hash.Hash32
 }

 func (fakeHash32) Write(p []byte) (int, error) { return len(p), nil }
 func (fakeHash32) Sum32() uint32 { return 0 }

 func main() {

 var h hash.Hash32 = fakeHash32{}

 h.Write([]byte("abc123")) // just a dummy Write

 fmt.Printf("%d \n", h.Sum32()) // will return 0

 }

References :

https://github.com/dgryski/dgohash/blob/master/marvin.go

http://golang.org/pkg/hash/#Hash32

Advertisement