Skip to content

Commit a3feda6

Browse files
committed
Use a stable hash function for computing IPC port
Fixes zeromq#1080 The previous code used String.GetHashCode, however in .NET 5 and later this value is not stable across process runs. The value intentionally varies due to "hash randomization", a security feature. In this case, we need a stable value, and we need the same value irrespective of the version of .NET or the platform. To ensure compatibility, we take the algorithm from .NET Framework.
1 parent 0af4d61 commit a3feda6

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/NetMQ/Core/Transports/Ipc/IpcAddress.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void Resolve(string name, bool ip4Only)
3939
{
4040
m_name = name;
4141

42-
int hash = name.GetHashCode();
42+
int hash = Strings.GetStableHashCode(name);
4343
if (hash < 0)
4444
hash = -hash;
4545
hash = hash%55536;

src/NetMQ/Utils/Strings.cs

+37
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,42 @@ internal static class Strings
99

1010
/// <inheritdoc cref="string.IsNullOrWhiteSpace(string)"/>
1111
public static bool IsNullOrWhiteSpace([NotNullWhen(returnValue: false)] string? s) => string.IsNullOrWhiteSpace(s);
12+
13+
/// <summary>
14+
/// Gets a hash value from a string. This hash is stable across process invocations (doesn't use hash randomization) and across different .NET versions and platforms.
15+
/// </summary>
16+
/// <remarks>
17+
/// The original code was taken from <c>string.GetHashCode()</c> with some minor changes
18+
/// https://github.com/microsoft/referencesource/blob/master/mscorlib/system/string.cs
19+
/// </remarks>
20+
public static int GetStableHashCode(string str)
21+
{
22+
int hash1 = 5381;
23+
int hash2 = hash1;
24+
25+
int i = 0;
26+
27+
while (i < str.Length)
28+
{
29+
char c = str[i];
30+
31+
hash1 = ((hash1 << 5) + hash1) ^ c;
32+
33+
i++;
34+
35+
if (i == str.Length)
36+
{
37+
break;
38+
}
39+
40+
c = str[i];
41+
42+
hash2 = ((hash2 << 5) + hash2) ^ c;
43+
44+
i++;
45+
}
46+
47+
return hash1 + (hash2 * 1566083941);
48+
}
1249
}
1350
}

0 commit comments

Comments
 (0)