Skip to content

Commit 40895b7

Browse files
authored
Merge pull request #1122 from drewnoakes/fix-1080-stable-hash
Use a stable hash function for computing IPC port
2 parents 0af4d61 + a3feda6 commit 40895b7

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 37 additions & 0 deletions
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)