Make Hasher more consistent

This commit is contained in:
Matt Nadareski
2023-10-26 11:54:13 -04:00
parent 101cdb7b34
commit 92463a103d
3 changed files with 79 additions and 44 deletions

View File

@@ -3,6 +3,7 @@
- Move Hash enum and simplify
- Add other non-cryptographic hashes
- Avoid unncessary allocation in hashing
- Make Hasher more consistent
### 2.7.3 (2023-10-26)

View File

@@ -9,8 +9,10 @@ namespace MPF.Core.Hashing
/// <summary>
/// Async hashing class wraper
/// </summary>
public class Hasher
public class Hasher : IDisposable
{
#region Properties
/// <summary>
/// Hash type associated with the current state
/// </summary>
@@ -20,12 +22,69 @@ namespace MPF.Core.Hashing
public Hash HashType { get; init; }
#endif
/// <summary>
/// Current hash in bytes
/// </summary>
#if NET48
public byte[] CurrentHashBytes
#else
public byte[]? CurrentHashBytes
#endif
{
get
{
#if NET48
switch (_hasher)
{
case HashAlgorithm ha:
return ha.Hash;
case NonCryptographicHashAlgorithm ncha:
return ncha.GetCurrentHash().Reverse().ToArray();
default:
return null;
}
#else
return (_hasher) switch
{
HashAlgorithm ha => ha.Hash,
NonCryptographicHashAlgorithm ncha => ncha.GetCurrentHash().Reverse().ToArray(),
_ => null,
};
#endif
}
}
/// <summary>
/// Current hash as a string
/// </summary>
#if NET48
public string CurrentHashString => ByteArrayToString(CurrentHashBytes);
#else
public string? CurrentHashString => ByteArrayToString(CurrentHashBytes);
#endif
#endregion
#region Private Fields
/// <summary>
/// Internal hasher being used for processing
/// </summary>
/// <remarks>May be either a HashAlgorithm or NonCryptographicHashAlgorithm</remarks>
#if NET48
private object _hasher;
#else
private object? _hasher;
#endif
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
/// <param name="hashType">Hash type to instantiate</param>
public Hasher(Hash hashType)
{
this.HashType = hashType;
@@ -69,12 +128,17 @@ namespace MPF.Core.Hashing
}
}
/// <inheritdoc/>
public void Dispose()
{
if (_hasher is IDisposable disposable)
disposable.Dispose();
}
#endregion
#region Hashing
/// <summary>
/// Process a buffer of some length with the internal hash algorithm
/// </summary>
@@ -107,44 +171,9 @@ namespace MPF.Core.Hashing
}
}
/// <summary>
/// Get internal hash as a byte array
/// </summary>
#if NET48
public byte[] GetHash()
#else
public byte[]? GetHash()
#endif
{
if (_hasher == null)
return null;
#endregion
switch (_hasher)
{
case HashAlgorithm ha:
return ha.Hash;
case NonCryptographicHashAlgorithm ncha:
return ncha.GetCurrentHash().Reverse().ToArray();
}
return null;
}
/// <summary>
/// Get internal hash as a string
/// </summary>
#if NET48
public string GetHashString()
#else
public string? GetHashString()
#endif
{
var hash = GetHash();
if (hash == null)
return null;
return ByteArrayToString(hash);
}
#region Helpers
/// <summary>
/// Convert a byte array to a hex string
@@ -172,5 +201,7 @@ namespace MPF.Core.Hashing
return null;
}
}
#endregion
}
}

View File

@@ -281,12 +281,15 @@ namespace MPF.Core
Parallel.ForEach(hashers, h => h.Terminate());
// Get the results
crc32 = hashers.First(h => h.HashType == Hash.CRC32).GetHashString();
md5 = hashers.First(h => h.HashType == Hash.MD5).GetHashString();
sha1 = hashers.First(h => h.HashType == Hash.SHA1).GetHashString();
//sha256 = hashers.First(h => h.HashType == Hash.SHA256).GetHashString();
//sha384 = hashers.First(h => h.HashType == Hash.SHA384).GetHashString();
//sha512 = hashers.First(h => h.HashType == Hash.SHA512).GetHashString();
crc32 = hashers.First(h => h.HashType == Hash.CRC32).CurrentHashString;
//crc64 = hashers.First(h => h.HashType == Hash.CRC64).CurrentHashString;
md5 = hashers.First(h => h.HashType == Hash.MD5).CurrentHashString;
sha1 = hashers.First(h => h.HashType == Hash.SHA1).CurrentHashString;
//sha256 = hashers.First(h => h.HashType == Hash.SHA256).CurrentHashString;
//sha384 = hashers.First(h => h.HashType == Hash.SHA384).CurrentHashString;
//sha512 = hashers.First(h => h.HashType == Hash.SHA512).CurrentHashString;
//xxHash32 = hashers.First(h => h.HashType == Hash.XxHash32).CurrentHashString;
//xxHash64 = hashers.First(h => h.HashType == Hash.XxHash64).CurrentHashString;
// Dispose of the hashers
loadBuffer.Dispose();