From 0b1146ea33c8e797dadc609a8d5c24daf1f3331a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20Cor=C3=A9n?= <2108U9@gmail.com> Date: Mon, 23 Apr 2018 20:21:34 +0200 Subject: [PATCH] Added public primitive hasher class --- MLAPI/Data/Cache.cs | 29 +++------- MLAPI/MLAPI.csproj | 1 + .../Binary/PrimitiveHasher.cs | 58 +++++++++++++++++++ 3 files changed, 68 insertions(+), 20 deletions(-) create mode 100644 MLAPI/NetworkingManagerComponents/Binary/PrimitiveHasher.cs 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 @@ + 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 ulongCachedHashes = new Dictionary(); + private static Dictionary ushortCachedHashes = new Dictionary(); + private static Dictionary uintCachedHashes = new Dictionary(); + + 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; + } + } + } +}