Skip to content

Commit 0bf6b22

Browse files
Replaced all hashing with non.allocating hashing
1 parent a4fa4b9 commit 0bf6b22

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

MLAPI/Data/NetworkConfig.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using MLAPI.NetworkingManagerComponents.Binary;
2+
using MLAPI.NetworkingManagerComponents.Cryptography;
23
using System;
34
using System.Collections.Generic;
45
using System.Security.Cryptography;
@@ -187,16 +188,16 @@ public byte[] GetConfig(bool cache = true)
187188
writer.WriteBool(EnableSceneSwitching);
188189
writer.WriteBool(SignKeyExchange);
189190

190-
using (SHA256Managed sha256 = new SHA256Managed())
191-
{
192-
//Returns a 256 bit / 32 byte long checksum of the config
191+
//using (SHA256Managed sha256 = new SHA256Managed())
192+
//{
193+
// Returns a 160 bit / 20 byte / 5 int checksum of the config
193194
if (cache)
194195
{
195-
ConfigHash = sha256.ComputeHash(writer.Finalize());
196+
ConfigHash = MessageDigest.SHA1_Opt(writer.Finalize()).ToArray(); //sha256.ComputeHash(writer.Finalize());
196197
return ConfigHash;
197198
}
198-
return sha256.ComputeHash(writer.Finalize());
199-
}
199+
return MessageDigest.SHA1_Opt(writer.Finalize()).ToArray(); //sha256.ComputeHash(writer.Finalize());
200+
//}
200201
}
201202
}
202203

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using MLAPI.NetworkingManagerComponents.Cryptography;
2+
using System.Collections.Generic;
23
using System.Security.Cryptography;
34
using System.Text;
45

@@ -15,44 +16,44 @@ public static ulong GetULongHash(string input, bool cache = false)
1516
if (cache && ulongCachedHashes.ContainsKey(input))
1617
return ulongCachedHashes[input];
1718

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);
19+
//using (SHA256Managed sha = new SHA256Managed())
20+
//{
21+
//byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
22+
ulong value = MessageDigest.SHA1_Opt(Encoding.UTF8.GetBytes(input)).CastULong(); //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);
2223
if (cache)
2324
ulongCachedHashes.Add(input, value);
2425
return value;
25-
}
26+
//}
2627
}
2728

2829
public static ulong GetUIntHash(string input, bool cache = false)
2930
{
3031
if (cache && uintCachedHashes.ContainsKey(input))
3132
return uintCachedHashes[input];
3233

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);
34+
//using (SHA256Managed sha = new SHA256Managed())
35+
//{
36+
//byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
37+
uint value = MessageDigest.SHA1_Opt(Encoding.UTF8.GetBytes(input)).CastUInt(); //hash[0] | ((uint)hash[1] << 8) | ((uint)hash[2] << 16) | ((uint)hash[3] << 24);
3738
if (cache)
3839
uintCachedHashes.Add(input, value);
3940
return value;
40-
}
41+
//}
4142
}
4243

4344
public static ushort GetUShortHash(string input, bool cache = false)
4445
{
4546
if (cache && ushortCachedHashes.ContainsKey(input))
4647
return ushortCachedHashes[input];
4748

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));
49+
//using (SHA256Managed sha = new SHA256Managed())
50+
//{
51+
//byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
52+
ushort value = MessageDigest.SHA1_Opt(Encoding.UTF8.GetBytes(input)).CastUShort(); //(ushort)(hash[0] | (ushort)(hash[1] << 8));
5253
if (cache)
5354
ushortCachedHashes.Add(input, value);
5455
return value;
55-
}
56+
//}
5657
}
5758
}
5859
}

MLAPI/NetworkingManagerComponents/Cryptography/MessageDigest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ public struct SHA1Result
1111
{
1212
public uint i0, i1, i2, i3, i4;
1313
public byte Get(int idx) => (byte)((idx < 4 ? i0 : idx < 8 ? i1 : idx < 12 ? i2 : idx < 16 ? i3 : i4) >> (8 * (idx % 4)));
14+
public byte[] ToArray()
15+
{
16+
byte[] b = new byte[20];
17+
for (int i = 0; i < 20; ++i) b[i] = Get(i);
18+
return b;
19+
}
20+
private ulong Collect(int bytes)
21+
{
22+
ulong u = 0;
23+
for (int i = 0; i < bytes; ++i) u |= (ulong)Get(i) << (8*i);
24+
return u;
25+
}
26+
public byte CastByte() => Get(0);
27+
public ushort CastUShort() => (ushort)Collect(2);
28+
public uint CastUInt() => (uint)Collect(4);
29+
public ulong CastULong() => (ulong)Collect(8);
1430
}
1531
public static SHA1Result SHA1_Opt(byte[] message)
1632
{

0 commit comments

Comments
 (0)