Replaced all hashing with non.allocating hashing
This commit is contained in:
parent
a4fa4b943a
commit
0bf6b22717
@ -1,4 +1,5 @@
|
||||
using MLAPI.NetworkingManagerComponents.Binary;
|
||||
using MLAPI.NetworkingManagerComponents.Cryptography;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
@ -187,16 +188,16 @@ namespace MLAPI.Data
|
||||
writer.WriteBool(EnableSceneSwitching);
|
||||
writer.WriteBool(SignKeyExchange);
|
||||
|
||||
using (SHA256Managed sha256 = new SHA256Managed())
|
||||
{
|
||||
//Returns a 256 bit / 32 byte long checksum of the config
|
||||
//using (SHA256Managed sha256 = new SHA256Managed())
|
||||
//{
|
||||
// Returns a 160 bit / 20 byte / 5 int checksum of the config
|
||||
if (cache)
|
||||
{
|
||||
ConfigHash = sha256.ComputeHash(writer.Finalize());
|
||||
ConfigHash = MessageDigest.SHA1_Opt(writer.Finalize()).ToArray(); //sha256.ComputeHash(writer.Finalize());
|
||||
return ConfigHash;
|
||||
}
|
||||
return sha256.ComputeHash(writer.Finalize());
|
||||
}
|
||||
return MessageDigest.SHA1_Opt(writer.Finalize()).ToArray(); //sha256.ComputeHash(writer.Finalize());
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using MLAPI.NetworkingManagerComponents.Cryptography;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
@ -15,14 +16,14 @@ namespace MLAPI.NetworkingManagerComponents.Binary
|
||||
if (cache && ulongCachedHashes.ContainsKey(input))
|
||||
return ulongCachedHashes[input];
|
||||
|
||||
using (SHA256Managed sha = new SHA256Managed())
|
||||
{
|
||||
byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
|
||||
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);
|
||||
//using (SHA256Managed sha = new SHA256Managed())
|
||||
//{
|
||||
//byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
|
||||
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);
|
||||
if (cache)
|
||||
ulongCachedHashes.Add(input, value);
|
||||
return value;
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
||||
public static ulong GetUIntHash(string input, bool cache = false)
|
||||
@ -30,14 +31,14 @@ namespace MLAPI.NetworkingManagerComponents.Binary
|
||||
if (cache && uintCachedHashes.ContainsKey(input))
|
||||
return uintCachedHashes[input];
|
||||
|
||||
using (SHA256Managed sha = new SHA256Managed())
|
||||
{
|
||||
byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
|
||||
uint value = hash[0] | ((uint)hash[1] << 8) | ((uint)hash[2] << 16) | ((uint)hash[3] << 24);
|
||||
//using (SHA256Managed sha = new SHA256Managed())
|
||||
//{
|
||||
//byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
|
||||
uint value = MessageDigest.SHA1_Opt(Encoding.UTF8.GetBytes(input)).CastUInt(); //hash[0] | ((uint)hash[1] << 8) | ((uint)hash[2] << 16) | ((uint)hash[3] << 24);
|
||||
if (cache)
|
||||
uintCachedHashes.Add(input, value);
|
||||
return value;
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
||||
public static ushort GetUShortHash(string input, bool cache = false)
|
||||
@ -45,14 +46,14 @@ namespace MLAPI.NetworkingManagerComponents.Binary
|
||||
if (cache && ushortCachedHashes.ContainsKey(input))
|
||||
return ushortCachedHashes[input];
|
||||
|
||||
using (SHA256Managed sha = new SHA256Managed())
|
||||
{
|
||||
byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
|
||||
ushort value = (ushort)(hash[0] | (ushort)(hash[1] << 8));
|
||||
//using (SHA256Managed sha = new SHA256Managed())
|
||||
//{
|
||||
//byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
|
||||
ushort value = MessageDigest.SHA1_Opt(Encoding.UTF8.GetBytes(input)).CastUShort(); //(ushort)(hash[0] | (ushort)(hash[1] << 8));
|
||||
if (cache)
|
||||
ushortCachedHashes.Add(input, value);
|
||||
return value;
|
||||
}
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,22 @@ namespace MLAPI.NetworkingManagerComponents.Cryptography
|
||||
{
|
||||
public uint i0, i1, i2, i3, i4;
|
||||
public byte Get(int idx) => (byte)((idx < 4 ? i0 : idx < 8 ? i1 : idx < 12 ? i2 : idx < 16 ? i3 : i4) >> (8 * (idx % 4)));
|
||||
public byte[] ToArray()
|
||||
{
|
||||
byte[] b = new byte[20];
|
||||
for (int i = 0; i < 20; ++i) b[i] = Get(i);
|
||||
return b;
|
||||
}
|
||||
private ulong Collect(int bytes)
|
||||
{
|
||||
ulong u = 0;
|
||||
for (int i = 0; i < bytes; ++i) u |= (ulong)Get(i) << (8*i);
|
||||
return u;
|
||||
}
|
||||
public byte CastByte() => Get(0);
|
||||
public ushort CastUShort() => (ushort)Collect(2);
|
||||
public uint CastUInt() => (uint)Collect(4);
|
||||
public ulong CastULong() => (ulong)Collect(8);
|
||||
}
|
||||
public static SHA1Result SHA1_Opt(byte[] message)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user