gitee.com/h79/goutils@v1.22.10/common/algorithm/hash.go (about)

     1  package algorithm
     2  
     3  import "hash/fnv"
     4  
     5  const (
     6  	// offset64 FNVa offset basis. See https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function#FNV-1a_hash
     7  	offset64 = 14695981039346656037
     8  	// prime64 FNVa prime value. See https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function#FNV-1a_hash
     9  	prime64 = 1099511628211
    10  )
    11  
    12  // FnvSum64 gets the string and returns its uint64 hash value.
    13  func FnvSum64(key string) uint64 {
    14  	var hash uint64 = offset64
    15  	for i := 0; i < len(key); i++ {
    16  		hash ^= uint64(key[i])
    17  		hash *= prime64
    18  	}
    19  	return hash
    20  }
    21  
    22  func HashCode(key string) uint32 {
    23  	h := fnv.New32a()
    24  	_, _ = h.Write([]byte(key))
    25  	return h.Sum32()
    26  }