1 Star 0 Fork 0

CNCF/devstatscode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
hash.go 1.17 KB
一键复制 编辑 原始数据 按行查看 历史
Lukasz Gryglicki 提交于 2019-03-18 20:33 +08:00 . Fixing package name and tests
package devstatscode
import (
"fmt"
"hash/fnv"
"strconv"
)
// HashStrings - returns unique Hash for strings array
// This value is supposed to be used as ID (negative) to mark it was artificially generated
func HashStrings(strs []string) int {
h := fnv.New64a()
s := ""
for _, str := range strs {
s += str
}
_, _ = h.Write([]byte(s))
res := int(h.Sum64())
if res > 0 {
res *= -1
}
if res == -0x8000000000000000 {
return HashStrings(append(strs, "a"))
}
return res
}
// HashObject takes map[string]interface{} and keys from []string and returns hash string
// from given keys from map
func HashObject(iv map[string]interface{}, keys []string) string {
h := fnv.New64a()
s := ""
for _, key := range keys {
v, ok := iv[key]
if !ok {
Fatalf("HashObject: %+v missing %s key", iv, key)
}
s += fmt.Sprintf("%v", v)
}
_, _ = h.Write([]byte(s))
return strconv.FormatUint(h.Sum64(), 36)
}
// HashArray takes []interface{} and returns hash string
// from given keys from map
func HashArray(ia []interface{}) string {
h := fnv.New64a()
s := ""
for _, iv := range ia {
s += fmt.Sprintf("%v", iv)
}
_, _ = h.Write([]byte(s))
return strconv.FormatUint(h.Sum64(), 36)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cncf/devstatscode.git
git@gitee.com:cncf/devstatscode.git
cncf
devstatscode
devstatscode
master

搜索帮助