diff --git a/CHANGELIST.md b/CHANGELIST.md
index 5780bd88..84cc1a41 100644
--- a/CHANGELIST.md
+++ b/CHANGELIST.md
@@ -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)
diff --git a/MPF.Core/Hashing/Hasher.cs b/MPF.Core/Hashing/Hasher.cs
index 705cc182..c26c574b 100644
--- a/MPF.Core/Hashing/Hasher.cs
+++ b/MPF.Core/Hashing/Hasher.cs
@@ -9,8 +9,10 @@ namespace MPF.Core.Hashing
///
/// Async hashing class wraper
///
- public class Hasher
+ public class Hasher : IDisposable
{
+ #region Properties
+
///
/// Hash type associated with the current state
///
@@ -20,12 +22,69 @@ namespace MPF.Core.Hashing
public Hash HashType { get; init; }
#endif
+ ///
+ /// Current hash in bytes
+ ///
+#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
+ }
+ }
+
+ ///
+ /// Current hash as a string
+ ///
+#if NET48
+ public string CurrentHashString => ByteArrayToString(CurrentHashBytes);
+#else
+ public string? CurrentHashString => ByteArrayToString(CurrentHashBytes);
+#endif
+
+ #endregion
+
+ #region Private Fields
+
+ ///
+ /// Internal hasher being used for processing
+ ///
+ /// May be either a HashAlgorithm or NonCryptographicHashAlgorithm
#if NET48
private object _hasher;
#else
private object? _hasher;
#endif
+ #endregion
+
+ #region Constructors
+
+ ///
+ /// Constructor
+ ///
+ /// Hash type to instantiate
public Hasher(Hash hashType)
{
this.HashType = hashType;
@@ -69,12 +128,17 @@ namespace MPF.Core.Hashing
}
}
+ ///
public void Dispose()
{
if (_hasher is IDisposable disposable)
disposable.Dispose();
}
+ #endregion
+
+ #region Hashing
+
///
/// Process a buffer of some length with the internal hash algorithm
///
@@ -107,44 +171,9 @@ namespace MPF.Core.Hashing
}
}
- ///
- /// Get internal hash as a byte array
- ///
-#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;
- }
-
- ///
- /// Get internal hash as a string
- ///
-#if NET48
- public string GetHashString()
-#else
- public string? GetHashString()
-#endif
- {
- var hash = GetHash();
- if (hash == null)
- return null;
-
- return ByteArrayToString(hash);
- }
+ #region Helpers
///
/// Convert a byte array to a hex string
@@ -172,5 +201,7 @@ namespace MPF.Core.Hashing
return null;
}
}
+
+ #endregion
}
}
diff --git a/MPF.Core/InfoTool.cs b/MPF.Core/InfoTool.cs
index 049272ad..c11e1898 100644
--- a/MPF.Core/InfoTool.cs
+++ b/MPF.Core/InfoTool.cs
@@ -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();