Skip to content

Commit d4ce8f4

Browse files
committed
Update Murmur3 to use cryptographically secure seed
Changed the Seed field from a constant to a runtime-generated value using a new GenerateSeed() method with RandomNumberGenerator. Added System.Security.Cryptography import for secure random seed generation, supporting both .NET 6.0+ and earlier versions.
1 parent 065249c commit d4ce8f4

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

Clojure/Clojure/Lib/Murmur3.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Collections;
1717
using System.Collections.Generic;
1818
using System.Linq;
19+
using System.Security.Cryptography;
1920
using System.Text;
2021

2122
namespace clojure.lang
@@ -37,7 +38,7 @@ public static class Murmur3
3738
{
3839
#region Data
3940

40-
const int Seed = 0;
41+
static readonly uint Seed = GenerateSeed();
4142
const uint C1 = 0xcc9e2d51;
4243
const uint C2 = 0x1b873593;
4344
const int R1 = 15;
@@ -220,6 +221,18 @@ private static uint RotateLeft(uint x, int n)
220221
{
221222
return (x << n) | (x >> (32 - n));
222223
}
224+
225+
private static uint GenerateSeed()
226+
{
227+
#if NET6_0_OR_GREATER
228+
return unchecked((uint)RandomNumberGenerator.GetInt32(int.MinValue, int.MaxValue));
229+
#else
230+
byte[] bytes = new byte[4];
231+
using (var rng = RandomNumberGenerator.Create())
232+
rng.GetBytes(bytes);
233+
return BitConverter.ToUInt32(bytes, 0);
234+
#endif
235+
}
223236
#endregion
224237
}
225238
}

0 commit comments

Comments
 (0)