本機庫具有 FNV-1 哈希算法https://golang.org/pkg/hash/fnv/返回 uint64 值(范圍:0 到 18446744073709551615)。我需要將此值存儲在 PostgreSQL bigserial 中,但它的范圍是 1 到 9223372036854775807??梢詫⒐4笮「臑槔纭?6?http://www.isthe.com/chongo/tech/comp/fnv/index.html#xor-fold有人可以幫助更改本機算法以生成 56 位哈希嗎? https://golang.org/src/hash/fnv/fnv.go更新我自己使用這個文檔http://www.isthe.com/chongo/tech/comp/fnv/index.html#xor-foldpackage mainimport ( "fmt" "hash/fnv")func main() { const MASK uint64 = 1<<63 - 1 h := fnv.New64() h.Write([]byte("1133")) hash := h.Sum64() fmt.Printf("%#x\n", MASK) fmt.Println(hash) hash = (hash >> 63) ^ (hash & MASK) fmt.Println(hash)}http://play.golang.org/p/j7q3D73qqu這是正確的嗎?
1 回答

catspeake
TA貢獻1111條經驗 獲得超0個贊
這是正確的嗎?
是的,這是對 63 位的正確 XOR 折疊。但是有一個更簡單的方法:
hash = hash % 9223372036854775808
XOR 折疊的分布是可疑的,可能在某處得到了證明,但不是很明顯。然而,Modulo 顯然是將散列算法的分布包裝到一個較小的 codomain。
- 1 回答
- 0 關注
- 417 瀏覽
添加回答
舉報
0/150
提交
取消