|
3 | 3 | // See the LICENSE file in the project root for more information |
4 | 4 |
|
5 | 5 | using System.Collections; |
| 6 | +using System.Security.Cryptography; |
6 | 7 | using System.Text; |
7 | 8 |
|
8 | 9 | namespace Elastic.Documentation.LegacyPageLookup; |
@@ -89,39 +90,17 @@ public bool Check(string item) |
89 | 90 | } |
90 | 91 |
|
91 | 92 | /// <summary> |
92 | | - /// Hashes the input data using Murmur3 with a given seed. |
| 93 | + /// Hashes the input data using SHA256 with a given seed. |
93 | 94 | /// </summary> |
94 | 95 | private int GetHash(byte[] data, int seed) |
95 | 96 | { |
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); |
125 | 104 | } |
126 | 105 |
|
127 | 106 | /// <summary> |
|
0 commit comments