mirror of
https://github.com/PeernetOfficial/Abstraction.git
synced 2026-07-22 13:17:50 +01:00
56 lines
864 B
Go
56 lines
864 B
Go
package hash
|
|
|
|
import (
|
|
"math/bits"
|
|
)
|
|
|
|
const (
|
|
c1 uint32 = 0xcc9e2d51
|
|
c2 uint32 = 0x1b873593
|
|
)
|
|
|
|
// Sum32WithSeed is a port of MurmurHash3_x86_32 function.
|
|
func Sum32WithSeed(data []byte, seed uint32) uint32 {
|
|
h1 := seed
|
|
dlen := len(data)
|
|
|
|
for len(data) >= 4 {
|
|
k1 := uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16 | uint32(data[3])<<24
|
|
data = data[4:]
|
|
|
|
k1 *= c1
|
|
k1 = bits.RotateLeft32(k1, 15)
|
|
k1 *= c2
|
|
|
|
h1 ^= k1
|
|
h1 = bits.RotateLeft32(h1, 13)
|
|
h1 = h1*5 + 0xe6546b64
|
|
}
|
|
|
|
var k1 uint32
|
|
switch len(data) {
|
|
case 3:
|
|
k1 ^= uint32(data[2]) << 16
|
|
fallthrough
|
|
case 2:
|
|
k1 ^= uint32(data[1]) << 8
|
|
fallthrough
|
|
case 1:
|
|
k1 ^= uint32(data[0])
|
|
k1 *= c1
|
|
k1 = bits.RotateLeft32(k1, 15)
|
|
k1 *= c2
|
|
h1 ^= k1
|
|
}
|
|
|
|
h1 ^= uint32(dlen)
|
|
|
|
h1 ^= h1 >> 16
|
|
h1 *= 0x85ebca6b
|
|
h1 ^= h1 >> 13
|
|
h1 *= 0xc2b2ae35
|
|
h1 ^= h1 >> 16
|
|
|
|
return h1
|
|
}
|