github.com/GuanceCloud/cliutils@v1.1.21/pkg/hash/fnv1a.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  // Package hash used to calculate hash
     7  package hash
     8  
     9  const (
    10  	// pkg "hash/fnv".
    11  	offset64 = 14695981039346656037
    12  	prime64  = 1099511628211
    13  )
    14  
    15  func Fnv1aNew() uint64 {
    16  	return offset64
    17  }
    18  
    19  func Fnv1aHashAdd(h uint64, s string) uint64 {
    20  	for i := 0; i < len(s); i++ {
    21  		h ^= uint64(s[i])
    22  		h *= prime64
    23  	}
    24  	return h
    25  }
    26  
    27  func Fnv1aHashAddByte(h uint64, s []byte) uint64 {
    28  	for i := 0; i < len(s); i++ {
    29  		h ^= uint64(s[i])
    30  		h *= prime64
    31  	}
    32  	return h
    33  }
    34  
    35  func Fnv1aStrHash(s string) uint64 {
    36  	var h uint64 = offset64
    37  	for i := 0; i < len(s); i++ {
    38  		h ^= uint64(s[i])
    39  		h *= prime64
    40  	}
    41  	return h
    42  }
    43  
    44  func Fnv1aU8Hash(s []byte) uint64 {
    45  	var h uint64 = offset64
    46  	for i := 0; i < len(s); i++ {
    47  		h ^= uint64(s[i])
    48  		h *= prime64
    49  	}
    50  	return h
    51  }
    52  
    53  func Fnv1aHash(v []string) uint64 {
    54  	var h uint64 = offset64
    55  	for _, s := range v {
    56  		for j := 0; j < len(s); j++ {
    57  			h ^= uint64(s[j])
    58  			h *= prime64
    59  		}
    60  	}
    61  	return h
    62  }