Merge pull request #288 from McSpazzy/mmh3

Add Murmurhash3 Support
This commit is contained in:
KillzXGaming 2020-10-02 18:05:07 -04:00 committed by GitHub
commit ad9345f7fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 93 additions and 0 deletions

View file

@ -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;
}
}
}

View file

@ -450,6 +450,7 @@
<Compile Include="Rendering\GenericModelRenderer\GenericRenderedObject.cs" />
<Compile Include="Security\Cryptography\crc32.cs" />
<Compile Include="DrawableContainer.cs" />
<Compile Include="Security\Cryptography\mmh3.cs" />
<Compile Include="StaticDynamic.cs" />
<Compile Include="Texture Decoding\3DS\ETC1.cs" />
<Compile Include="FileFormats\Animation\SMD.cs" />

View file

@ -27,6 +27,7 @@ namespace Toolbox
hashTypeCB.Items.Add("CRC32");
hashTypeCB.Items.Add("BCSV");
hashTypeCB.Items.Add("SARC");
hashTypeCB.Items.Add("MMH3");
hashTypeCB.SelectedIndex = 0;
@ -71,6 +72,8 @@ namespace Toolbox
return stringToHash(text);
else if (type == "SARC")
return NameHash(text);
else if (type == "MMH3")
return Toolbox.Library.Security.Cryptography.MurMurHash3.Hash(text);
return 0;
}