Skip to content

Commit e36a293

Browse files
committed
Refactor GetHash method
1 parent 721d826 commit e36a293

File tree

2 files changed

+9
-30
lines changed

2 files changed

+9
-30
lines changed

src/Elastic.Documentation.LegacyPageLookup/BloomFilter.cs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information
44

55
using System.Collections;
6+
using System.Security.Cryptography;
67
using System.Text;
78

89
namespace Elastic.Documentation.LegacyPageLookup;
@@ -89,39 +90,17 @@ public bool Check(string item)
8990
}
9091

9192
/// <summary>
92-
/// Hashes the input data using Murmur3 with a given seed.
93+
/// Hashes the input data using SHA256 with a given seed.
9394
/// </summary>
9495
private int GetHash(byte[] data, int seed)
9596
{
96-
// Using a seeded MurmurHash3 implementation for good distribution.
97-
// A simple GetHashCode() is not sufficient as it can change between versions/processes.
98-
// Here we use a custom implementation detail for demonstration.
99-
// In a real-world scenario, you might use a library like System.IO.Hashing.MurmurHash3.
100-
const uint c1 = 0xcc9e2d51;
101-
const uint c2 = 0x1b873593;
102-
const int r1 = 15;
103-
const int m = 5;
104-
const uint n = 0xe6546b64;
105-
106-
var hash = (uint)seed;
107-
var length = data.Length;
108-
var nblocks = length / 4;
109-
110-
for (var i = 0; i < nblocks; i++)
111-
{
112-
var k = BitConverter.ToUInt32(data, i * 4);
113-
k *= c1;
114-
k = (k << r1) | (k >> (32 - r1));
115-
k *= c2;
116-
117-
hash ^= k;
118-
hash = (hash << 13) | (hash >> 19);
119-
hash = (hash * m) + n;
120-
}
121-
122-
// This is a simplified hash generation. A full implementation would handle the tail.
123-
// For our purpose, this is sufficient to generate distinct hashes per seed.
124-
return (int)(Math.Abs(hash) % _bitArray.Length);
97+
var seedBytes = BitConverter.GetBytes(seed);
98+
var combinedBytes = new byte[data.Length + seedBytes.Length];
99+
Buffer.BlockCopy(data, 0, combinedBytes, 0, data.Length);
100+
Buffer.BlockCopy(seedBytes, 0, combinedBytes, data.Length, seedBytes.Length);
101+
var hashBytes = SHA256.HashData(combinedBytes);
102+
var hashInt = BitConverter.ToInt32(hashBytes, 0);
103+
return Math.Abs(hashInt % _bitArray.Length);
125104
}
126105

127106
/// <summary>
Binary file not shown.

0 commit comments

Comments
 (0)