goconsistencehash/encrytor.go
2025-05-07 10:34:10 +08:00

33 lines
476 B
Go

package consistencehash
import (
"hash"
"math"
"github.com/spaolacci/murmur3"
)
type Encrytor interface {
Hash(string) int32
}
type EncrytorImp struct {
hash hash.Hash32
}
func NewEncrytorImp(hash hash.Hash32) *EncrytorImp {
return &EncrytorImp{
hash: hash,
}
}
func (e *EncrytorImp) Hash(id string) int32 {
e.hash = murmur3.New32()
// e.hash.Reset()
e.hash.Write([]byte(id))
return int32(e.hash.Sum32() % math.MaxInt32)
}
var _ Encrytor = &EncrytorImp{}