From 618fa1f6e06c8697ea698509e77b22d3aefa9166 Mon Sep 17 00:00:00 2001 From: McSpazzy Date: Fri, 2 Oct 2020 17:01:25 +1000 Subject: [PATCH 1/2] Add murmerhash3 support --- .../Security/Cryptography/mmh3.cs | 89 +++++++++++++++++++ Switch_Toolbox_Library/Toolbox_Library.csproj | 1 + Toolbox/GUI/HashCalculatorForm.cs | 4 + 3 files changed, 94 insertions(+) create mode 100644 Switch_Toolbox_Library/Security/Cryptography/mmh3.cs diff --git a/Switch_Toolbox_Library/Security/Cryptography/mmh3.cs b/Switch_Toolbox_Library/Security/Cryptography/mmh3.cs new file mode 100644 index 00000000..8fdfa522 --- /dev/null +++ b/Switch_Toolbox_Library/Security/Cryptography/mmh3.cs @@ -0,0 +1,89 @@ +using System.IO; +using System.Text; + +namespace Toolbox.Library.Security.Cryptography +{ + public static class MurMurHash3 + { + private const uint Seed = 0; + + public static uint Hash(string s) + { + var ss = new MemoryStream(Encoding.UTF8.GetBytes(s)); + return Hash(ss); + } + + public static uint Hash(Stream stream) + { + const uint c1 = 0xcc9e2d51; + const uint c2 = 0x1b873593; + + var h1 = Seed; + uint streamLength = 0; + + using (var reader = new BinaryReader(stream)) + { + var chunk = reader.ReadBytes(4); + while (chunk.Length > 0) + { + streamLength += (uint)chunk.Length; + uint k1; + switch (chunk.Length) + { + case 4: + k1 = (uint)(chunk[0] | chunk[1] << 8 | chunk[2] << 16 | chunk[3] << 24); + k1 *= c1; + k1 = Rot(k1, 15); + k1 *= c2; + h1 ^= k1; + h1 = Rot(h1, 13); + h1 = h1 * 5 + 0xe6546b64; + break; + case 3: + k1 = (uint) (chunk[0] | chunk[1] << 8 | chunk[2] << 16); + k1 *= c1; + k1 = Rot(k1, 15); + k1 *= c2; + h1 ^= k1; + break; + case 2: + k1 = (uint) (chunk[0] | chunk[1] << 8); + k1 *= c1; + k1 = Rot(k1, 15); + k1 *= c2; + h1 ^= k1; + break; + case 1: + k1 = (chunk[0]); + k1 *= c1; + k1 = Rot(k1, 15); + k1 *= c2; + h1 ^= k1; + break; + } + chunk = reader.ReadBytes(4); + } + } + + h1 ^= streamLength; + h1 = Mix(h1); + + return h1; + } + + private static uint Rot(uint x, byte r) + { + return (x << r) | (x >> (32 - r)); + } + + private static uint Mix(uint h) + { + h ^= h >> 16; + h *= 0x85ebca6b; + h ^= h >> 13; + h *= 0xc2b2ae35; + h ^= h >> 16; + return h; + } + } +} diff --git a/Switch_Toolbox_Library/Toolbox_Library.csproj b/Switch_Toolbox_Library/Toolbox_Library.csproj index 67abc8be..f3c6d3d8 100644 --- a/Switch_Toolbox_Library/Toolbox_Library.csproj +++ b/Switch_Toolbox_Library/Toolbox_Library.csproj @@ -450,6 +450,7 @@ + diff --git a/Toolbox/GUI/HashCalculatorForm.cs b/Toolbox/GUI/HashCalculatorForm.cs index 18de22b5..67ff12b1 100644 --- a/Toolbox/GUI/HashCalculatorForm.cs +++ b/Toolbox/GUI/HashCalculatorForm.cs @@ -9,6 +9,7 @@ using System.Threading; using System.Windows.Forms; using Toolbox.Library.Forms; using Toolbox.Library; +using Toolbox.Library.Security.Cryptography; namespace Toolbox { @@ -27,6 +28,7 @@ namespace Toolbox hashTypeCB.Items.Add("CRC32"); hashTypeCB.Items.Add("BCSV"); hashTypeCB.Items.Add("SARC"); + hashTypeCB.Items.Add("MMH3"); hashTypeCB.SelectedIndex = 0; @@ -71,6 +73,8 @@ namespace Toolbox return stringToHash(text); else if (type == "SARC") return NameHash(text); + else if (type == "MMH3") + return MurMurHash3.Hash(text); return 0; } From 67eb77dcb62d58cd486a70e093c5ddf5d26fd8c9 Mon Sep 17 00:00:00 2001 From: McSpazzy Date: Fri, 2 Oct 2020 17:15:24 +1000 Subject: [PATCH 2/2] Use full name instead of import --- Toolbox/GUI/HashCalculatorForm.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Toolbox/GUI/HashCalculatorForm.cs b/Toolbox/GUI/HashCalculatorForm.cs index 67ff12b1..2e215bba 100644 --- a/Toolbox/GUI/HashCalculatorForm.cs +++ b/Toolbox/GUI/HashCalculatorForm.cs @@ -9,7 +9,6 @@ using System.Threading; using System.Windows.Forms; using Toolbox.Library.Forms; using Toolbox.Library; -using Toolbox.Library.Security.Cryptography; namespace Toolbox { @@ -74,7 +73,7 @@ namespace Toolbox else if (type == "SARC") return NameHash(text); else if (type == "MMH3") - return MurMurHash3.Hash(text); + return Toolbox.Library.Security.Cryptography.MurMurHash3.Hash(text); return 0; }