diff --git a/MLAPI/Data/Cache.cs b/MLAPI/Data/Cache.cs index 8b2d74d..990e427 100644 --- a/MLAPI/Data/Cache.cs +++ b/MLAPI/Data/Cache.cs @@ -1,6 +1,5 @@ -using System.Collections.Generic; -using System.Security.Cryptography; -using System.Text; +using MLAPI.NetworkingManagerComponents.Binary; +using System.Collections.Generic; namespace MLAPI.Data { @@ -14,15 +13,10 @@ namespace MLAPI.Data if (messageAttributeHashes.ContainsKey(name)) return messageAttributeHashes[name]; - using (SHA256Managed sha = new SHA256Managed()) - { - byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(name)); - 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); - //ulong value = hash[0] | ((uint)hash[1] << 8) | ((uint)hash[2] << 16) | ((uint)hash[3] << 24); - messageAttributeHashes.Add(name, value); - messageAttributeNames.Add(value, name); - return value; - } + ulong value = PrimitiveHasher.GetULongHash(name); + messageAttributeHashes.Add(name, value); + messageAttributeNames.Add(value, name); + return value; } internal static string GetAttributeMethodName(ulong hash) @@ -38,14 +32,9 @@ namespace MLAPI.Data if (messageAttributeHashes.ContainsKey(name)) return; - using (SHA256Managed sha = new SHA256Managed()) - { - byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(name)); - 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); - //ulong value = hash[0] | ((uint)hash[1] << 8) | ((uint)hash[2] << 16) | ((uint)hash[3] << 24); - messageAttributeHashes.Add(name, value); - messageAttributeNames.Add(value, name); - } + ulong value = PrimitiveHasher.GetULongHash(name); + messageAttributeHashes.Add(name, value); + messageAttributeNames.Add(value, name); } } } diff --git a/MLAPI/MLAPI.csproj b/MLAPI/MLAPI.csproj index 604707c..f208ce5 100644 --- a/MLAPI/MLAPI.csproj +++ b/MLAPI/MLAPI.csproj @@ -89,6 +89,7 @@ <Compile Include="NetworkingManagerComponents\Binary\BitReader.cs" /> <Compile Include="NetworkingManagerComponents\Binary\BinaryHelpers.cs" /> <Compile Include="NetworkingManagerComponents\Binary\BinarySerializer.cs" /> + <Compile Include="NetworkingManagerComponents\Binary\PrimitiveHasher.cs" /> <Compile Include="NetworkingManagerComponents\Cryptography\CryptographyHelper.cs" /> <Compile Include="NetworkingManagerComponents\Cryptography\DiffieHellman.cs" /> <Compile Include="NetworkingManagerComponents\Cryptography\EllipticCurve.cs" /> diff --git a/MLAPI/NetworkingManagerComponents/Binary/PrimitiveHasher.cs b/MLAPI/NetworkingManagerComponents/Binary/PrimitiveHasher.cs new file mode 100644 index 0000000..057f615 --- /dev/null +++ b/MLAPI/NetworkingManagerComponents/Binary/PrimitiveHasher.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Text; + +namespace MLAPI.NetworkingManagerComponents.Binary +{ + public static class PrimitiveHasher + { + private static Dictionary<string, ulong> ulongCachedHashes = new Dictionary<string, ulong>(); + private static Dictionary<string, ushort> ushortCachedHashes = new Dictionary<string, ushort>(); + private static Dictionary<string, uint> uintCachedHashes = new Dictionary<string, uint>(); + + public static ulong GetULongHash(string input, bool cache = false) + { + 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); + if (cache) + ulongCachedHashes.Add(input, value); + return value; + } + } + + public static ulong GetUIntHash(string input, bool cache = false) + { + if (cache && uintCachedHashes.ContainsKey(input)) + return ulongCachedHashes[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); + if (cache) + uintCachedHashes.Add(input, value); + return value; + } + } + + public static ushort GetUShortHash(string input, bool cache = false) + { + 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)); + if (cache) + ushortCachedHashes.Add(input, value); + return value; + } + } + } +}