xxHash are non-cryptographic hashes

This commit is contained in:
Matt Nadareski
2025-01-02 15:23:13 -05:00
parent c65184689d
commit 7776112ec6
8 changed files with 64 additions and 48 deletions

View File

@@ -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(),

View File

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

View File

@@ -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;
/// <summary>
/// Internal xxHash32 state
/// Internal xxHash-32 state
/// </summary>
private readonly XXH32State _state;
private readonly XxHash32State _state;
public XxHash32(uint seed = 0)
{
_seed = seed;
_state = new XXH32State();
_state = new XxHash32State();
_state.Reset(seed);
}

View File

@@ -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
{
/// <summary>
/// Structure for XXH32 streaming API.
/// Structure for xxHash-32 streaming API.
/// </summary>
/// <see href="https://github.com/Cyan4973/xxHash/blob/dev/xxhash.h"/>
internal class XXH32State
internal class XxHash32State
{
/// <summary>
/// Total length hashed, modulo 2^32

View File

@@ -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;
/// <summary>
/// Internal xxHash64 state
/// Internal xxHash-64 state
/// </summary>
private readonly XXH64State _state;
private readonly XxHash64State _state;
public XxHash64(uint seed = 0)
{
_seed = seed;
_state = new XXH64State();
_state = new XxHash64State();
_state.Reset(seed);
}

View File

@@ -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
{
/// <summary>
/// Structure for XXH64 streaming API.
/// Structure for xxHash-64 streaming API.
/// </summary>
/// <see href="https://github.com/Cyan4973/xxHash/blob/dev/xxhash.h"/>
internal class XXH64State
internal class XxHash64State
{
/// <summary>
/// Total length hashed. This is always 64-bit.
@@ -198,7 +197,23 @@ namespace SabreTools.Hashing.XxHash
--length;
}
return XXH64Avalanche(hash);
return Avalanche(hash);
}
/// <summary>
/// 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.
/// </summary>
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;
}
}
}

View File

@@ -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
/// <summary>

View File

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