diff --git a/SabreTools.Hashing/HashWrapper.cs b/SabreTools.Hashing/HashWrapper.cs index b5e3b9f..bcc7a60 100644 --- a/SabreTools.Hashing/HashWrapper.cs +++ b/SabreTools.Hashing/HashWrapper.cs @@ -316,8 +316,8 @@ namespace SabreTools.Hashing HashType.Tiger2_192_3 => new MessageDigest.Tiger2_192_3(), HashType.Tiger2_192_4 => new MessageDigest.Tiger2_192_4(), - HashType.XxHash32 => new XxHash.XxHash32(), - HashType.XxHash64 => new XxHash.XxHash64(), + HashType.XxHash32 => new NonCryptographicHash.XxHash32(), + HashType.XxHash64 => new NonCryptographicHash.XxHash64(), #if NET462_OR_GREATER || NETCOREAPP HashType.XxHash3 => new XxHash3(), HashType.XxHash128 => new XxHash128(), diff --git a/SabreTools.Hashing/NonCryptographicHash/Constants.cs b/SabreTools.Hashing/NonCryptographicHash/Constants.cs index 5a1fefc..9de3146 100644 --- a/SabreTools.Hashing/NonCryptographicHash/Constants.cs +++ b/SabreTools.Hashing/NonCryptographicHash/Constants.cs @@ -11,5 +11,33 @@ namespace SabreTools.Hashing.NonCryptographicHash public const ulong FNV64Prime = 0x00000100000001b3; #endregion + + #region xxHash-32 + + public const uint XXH_PRIME32_1 = 0x9E3779B1; + + public const uint XXH_PRIME32_2 = 0x85EBCA77; + + public const uint XXH_PRIME32_3 = 0xC2B2AE3D; + + public const uint XXH_PRIME32_4 = 0x27D4EB2F; + + public const uint XXH_PRIME32_5 = 0x165667B1; + + #endregion + + #region xxHash-64 + + public const ulong XXH_PRIME64_1 = 0x9E3779B185EBCA87; + + public const ulong XXH_PRIME64_2 = 0xC2B2AE3D27D4EB4F; + + public const ulong XXH_PRIME64_3 = 0x165667B19E3779F9; + + public const ulong XXH_PRIME64_4 = 0x85EBCA77C2B2AE63; + + public const ulong XXH_PRIME64_5 = 0x27D4EB2F165667C5; + + #endregion } } \ No newline at end of file diff --git a/SabreTools.Hashing/XxHash/XxHash32.cs b/SabreTools.Hashing/NonCryptographicHash/XxHash32.cs similarity index 84% rename from SabreTools.Hashing/XxHash/XxHash32.cs rename to SabreTools.Hashing/NonCryptographicHash/XxHash32.cs index 1bb4616..54cb67b 100644 --- a/SabreTools.Hashing/XxHash/XxHash32.cs +++ b/SabreTools.Hashing/NonCryptographicHash/XxHash32.cs @@ -1,6 +1,6 @@ using System; -namespace SabreTools.Hashing.XxHash +namespace SabreTools.Hashing.NonCryptographicHash { public class XxHash32 : System.Security.Cryptography.HashAlgorithm { @@ -10,14 +10,14 @@ namespace SabreTools.Hashing.XxHash private readonly uint _seed; /// - /// Internal xxHash32 state + /// Internal xxHash-32 state /// - private readonly XXH32State _state; + private readonly XxHash32State _state; public XxHash32(uint seed = 0) { _seed = seed; - _state = new XXH32State(); + _state = new XxHash32State(); _state.Reset(seed); } diff --git a/SabreTools.Hashing/XxHash/XXH32State.cs b/SabreTools.Hashing/NonCryptographicHash/XxHash32State.cs similarity index 97% rename from SabreTools.Hashing/XxHash/XXH32State.cs rename to SabreTools.Hashing/NonCryptographicHash/XxHash32State.cs index cd065f2..aa7c29c 100644 --- a/SabreTools.Hashing/XxHash/XXH32State.cs +++ b/SabreTools.Hashing/NonCryptographicHash/XxHash32State.cs @@ -1,14 +1,14 @@ using System; using static SabreTools.Hashing.HashOperations; -using static SabreTools.Hashing.XxHash.Constants; +using static SabreTools.Hashing.NonCryptographicHash.Constants; -namespace SabreTools.Hashing.XxHash +namespace SabreTools.Hashing.NonCryptographicHash { /// - /// Structure for XXH32 streaming API. + /// Structure for xxHash-32 streaming API. /// /// - internal class XXH32State + internal class XxHash32State { /// /// Total length hashed, modulo 2^32 diff --git a/SabreTools.Hashing/XxHash/XxHash64.cs b/SabreTools.Hashing/NonCryptographicHash/XxHash64.cs similarity index 84% rename from SabreTools.Hashing/XxHash/XxHash64.cs rename to SabreTools.Hashing/NonCryptographicHash/XxHash64.cs index 348e7ff..fcb22cf 100644 --- a/SabreTools.Hashing/XxHash/XxHash64.cs +++ b/SabreTools.Hashing/NonCryptographicHash/XxHash64.cs @@ -1,6 +1,6 @@ using System; -namespace SabreTools.Hashing.XxHash +namespace SabreTools.Hashing.NonCryptographicHash { public class XxHash64 : System.Security.Cryptography.HashAlgorithm { @@ -10,14 +10,14 @@ namespace SabreTools.Hashing.XxHash private readonly uint _seed; /// - /// Internal xxHash64 state + /// Internal xxHash-64 state /// - private readonly XXH64State _state; + private readonly XxHash64State _state; public XxHash64(uint seed = 0) { _seed = seed; - _state = new XXH64State(); + _state = new XxHash64State(); _state.Reset(seed); } diff --git a/SabreTools.Hashing/XxHash/XXH64State.cs b/SabreTools.Hashing/NonCryptographicHash/XxHash64State.cs similarity index 89% rename from SabreTools.Hashing/XxHash/XXH64State.cs rename to SabreTools.Hashing/NonCryptographicHash/XxHash64State.cs index e7dd3c4..c543058 100644 --- a/SabreTools.Hashing/XxHash/XXH64State.cs +++ b/SabreTools.Hashing/NonCryptographicHash/XxHash64State.cs @@ -1,15 +1,14 @@ using System; using static SabreTools.Hashing.HashOperations; -using static SabreTools.Hashing.XxHash.Constants; -using static SabreTools.Hashing.XxHash.Utility; +using static SabreTools.Hashing.NonCryptographicHash.Constants; -namespace SabreTools.Hashing.XxHash +namespace SabreTools.Hashing.NonCryptographicHash { /// - /// Structure for XXH64 streaming API. + /// Structure for xxHash-64 streaming API. /// /// - internal class XXH64State + internal class XxHash64State { /// /// Total length hashed. This is always 64-bit. @@ -198,7 +197,23 @@ namespace SabreTools.Hashing.XxHash --length; } - return XXH64Avalanche(hash); + return Avalanche(hash); + } + + /// + /// Mixes all bits to finalize the hash. + /// + /// The final mix ensures that all input bits have a chance to impact any bit in + /// the output digest, resulting in an unbiased distribution. + /// + private static ulong Avalanche(ulong hash) + { + hash ^= hash >> 33; + hash *= XXH_PRIME64_2; + hash ^= hash >> 29; + hash *= XXH_PRIME64_3; + hash ^= hash >> 32; + return hash; } } } \ No newline at end of file diff --git a/SabreTools.Hashing/XxHash/Constants.cs b/SabreTools.Hashing/XxHash/Constants.cs index 1884c88..f7abdf5 100644 --- a/SabreTools.Hashing/XxHash/Constants.cs +++ b/SabreTools.Hashing/XxHash/Constants.cs @@ -13,34 +13,6 @@ namespace SabreTools.Hashing.XxHash #endregion - #region XXH32 - - public const uint XXH_PRIME32_1 = 0x9E3779B1; - - public const uint XXH_PRIME32_2 = 0x85EBCA77; - - public const uint XXH_PRIME32_3 = 0xC2B2AE3D; - - public const uint XXH_PRIME32_4 = 0x27D4EB2F; - - public const uint XXH_PRIME32_5 = 0x165667B1; - - #endregion - - #region XXH64 - - public const ulong XXH_PRIME64_1 = 0x9E3779B185EBCA87; - - public const ulong XXH_PRIME64_2 = 0xC2B2AE3D27D4EB4F; - - public const ulong XXH_PRIME64_3 = 0x165667B19E3779F9; - - public const ulong XXH_PRIME64_4 = 0x85EBCA77C2B2AE63; - - public const ulong XXH_PRIME64_5 = 0x27D4EB2F165667C5; - - #endregion - #region XXH3 /// diff --git a/SabreTools.Hashing/XxHash/Utility.cs b/SabreTools.Hashing/XxHash/Utility.cs index f5600d1..5efa98e 100644 --- a/SabreTools.Hashing/XxHash/Utility.cs +++ b/SabreTools.Hashing/XxHash/Utility.cs @@ -1,4 +1,5 @@ using static SabreTools.Hashing.HashOperations; +using static SabreTools.Hashing.NonCryptographicHash.Constants; using static SabreTools.Hashing.XxHash.Constants; namespace SabreTools.Hashing.XxHash