mirror of
https://github.com/SabreTools/SabreTools.Hashing.git
synced 2026-07-08 18:06:40 +00:00
FNV are non-cryptographic hashes
This commit is contained in:
@@ -2,7 +2,7 @@ namespace SabreTools.Hashing.Checksum
|
||||
{
|
||||
internal static class Constants
|
||||
{
|
||||
#region Adler-32 / Fletcher-32
|
||||
#region Adler-32
|
||||
|
||||
/// <summary>
|
||||
/// Largest prime smaller than 65536
|
||||
@@ -14,6 +14,10 @@ namespace SabreTools.Hashing.Checksum
|
||||
/// </summary>
|
||||
public const ushort A32NMAX = 5552;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fletcher-32
|
||||
|
||||
/// <summary>
|
||||
/// Max value for a single half of a Fletcher-32 checksum
|
||||
/// </summary>
|
||||
@@ -25,15 +29,5 @@ namespace SabreTools.Hashing.Checksum
|
||||
public const uint F64BASE = 0xffffffff;
|
||||
|
||||
#endregion
|
||||
|
||||
#region FNV
|
||||
|
||||
public const uint FNV32Basis = 0x811c9dc5;
|
||||
public const ulong FNV64Basis = 0xcbf29ce484222325;
|
||||
|
||||
public const uint FNV32Prime = 0x01000193;
|
||||
public const ulong FNV64Prime = 0x00000100000001b3;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
namespace SabreTools.Hashing.Checksum
|
||||
{
|
||||
public abstract class FnvBase<T> : ChecksumBase<T> where T : struct
|
||||
{
|
||||
/// <summary>
|
||||
/// Initial value to use
|
||||
/// </summary>
|
||||
protected T _basis;
|
||||
|
||||
/// <summary>
|
||||
/// Round prime to use
|
||||
/// </summary>
|
||||
protected T _prime;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Initialize()
|
||||
{
|
||||
_hash = _basis;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ using Aaru.CommonTypes.Interfaces;
|
||||
using Blake3;
|
||||
#endif
|
||||
using SabreTools.Hashing.Checksum;
|
||||
using SabreTools.Hashing.NonCryptographicHash;
|
||||
using static SabreTools.Hashing.HashOperations;
|
||||
|
||||
namespace SabreTools.Hashing
|
||||
|
||||
15
SabreTools.Hashing/NonCryptographicHash/Constants.cs
Normal file
15
SabreTools.Hashing/NonCryptographicHash/Constants.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace SabreTools.Hashing.NonCryptographicHash
|
||||
{
|
||||
internal static class Constants
|
||||
{
|
||||
#region FNV
|
||||
|
||||
public const uint FNV32Basis = 0x811c9dc5;
|
||||
public const ulong FNV64Basis = 0xcbf29ce484222325;
|
||||
|
||||
public const uint FNV32Prime = 0x01000193;
|
||||
public const ulong FNV64Prime = 0x00000100000001b3;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using static SabreTools.Hashing.Checksum.Constants;
|
||||
using static SabreTools.Hashing.NonCryptographicHash.Constants;
|
||||
|
||||
namespace SabreTools.Hashing.Checksum
|
||||
namespace SabreTools.Hashing.NonCryptographicHash
|
||||
{
|
||||
public class FNV0_32 : FnvBase<uint>
|
||||
{
|
||||
@@ -1,6 +1,6 @@
|
||||
using static SabreTools.Hashing.Checksum.Constants;
|
||||
using static SabreTools.Hashing.NonCryptographicHash.Constants;
|
||||
|
||||
namespace SabreTools.Hashing.Checksum
|
||||
namespace SabreTools.Hashing.NonCryptographicHash
|
||||
{
|
||||
public class FNV0_64 : FnvBase<ulong>
|
||||
{
|
||||
@@ -1,6 +1,6 @@
|
||||
using static SabreTools.Hashing.Checksum.Constants;
|
||||
using static SabreTools.Hashing.NonCryptographicHash.Constants;
|
||||
|
||||
namespace SabreTools.Hashing.Checksum
|
||||
namespace SabreTools.Hashing.NonCryptographicHash
|
||||
{
|
||||
public class FNV1_32 : FnvBase<uint>
|
||||
{
|
||||
@@ -1,6 +1,6 @@
|
||||
using static SabreTools.Hashing.Checksum.Constants;
|
||||
using static SabreTools.Hashing.NonCryptographicHash.Constants;
|
||||
|
||||
namespace SabreTools.Hashing.Checksum
|
||||
namespace SabreTools.Hashing.NonCryptographicHash
|
||||
{
|
||||
public class FNV1_64 : FnvBase<ulong>
|
||||
{
|
||||
@@ -1,6 +1,6 @@
|
||||
using static SabreTools.Hashing.Checksum.Constants;
|
||||
using static SabreTools.Hashing.NonCryptographicHash.Constants;
|
||||
|
||||
namespace SabreTools.Hashing.Checksum
|
||||
namespace SabreTools.Hashing.NonCryptographicHash
|
||||
{
|
||||
public class FNV1a_32 : FnvBase<uint>
|
||||
{
|
||||
@@ -1,6 +1,6 @@
|
||||
using static SabreTools.Hashing.Checksum.Constants;
|
||||
using static SabreTools.Hashing.NonCryptographicHash.Constants;
|
||||
|
||||
namespace SabreTools.Hashing.Checksum
|
||||
namespace SabreTools.Hashing.NonCryptographicHash
|
||||
{
|
||||
public class FNV1a_64 : FnvBase<ulong>
|
||||
{
|
||||
60
SabreTools.Hashing/NonCryptographicHash/FnvBase.cs
Normal file
60
SabreTools.Hashing/NonCryptographicHash/FnvBase.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
|
||||
namespace SabreTools.Hashing.NonCryptographicHash
|
||||
{
|
||||
/// <summary>
|
||||
/// Common base class for FNV non-cryptographic hashes
|
||||
/// </summary>
|
||||
public abstract class FnvBase : System.Security.Cryptography.HashAlgorithm
|
||||
{
|
||||
// No common, untyped functionality
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Common base class for FNV non-cryptographic hashes
|
||||
/// </summary>
|
||||
public abstract class FnvBase<T> : FnvBase where T : struct
|
||||
{
|
||||
/// <summary>
|
||||
/// Initial value to use
|
||||
/// </summary>
|
||||
protected T _basis;
|
||||
|
||||
/// <summary>
|
||||
/// Round prime to use
|
||||
/// </summary>
|
||||
protected T _prime;
|
||||
|
||||
/// <summary>
|
||||
/// The current value of the hash
|
||||
/// </summary>
|
||||
protected T _hash;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Initialize()
|
||||
{
|
||||
_hash = _basis;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override byte[] HashFinal()
|
||||
{
|
||||
byte[] hashArr = _hash switch
|
||||
{
|
||||
short s => BitConverter.GetBytes(s),
|
||||
ushort s => BitConverter.GetBytes(s),
|
||||
|
||||
int i => BitConverter.GetBytes(i),
|
||||
uint i => BitConverter.GetBytes(i),
|
||||
|
||||
long l => BitConverter.GetBytes(l),
|
||||
ulong l => BitConverter.GetBytes(l),
|
||||
|
||||
_ => [],
|
||||
};
|
||||
|
||||
Array.Reverse(hashArr);
|
||||
return hashArr;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user