Skip to content

Commit 0b1146e

Browse files
committed
Added public primitive hasher class
1 parent 231c60d commit 0b1146e

File tree

3 files changed

+68
-20
lines changed

3 files changed

+68
-20
lines changed

MLAPI/Data/Cache.cs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
using System.Collections.Generic;
2-
using System.Security.Cryptography;
3-
using System.Text;
1+
using MLAPI.NetworkingManagerComponents.Binary;
2+
using System.Collections.Generic;
43

54
namespace MLAPI.Data
65
{
@@ -14,15 +13,10 @@ internal static ulong GetMessageAttributeHash(string name)
1413
if (messageAttributeHashes.ContainsKey(name))
1514
return messageAttributeHashes[name];
1615

17-
using (SHA256Managed sha = new SHA256Managed())
18-
{
19-
byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(name));
20-
ulong value = hash[0] | ((ulong)hash[1] << 8) | ((ulong)hash[2] << 16) | ((ulong)hash[3] << 24) | ((ulong)hash[4] << 32) | ((ulong)hash[5] << 40) | ((ulong)hash[6] << 48) | ((ulong)hash[7] << 56);
21-
//ulong value = hash[0] | ((uint)hash[1] << 8) | ((uint)hash[2] << 16) | ((uint)hash[3] << 24);
22-
messageAttributeHashes.Add(name, value);
23-
messageAttributeNames.Add(value, name);
24-
return value;
25-
}
16+
ulong value = PrimitiveHasher.GetULongHash(name);
17+
messageAttributeHashes.Add(name, value);
18+
messageAttributeNames.Add(value, name);
19+
return value;
2620
}
2721

2822
internal static string GetAttributeMethodName(ulong hash)
@@ -38,14 +32,9 @@ internal static void RegisterMessageAttributeName(string name)
3832
if (messageAttributeHashes.ContainsKey(name))
3933
return;
4034

41-
using (SHA256Managed sha = new SHA256Managed())
42-
{
43-
byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(name));
44-
ulong value = hash[0] | ((ulong)hash[1] << 8) | ((ulong)hash[2] << 16) | ((ulong)hash[3] << 24) | ((ulong)hash[4] << 32) | ((ulong)hash[5] << 40) | ((ulong)hash[6] << 48) | ((ulong)hash[7] << 56);
45-
//ulong value = hash[0] | ((uint)hash[1] << 8) | ((uint)hash[2] << 16) | ((uint)hash[3] << 24);
46-
messageAttributeHashes.Add(name, value);
47-
messageAttributeNames.Add(value, name);
48-
}
35+
ulong value = PrimitiveHasher.GetULongHash(name);
36+
messageAttributeHashes.Add(name, value);
37+
messageAttributeNames.Add(value, name);
4938
}
5039
}
5140
}

MLAPI/MLAPI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<Compile Include="NetworkingManagerComponents\Binary\BitReader.cs" />
9090
<Compile Include="NetworkingManagerComponents\Binary\BinaryHelpers.cs" />
9191
<Compile Include="NetworkingManagerComponents\Binary\BinarySerializer.cs" />
92+
<Compile Include="NetworkingManagerComponents\Binary\PrimitiveHasher.cs" />
9293
<Compile Include="NetworkingManagerComponents\Cryptography\CryptographyHelper.cs" />
9394
<Compile Include="NetworkingManagerComponents\Cryptography\DiffieHellman.cs" />
9495
<Compile Include="NetworkingManagerComponents\Cryptography\EllipticCurve.cs" />
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.Collections.Generic;
2+
using System.Security.Cryptography;
3+
using System.Text;
4+
5+
namespace MLAPI.NetworkingManagerComponents.Binary
6+
{
7+
public static class PrimitiveHasher
8+
{
9+
private static Dictionary<string, ulong> ulongCachedHashes = new Dictionary<string, ulong>();
10+
private static Dictionary<string, ushort> ushortCachedHashes = new Dictionary<string, ushort>();
11+
private static Dictionary<string, uint> uintCachedHashes = new Dictionary<string, uint>();
12+
13+
public static ulong GetULongHash(string input, bool cache = false)
14+
{
15+
if (cache && ulongCachedHashes.ContainsKey(input))
16+
return ulongCachedHashes[input];
17+
18+
using (SHA256Managed sha = new SHA256Managed())
19+
{
20+
byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
21+
ulong value = hash[0] | ((ulong)hash[1] << 8) | ((ulong)hash[2] << 16) | ((ulong)hash[3] << 24) | ((ulong)hash[4] << 32) | ((ulong)hash[5] << 40) | ((ulong)hash[6] << 48) | ((ulong)hash[7] << 56);
22+
if (cache)
23+
ulongCachedHashes.Add(input, value);
24+
return value;
25+
}
26+
}
27+
28+
public static ulong GetUIntHash(string input, bool cache = false)
29+
{
30+
if (cache && uintCachedHashes.ContainsKey(input))
31+
return ulongCachedHashes[input];
32+
33+
using (SHA256Managed sha = new SHA256Managed())
34+
{
35+
byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
36+
uint value = hash[0] | ((uint)hash[1] << 8) | ((uint)hash[2] << 16) | ((uint)hash[3] << 24);
37+
if (cache)
38+
uintCachedHashes.Add(input, value);
39+
return value;
40+
}
41+
}
42+
43+
public static ushort GetUShortHash(string input, bool cache = false)
44+
{
45+
if (cache && ushortCachedHashes.ContainsKey(input))
46+
return ushortCachedHashes[input];
47+
48+
using (SHA256Managed sha = new SHA256Managed())
49+
{
50+
byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
51+
ushort value = (ushort)(hash[0] | (ushort)(hash[1] << 8));
52+
if (cache)
53+
ushortCachedHashes.Add(input, value);
54+
return value;
55+
}
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)