-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathMurMurHash3.cs
More file actions
88 lines (79 loc) · 2.6 KB
/
MurMurHash3.cs
File metadata and controls
88 lines (79 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System.Text;
namespace TotkRandomizer
{
public static class MurMurHash3
{
private const uint Seed = 0;
public static uint Hash(string s)
{
var ss = new MemoryStream(Encoding.UTF8.GetBytes(s));
return Hash(ss);
}
public static uint Hash(Stream stream)
{
const uint c1 = 0xcc9e2d51;
const uint c2 = 0x1b873593;
var h1 = Seed;
uint streamLength = 0;
using (var reader = new BinaryReader(stream))
{
var chunk = reader.ReadBytes(4);
while (chunk.Length > 0)
{
streamLength += (uint)chunk.Length;
uint k1;
switch (chunk.Length)
{
case 4:
k1 = (uint)(chunk[0] | chunk[1] << 8 | chunk[2] << 16 | chunk[3] << 24);
k1 *= c1;
k1 = Rot(k1, 15);
k1 *= c2;
h1 ^= k1;
h1 = Rot(h1, 13);
h1 = h1 * 5 + 0xe6546b64;
break;
case 3:
k1 = (uint)(chunk[0] | chunk[1] << 8 | chunk[2] << 16);
k1 *= c1;
k1 = Rot(k1, 15);
k1 *= c2;
h1 ^= k1;
break;
case 2:
k1 = (uint)(chunk[0] | chunk[1] << 8);
k1 *= c1;
k1 = Rot(k1, 15);
k1 *= c2;
h1 ^= k1;
break;
case 1:
k1 = (chunk[0]);
k1 *= c1;
k1 = Rot(k1, 15);
k1 *= c2;
h1 ^= k1;
break;
}
chunk = reader.ReadBytes(4);
}
}
h1 ^= streamLength;
h1 = Mix(h1);
return h1;
}
private static uint Rot(uint x, byte r)
{
return (x << r) | (x >> (32 - r));
}
private static uint Mix(uint h)
{
h ^= h >> 16;
h *= 0x85ebca6b;
h ^= h >> 13;
h *= 0xc2b2ae35;
h ^= h >> 16;
return h;
}
}
}