Convert HashType from enum to static class with constants

Because the ordering of the enum was not guaranteed, both because of variances in .NET version and even library version, any change could mean that invisible version bumps would misalign. This attempts to fix that by converting HashType from an enum to a static class with a set of constants instead. This allows logical groupings and orderings without it affecting the values of each item.
This commit is contained in:
Matt Nadareski
2026-03-20 00:18:40 -04:00
parent 41efdaaf35
commit 00a5c6c2c5
10 changed files with 470 additions and 301 deletions

View File

@@ -29,8 +29,7 @@ namespace Hasher.Features
Console.WriteLine("Hash Name Parameter Name ");
Console.WriteLine("--------------------------------------------------------------");
var hashTypes = (HashType[])Enum.GetValues(typeof(HashType));
foreach (var hashType in hashTypes)
foreach (var hashType in HashType.AllHashes)
{
// Derive the parameter name
string paramName = $"{hashType}";

View File

@@ -46,7 +46,7 @@ namespace Hasher.Features
{
// Get the required variables
bool debug = GetBoolean(_debugName);
List<HashType> hashTypes = GetHashTypes(GetStringList(_typeName));
List<string> hashTypes = GetHashTypes(GetStringList(_typeName));
// Loop through all of the input files
for (int i = 0; i < Inputs.Count; i++)
@@ -64,9 +64,9 @@ namespace Hasher.Features
/// <summary>
/// Derive a list of hash types from a list of strings
/// </summary>
private static List<HashType> GetHashTypes(List<string> types)
private static List<string> GetHashTypes(List<string> types)
{
List<HashType> hashTypes = [];
List<string> hashTypes = [];
if (types.Count == 0)
{
hashTypes.Add(HashType.CRC32);
@@ -76,15 +76,15 @@ namespace Hasher.Features
}
else if (types.Contains("all"))
{
hashTypes = [.. (HashType[])Enum.GetValues(typeof(HashType))];
hashTypes = [.. HashType.AllHashes];
}
else
{
foreach (string typeString in types)
{
HashType? hashType = typeString.GetHashType();
if (hashType is not null && !hashTypes.Contains(hashType.Value))
hashTypes.Add(item: hashType.Value);
string? hashType = typeString.GetHashType();
if (hashType is not null && !hashTypes.Contains(hashType))
hashTypes.Add(item: hashType);
}
}
@@ -97,7 +97,7 @@ namespace Hasher.Features
/// <param name="path">File or directory path</param>
/// <param name="hashTypes">Set of hashes to retrieve</param>
/// <param name="debug">Enable debug output</param>
private static void PrintPathHashes(string path, List<HashType> hashTypes, bool debug)
private static void PrintPathHashes(string path, List<string> hashTypes, bool debug)
{
Console.WriteLine($"Checking possible path: {path}");
@@ -125,7 +125,7 @@ namespace Hasher.Features
/// <param name="file">File path</param>
/// <param name="hashTypes">Set of hashes to retrieve</param>
/// <param name="debug">Enable debug output</param>
private static void PrintFileHashes(string file, List<HashType> hashTypes, bool debug)
private static void PrintFileHashes(string file, List<string> hashTypes, bool debug)
{
Console.WriteLine($"Attempting to hash {file}, this may take a while...");
Console.WriteLine();
@@ -149,7 +149,7 @@ namespace Hasher.Features
// Output subset of available hashes
var builder = new StringBuilder();
foreach (HashType hashType in hashTypes)
foreach (string hashType in hashTypes)
{
// TODO: Make helper to pretty-print hash type names
if (hashes.TryGetValue(hashType, out string? hash) && hash is not null)

View File

@@ -15,13 +15,12 @@ namespace SabreTools.Hashing.Test
/// <summary>
/// Get an array of all hash types
/// </summary>
public static TheoryData<HashType> AllHashTypes
public static TheoryData<string> AllHashTypes
{
get
{
var values = Enum.GetValues<HashType>();
var set = new TheoryData<HashType>();
foreach (var value in values)
var set = new TheoryData<string>();
foreach (var value in HashType.AllHashes)
{
set.Add(value);
}
@@ -77,7 +76,7 @@ namespace SabreTools.Hashing.Test
[Theory]
[MemberData(nameof(AllHashTypes))]
public void GetFileHashesSerialTest(HashType hashType)
public void GetFileHashesSerialTest(string hashType)
{
var hashValue = HashTool.GetFileHash(_hashFilePath, hashType);
TestHelper.ValidateHash(hashType, hashValue);

View File

@@ -13,7 +13,7 @@ namespace SabreTools.Hashing.Test
private const long _hashFileSize = 125;
private static readonly Dictionary<HashType, string> _knownHashes = new()
private static readonly Dictionary<string, string> _knownHashes = new()
{
{HashType.Adler32, "08562d95"},
@@ -224,7 +224,7 @@ namespace SabreTools.Hashing.Test
/// <summary>
/// Validate the hashes in a hash dictionary
/// </summary>
public static void ValidateHashes(Dictionary<HashType, string?>? hashDict)
public static void ValidateHashes(Dictionary<string, string?>? hashDict)
{
Assert.NotNull(hashDict);
foreach (var hashType in _knownHashes.Keys)
@@ -236,7 +236,7 @@ namespace SabreTools.Hashing.Test
/// <summary>
/// Validate a single hash
/// </summary>
public static void ValidateHash(HashType hashType, string? hashValue)
public static void ValidateHash(string hashType, string? hashValue)
=> Assert.Equal(_knownHashes[hashType], hashValue);
/// <summary>

View File

@@ -9,13 +9,12 @@ namespace SabreTools.Hashing.Test
/// <summary>
/// Get an array of all hash types
/// </summary>
public static TheoryData<HashType> AllHashTypes
public static TheoryData<string> AllHashTypes
{
get
{
var values = Enum.GetValues<HashType>();
var set = new TheoryData<HashType>();
foreach (var value in values)
var set = new TheoryData<string>();
foreach (var value in HashType.AllHashes)
{
set.Add(value);
}
@@ -26,7 +25,7 @@ namespace SabreTools.Hashing.Test
[Theory]
[MemberData(nameof(AllHashTypes))]
public void GetZeroByteHashes(HashType hashType)
public void GetZeroByteHashes(string hashType)
{
var expected = ZeroHash.GetBytes(hashType);
var actual = HashTool.GetByteArrayHashArray([], hashType);
@@ -38,7 +37,7 @@ namespace SabreTools.Hashing.Test
[Theory]
[MemberData(nameof(AllHashTypes))]
public void GetZeroStringHashes(HashType hashType)
public void GetZeroStringHashes(string hashType)
{
var expected = ZeroHash.GetString(hashType);
var actual = HashTool.GetByteArrayHash([], hashType);

View File

@@ -6,7 +6,7 @@ namespace SabreTools.Hashing
/// Get the name of a given hash type, if possible
/// </summary>
/// TODO: This should be automated instead of hardcoded
public static string? GetHashName(this HashType hashType)
public static string? GetHashName(this string hashType)
{
return hashType switch
{
@@ -221,7 +221,7 @@ namespace SabreTools.Hashing
/// Get the hash type associated to a string, if possible
/// </summary>
/// TODO: This should be automated instead of hardcoded
public static HashType? GetHashType(this string? str)
public static string? GetHashType(this string? str)
{
// Ignore invalid strings
if (string.IsNullOrEmpty(str))

View File

@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.IO;
#if NET40_OR_GREATER || NETCOREAPP || NETSTANDARD2_0_OR_GREATER
@@ -29,7 +28,7 @@ namespace SabreTools.Hashing
crc32 = null; md5 = null; sha1 = null;
// Get all file hashes
HashType[] standardHashTypes = [HashType.CRC32, HashType.MD5, HashType.SHA1];
string[] standardHashTypes = [HashType.CRC32, HashType.MD5, HashType.SHA1];
var fileHashes = GetFileHashesAndSize(filename, standardHashTypes, out size);
if (fileHashes is null)
return false;
@@ -56,7 +55,7 @@ namespace SabreTools.Hashing
crc32 = null; md5 = null; sha1 = null;
// Get all file hashes
HashType[] standardHashTypes = [HashType.CRC32, HashType.MD5, HashType.SHA1];
string[] standardHashTypes = [HashType.CRC32, HashType.MD5, HashType.SHA1];
var fileHashes = GetByteArrayHashesAndSize(array, standardHashTypes, out size);
if (fileHashes is null)
return false;
@@ -83,7 +82,7 @@ namespace SabreTools.Hashing
crc32 = null; md5 = null; sha1 = null;
// Get all file hashes
HashType[] standardHashTypes = [HashType.CRC32, HashType.MD5, HashType.SHA1];
string[] standardHashTypes = [HashType.CRC32, HashType.MD5, HashType.SHA1];
var fileHashes = GetStreamHashesAndSize(stream, standardHashTypes, out size);
if (fileHashes is null)
return false;
@@ -104,7 +103,7 @@ namespace SabreTools.Hashing
/// </summary>
/// <param name="filename">Path to the input file</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, string?>? GetFileHashes(string filename)
public static Dictionary<string, string?>? GetFileHashes(string filename)
=> GetFileHashesAndSize(filename, out _);
/// <summary>
@@ -112,7 +111,7 @@ namespace SabreTools.Hashing
/// </summary>
/// <param name="filename">Path to the input file</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, byte[]?>? GetFileHashArrays(string filename)
public static Dictionary<string, byte[]?>? GetFileHashArrays(string filename)
=> GetFileHashArraysAndSize(filename, out _);
/// <summary>
@@ -121,7 +120,7 @@ namespace SabreTools.Hashing
/// <param name="filename">Path to the input file</param>
/// <param name="hashType">Hash type to get from the file</param>
/// <returns>Hash on success, null on error</returns>
public static string? GetFileHash(string filename, HashType hashType)
public static string? GetFileHash(string filename, string hashType)
=> GetFileHashAndSize(filename, hashType, out _);
/// <summary>
@@ -130,7 +129,7 @@ namespace SabreTools.Hashing
/// <param name="filename">Path to the input file</param>
/// <param name="hashType">Hash type to get from the file</param>
/// <returns>Hash on success, null on error</returns>
public static byte[]? GetFileHashArray(string filename, HashType hashType)
public static byte[]? GetFileHashArray(string filename, string hashType)
=> GetFileHashArrayAndSize(filename, hashType, out _);
/// <summary>
@@ -139,7 +138,7 @@ namespace SabreTools.Hashing
/// <param name="filename">Path to the input file</param>
/// <param name="hashTypes">Array of hash types to get from the file</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, string?>? GetFileHashes(string filename, HashType[] hashTypes)
public static Dictionary<string, string?>? GetFileHashes(string filename, string[] hashTypes)
=> GetFileHashesAndSize(filename, hashTypes, out _);
/// <summary>
@@ -148,7 +147,7 @@ namespace SabreTools.Hashing
/// <param name="filename">Path to the input file</param>
/// <param name="hashTypes">Array of hash types to get from the file</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, byte[]?>? GetFileHashArrays(string filename, HashType[] hashTypes)
public static Dictionary<string, byte[]?>? GetFileHashArrays(string filename, string[] hashTypes)
=> GetFileHashArraysAndSize(filename, hashTypes, out _);
#endregion
@@ -161,14 +160,8 @@ namespace SabreTools.Hashing
/// <param name="filename">Path to the input file</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, string?>? GetFileHashesAndSize(string filename, out long size)
{
// Create a hash array for all entries
HashType[] hashTypes = (HashType[])Enum.GetValues(typeof(HashType));
// Return the hashes from the stream
return GetFileHashesAndSize(filename, hashTypes, out size);
}
public static Dictionary<string, string?>? GetFileHashesAndSize(string filename, out long size)
=> GetFileHashesAndSize(filename, HashType.AllHashes, out size);
/// <summary>
/// Get hashes and size from an input file path
@@ -176,14 +169,8 @@ namespace SabreTools.Hashing
/// <param name="filename">Path to the input file</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, byte[]?>? GetFileHashArraysAndSize(string filename, out long size)
{
// Create a hash array for all entries
HashType[] hashTypes = (HashType[])Enum.GetValues(typeof(HashType));
// Return the hashes from the stream
return GetFileHashArraysAndSize(filename, hashTypes, out size);
}
public static Dictionary<string, byte[]?>? GetFileHashArraysAndSize(string filename, out long size)
=> GetFileHashArraysAndSize(filename, HashType.AllHashes, out size);
/// <summary>
/// Get a hash and size from an input file path
@@ -192,7 +179,7 @@ namespace SabreTools.Hashing
/// <param name="hashType">Hash type to get from the file</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Hash and size on success, null on error</returns>
public static string? GetFileHashAndSize(string filename, HashType hashType, out long size)
public static string? GetFileHashAndSize(string filename, string hashType, out long size)
{
var hashes = GetFileHashesAndSize(filename, [hashType], out size);
return hashes?[hashType];
@@ -205,7 +192,7 @@ namespace SabreTools.Hashing
/// <param name="hashType">Hash type to get from the file</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Hash and size on success, null on error</returns>
public static byte[]? GetFileHashArrayAndSize(string filename, HashType hashType, out long size)
public static byte[]? GetFileHashArrayAndSize(string filename, string hashType, out long size)
{
var hashes = GetFileHashArraysAndSize(filename, [hashType], out size);
return hashes?[hashType];
@@ -218,7 +205,7 @@ namespace SabreTools.Hashing
/// <param name="hashTypes">Array of hash types to get from the file</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, string?>? GetFileHashesAndSize(string filename, HashType[] hashTypes, out long size)
public static Dictionary<string, string?>? GetFileHashesAndSize(string filename, string[] hashTypes, out long size)
{
// If the file doesn't exist, we can't do anything
if (!File.Exists(filename))
@@ -241,7 +228,7 @@ namespace SabreTools.Hashing
/// <param name="hashTypes">Array of hash types to get from the file</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, byte[]?>? GetFileHashArraysAndSize(string filename, HashType[] hashTypes, out long size)
public static Dictionary<string, byte[]?>? GetFileHashArraysAndSize(string filename, string[] hashTypes, out long size)
{
// If the file doesn't exist, we can't do anything
if (!File.Exists(filename))
@@ -266,7 +253,7 @@ namespace SabreTools.Hashing
/// </summary>
/// <param name="input">Byte array to hash</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, string?>? GetByteArrayHashes(byte[] input)
public static Dictionary<string, string?>? GetByteArrayHashes(byte[] input)
=> GetByteArrayHashesAndSize(input, out _);
/// <summary>
@@ -274,7 +261,7 @@ namespace SabreTools.Hashing
/// </summary>
/// <param name="input">Byte array to hash</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, byte[]?>? GetByteArrayHashArrays(byte[] input)
public static Dictionary<string, byte[]?>? GetByteArrayHashArrays(byte[] input)
=> GetByteArrayHashArraysAndSize(input, out _);
/// <summary>
@@ -283,7 +270,7 @@ namespace SabreTools.Hashing
/// <param name="input">Byte array to hash</param>
/// <param name="hashType">Hash type to get from the file</param>
/// <returns>Hash on success, null on error</returns>
public static string? GetByteArrayHash(byte[] input, HashType hashType)
public static string? GetByteArrayHash(byte[] input, string hashType)
=> GetByteArrayHashAndSize(input, hashType, out _);
/// <summary>
@@ -292,7 +279,7 @@ namespace SabreTools.Hashing
/// <param name="input">Byte array to hash</param>
/// <param name="hashType">Hash type to get from the file</param>
/// <returns>Hash on success, null on error</returns>
public static byte[]? GetByteArrayHashArray(byte[] input, HashType hashType)
public static byte[]? GetByteArrayHashArray(byte[] input, string hashType)
=> GetByteArrayHashArrayAndSize(input, hashType, out _);
/// <summary>
@@ -301,7 +288,7 @@ namespace SabreTools.Hashing
/// <param name="input">Byte array to hash</param>
/// <param name="hashTypes">Array of hash types to get from the file</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, string?>? GetByteArrayHashes(byte[] input, HashType[] hashTypes)
public static Dictionary<string, string?>? GetByteArrayHashes(byte[] input, string[] hashTypes)
=> GetByteArrayHashesAndSize(input, hashTypes, out _);
/// <summary>
@@ -310,7 +297,7 @@ namespace SabreTools.Hashing
/// <param name="input">Byte array to hash</param>
/// <param name="hashTypes">Array of hash types to get from the file</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, byte[]?>? GetByteArrayHashArrays(byte[] input, HashType[] hashTypes)
public static Dictionary<string, byte[]?>? GetByteArrayHashArrays(byte[] input, string[] hashTypes)
=> GetByteArrayHashArraysAndSize(input, hashTypes, out _);
#endregion
@@ -323,14 +310,8 @@ namespace SabreTools.Hashing
/// <param name="input">Byte array to hash</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, string?>? GetByteArrayHashesAndSize(byte[] input, out long size)
{
// Create a hash array for all entries
HashType[] hashTypes = (HashType[])Enum.GetValues(typeof(HashType));
// Return the hashes from the stream
return GetStreamHashesAndSize(new MemoryStream(input), hashTypes, out size);
}
public static Dictionary<string, string?>? GetByteArrayHashesAndSize(byte[] input, out long size)
=> GetStreamHashesAndSize(new MemoryStream(input), HashType.AllHashes, out size);
/// <summary>
/// Get hashes from an input byte array
@@ -338,14 +319,8 @@ namespace SabreTools.Hashing
/// <param name="input">Byte array to hash</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, byte[]?>? GetByteArrayHashArraysAndSize(byte[] input, out long size)
{
// Create a hash array for all entries
HashType[] hashTypes = (HashType[])Enum.GetValues(typeof(HashType));
// Return the hashes from the stream
return GetStreamHashArraysAndSize(new MemoryStream(input), hashTypes, out size);
}
public static Dictionary<string, byte[]?>? GetByteArrayHashArraysAndSize(byte[] input, out long size)
=> GetStreamHashArraysAndSize(new MemoryStream(input), HashType.AllHashes, out size);
/// <summary>
/// Get a hash from an input byte array
@@ -354,7 +329,7 @@ namespace SabreTools.Hashing
/// <param name="hashType">Hash type to get from the file</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Hash on success, null on error</returns>
public static string? GetByteArrayHashAndSize(byte[] input, HashType hashType, out long size)
public static string? GetByteArrayHashAndSize(byte[] input, string hashType, out long size)
{
var hashes = GetStreamHashesAndSize(new MemoryStream(input), [hashType], out size);
return hashes?[hashType];
@@ -367,7 +342,7 @@ namespace SabreTools.Hashing
/// <param name="hashType">Hash type to get from the file</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Hash on success, null on error</returns>
public static byte[]? GetByteArrayHashArrayAndSize(byte[] input, HashType hashType, out long size)
public static byte[]? GetByteArrayHashArrayAndSize(byte[] input, string hashType, out long size)
{
var hashes = GetStreamHashArraysAndSize(new MemoryStream(input), [hashType], out size);
return hashes?[hashType];
@@ -380,7 +355,7 @@ namespace SabreTools.Hashing
/// <param name="hashTypes">Array of hash types to get from the file</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, string?>? GetByteArrayHashesAndSize(byte[] input, HashType[] hashTypes, out long size)
public static Dictionary<string, string?>? GetByteArrayHashesAndSize(byte[] input, string[] hashTypes, out long size)
=> GetStreamHashesAndSize(new MemoryStream(input), hashTypes, out size);
/// <summary>
@@ -390,7 +365,7 @@ namespace SabreTools.Hashing
/// <param name="hashTypes">Array of hash types to get from the file</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, byte[]?>? GetByteArrayHashArraysAndSize(byte[] input, HashType[] hashTypes, out long size)
public static Dictionary<string, byte[]?>? GetByteArrayHashArraysAndSize(byte[] input, string[] hashTypes, out long size)
=> GetStreamHashArraysAndSize(new MemoryStream(input), hashTypes, out size);
#endregion
@@ -403,7 +378,7 @@ namespace SabreTools.Hashing
/// <param name="input">Stream to hash</param>
/// <param name="leaveOpen">Indicates if the source stream should be left open after hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, string?>? GetStreamHashes(Stream input, bool leaveOpen = false)
public static Dictionary<string, string?>? GetStreamHashes(Stream input, bool leaveOpen = false)
=> GetStreamHashesAndSize(input, leaveOpen, out _);
/// <summary>
@@ -412,7 +387,7 @@ namespace SabreTools.Hashing
/// <param name="input">Stream to hash</param>
/// <param name="leaveOpen">Indicates if the source stream should be left open after hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, byte[]?>? GetStreamHashArrays(Stream input, bool leaveOpen = false)
public static Dictionary<string, byte[]?>? GetStreamHashArrays(Stream input, bool leaveOpen = false)
=> GetStreamHashArraysAndSize(input, leaveOpen, out _);
/// <summary>
@@ -422,7 +397,7 @@ namespace SabreTools.Hashing
/// <param name="hashType">Hash type to get from the file</param>
/// <param name="leaveOpen">Indicates if the source stream should be left open after hashing</param>
/// <returns>Hash on success, null on error</returns>
public static string? GetStreamHash(Stream input, HashType hashType, bool leaveOpen = false)
public static string? GetStreamHash(Stream input, string hashType, bool leaveOpen = false)
=> GetStreamHashAndSize(input, hashType, leaveOpen, out _);
/// <summary>
@@ -432,7 +407,7 @@ namespace SabreTools.Hashing
/// <param name="hashType">Hash type to get from the file</param>
/// <param name="leaveOpen">Indicates if the source stream should be left open after hashing</param>
/// <returns>Hash on success, null on error</returns>
public static byte[]? GetStreamHashArray(Stream input, HashType hashType, bool leaveOpen = false)
public static byte[]? GetStreamHashArray(Stream input, string hashType, bool leaveOpen = false)
=> GetStreamHashArrayAndSize(input, hashType, leaveOpen, out _);
/// <summary>
@@ -442,7 +417,7 @@ namespace SabreTools.Hashing
/// <param name="hashTypes">Array of hash types to get from the file</param>
/// <param name="leaveOpen">Indicates if the source stream should be left open after hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, string?>? GetStreamHashes(Stream input, HashType[] hashTypes, bool leaveOpen = false)
public static Dictionary<string, string?>? GetStreamHashes(Stream input, string[] hashTypes, bool leaveOpen = false)
=> GetStreamHashesAndSize(input, hashTypes, leaveOpen, out _);
/// <summary>
@@ -452,7 +427,7 @@ namespace SabreTools.Hashing
/// <param name="hashTypes">Array of hash types to get from the file</param>
/// <param name="leaveOpen">Indicates if the source stream should be left open after hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, byte[]?>? GetStreamHashArrays(Stream input, HashType[] hashTypes, bool leaveOpen = false)
public static Dictionary<string, byte[]?>? GetStreamHashArrays(Stream input, string[] hashTypes, bool leaveOpen = false)
=> GetStreamHashArraysAndSize(input, hashTypes, leaveOpen, out _);
#endregion
@@ -465,7 +440,7 @@ namespace SabreTools.Hashing
/// <param name="input">Stream to hash</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, string?>? GetStreamHashesAndSize(Stream input, out long size)
public static Dictionary<string, string?>? GetStreamHashesAndSize(Stream input, out long size)
=> GetStreamHashesAndSize(input, leaveOpen: false, out size);
/// <summary>
@@ -475,14 +450,8 @@ namespace SabreTools.Hashing
/// <param name="leaveOpen">Indicates if the source stream should be left open after hashing</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, string?>? GetStreamHashesAndSize(Stream input, bool leaveOpen, out long size)
{
// Create a hash array for all entries
HashType[] hashTypes = (HashType[])Enum.GetValues(typeof(HashType));
// Get the output hashes
return GetStreamHashesAndSize(input, hashTypes, leaveOpen, out size);
}
public static Dictionary<string, string?>? GetStreamHashesAndSize(Stream input, bool leaveOpen, out long size)
=> GetStreamHashesAndSize(input, HashType.AllHashes, leaveOpen, out size);
/// <summary>
/// Get hashes and size from an input Stream
@@ -490,7 +459,7 @@ namespace SabreTools.Hashing
/// <param name="input">Stream to hash</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, byte[]?>? GetStreamHashArraysAndSize(Stream input, out long size)
public static Dictionary<string, byte[]?>? GetStreamHashArraysAndSize(Stream input, out long size)
=> GetStreamHashArraysAndSize(input, leaveOpen: false, out size);
/// <summary>
@@ -500,14 +469,8 @@ namespace SabreTools.Hashing
/// <param name="leaveOpen">Indicates if the source stream should be left open after hashing</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, byte[]?>? GetStreamHashArraysAndSize(Stream input, bool leaveOpen, out long size)
{
// Create a hash array for all entries
HashType[] hashTypes = (HashType[])Enum.GetValues(typeof(HashType));
// Get the output hashes
return GetStreamHashArraysAndSize(input, hashTypes, leaveOpen, out size);
}
public static Dictionary<string, byte[]?>? GetStreamHashArraysAndSize(Stream input, bool leaveOpen, out long size)
=> GetStreamHashArraysAndSize(input, HashType.AllHashes, leaveOpen, out size);
/// <summary>
/// Get a hash and size from an input Stream
@@ -516,7 +479,7 @@ namespace SabreTools.Hashing
/// <param name="hashType">Hash type to get from the file</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Hash on success, null on error</returns>
public static string? GetStreamHashAndSize(Stream input, HashType hashType, out long size)
public static string? GetStreamHashAndSize(Stream input, string hashType, out long size)
=> GetStreamHashAndSize(input, hashType, leaveOpen: false, out size);
/// <summary>
@@ -527,7 +490,7 @@ namespace SabreTools.Hashing
/// <param name="leaveOpen">Indicates if the source stream should be left open after hashing</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Hash on success, null on error</returns>
public static string? GetStreamHashAndSize(Stream input, HashType hashType, bool leaveOpen, out long size)
public static string? GetStreamHashAndSize(Stream input, string hashType, bool leaveOpen, out long size)
{
var hashes = GetStreamHashesAndSize(input, [hashType], leaveOpen, out size);
return hashes?[hashType];
@@ -540,7 +503,7 @@ namespace SabreTools.Hashing
/// <param name="hashType">Hash type to get from the file</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Hash on success, null on error</returns>
public static byte[]? GetStreamHashArrayAndSize(Stream input, HashType hashType, out long size)
public static byte[]? GetStreamHashArrayAndSize(Stream input, string hashType, out long size)
=> GetStreamHashArrayAndSize(input, hashType, leaveOpen: false, out size);
/// <summary>
@@ -551,7 +514,7 @@ namespace SabreTools.Hashing
/// <param name="leaveOpen">Indicates if the source stream should be left open after hashing</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Hash on success, null on error</returns>
public static byte[]? GetStreamHashArrayAndSize(Stream input, HashType hashType, bool leaveOpen, out long size)
public static byte[]? GetStreamHashArrayAndSize(Stream input, string hashType, bool leaveOpen, out long size)
{
var hashes = GetStreamHashArraysAndSize(input, [hashType], leaveOpen, out size);
return hashes?[hashType];
@@ -564,7 +527,7 @@ namespace SabreTools.Hashing
/// <param name="hashTypes">Array of hash types to get from the file</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, string?>? GetStreamHashesAndSize(Stream input, HashType[] hashTypes, out long size)
public static Dictionary<string, string?>? GetStreamHashesAndSize(Stream input, string[] hashTypes, out long size)
=> GetStreamHashesAndSize(input, hashTypes, leaveOpen: false, out size);
/// <summary>
@@ -575,10 +538,10 @@ namespace SabreTools.Hashing
/// <param name="leaveOpen">Indicates if the source stream should be left open after hashing</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, string?>? GetStreamHashesAndSize(Stream input, HashType[] hashTypes, bool leaveOpen, out long size)
public static Dictionary<string, string?>? GetStreamHashesAndSize(Stream input, string[] hashTypes, bool leaveOpen, out long size)
{
// Create the output dictionary
var hashDict = new Dictionary<HashType, string?>();
var hashDict = new Dictionary<string, string?>();
try
{
@@ -623,7 +586,7 @@ namespace SabreTools.Hashing
/// <param name="hashTypes">Array of hash types to get from the file</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, byte[]?>? GetStreamHashArraysAndSize(Stream input, HashType[] hashTypes, out long size)
public static Dictionary<string, byte[]?>? GetStreamHashArraysAndSize(Stream input, string[] hashTypes, out long size)
=> GetStreamHashArraysAndSize(input, hashTypes, leaveOpen: false, out size);
/// <summary>
@@ -634,10 +597,10 @@ namespace SabreTools.Hashing
/// <param name="leaveOpen">Indicates if the source stream should be left open after hashing</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns>Dictionary containing hashes on success, null on error</returns>
public static Dictionary<HashType, byte[]?>? GetStreamHashArraysAndSize(Stream input, HashType[] hashTypes, bool leaveOpen, out long size)
public static Dictionary<string, byte[]?>? GetStreamHashArraysAndSize(Stream input, string[] hashTypes, bool leaveOpen, out long size)
{
// Create the output dictionary
var hashDict = new Dictionary<HashType, byte[]?>();
var hashDict = new Dictionary<string, byte[]?>();
try
{
@@ -683,19 +646,19 @@ namespace SabreTools.Hashing
/// <param name="leaveOpen">Indicates if the source stream should be left open after hashing</param>
/// <param name="size">Amount of bytes read during hashing</param>
/// <returns></returns>
private static Dictionary<HashType, HashWrapper>? GetStreamHashesInternal(Stream input, HashType[] hashTypes, bool leaveOpen, out long size)
private static Dictionary<string, HashWrapper>? GetStreamHashesInternal(Stream input, string[] hashTypes, bool leaveOpen, out long size)
{
// Create the output dictionary and size counter
var hashDict = new Dictionary<HashType, string?>();
var hashDict = new Dictionary<string, string?>();
size = 0;
try
{
// Get a list of hashers to run over the buffer
var hashers = new Dictionary<HashType, HashWrapper>();
var hashers = new Dictionary<string, HashWrapper>();
// Add hashers based on requested types
foreach (HashType hashType in hashTypes)
foreach (string hashType in hashTypes)
{
hashers[hashType] = new HashWrapper(hashType);
}

File diff suppressed because it is too large Load Diff

View File

@@ -17,7 +17,7 @@ namespace SabreTools.Hashing
/// <summary>
/// Hash type associated with the current state
/// </summary>
public readonly HashType HashType;
public readonly string HashName;
/// <summary>
/// Current hash in bytes
@@ -67,9 +67,9 @@ namespace SabreTools.Hashing
/// Constructor
/// </summary>
/// <param name="hashType">Hash type to instantiate</param>
public HashWrapper(HashType hashType)
public HashWrapper(string hashType)
{
HashType = hashType;
HashName = hashType.GetHashType() ?? string.Empty;
GetHasher();
}
@@ -78,7 +78,7 @@ namespace SabreTools.Hashing
/// </summary>
private void GetHasher()
{
_hasher = HashType switch
_hasher = HashName switch
{
HashType.Adler32 => new Adler32(),

View File

@@ -88,7 +88,7 @@ namespace SabreTools.Hashing
/// <summary>
/// Set of all known 0-byte outputs as strings
/// </summary>
private static readonly Dictionary<HashType, byte[]> _bytes = new()
private static readonly Dictionary<string, byte[]> _bytes = new()
{
{HashType.Adler32, [0x00, 0x00, 0x00, 0x01]},
@@ -394,7 +394,7 @@ namespace SabreTools.Hashing
/// <summary>
/// Set of all known 0-byte outputs as strings
/// </summary>
private static readonly Dictionary<HashType, string> _strings = new()
private static readonly Dictionary<string, string> _strings = new()
{
{HashType.Adler32, "00000001"},
@@ -605,12 +605,12 @@ namespace SabreTools.Hashing
/// </summary>
/// <param name="hashType">Hash type to get the value for</param>
/// <returns>Non-empty array containing the value on success, empty array on failure</returns>
public static byte[] GetBytes(HashType hashType)
public static byte[] GetBytes(string hashType)
{
if (!_strings.ContainsKey(hashType))
if (!_bytes.TryGetValue(hashType, out byte[]? value))
return [];
return _bytes[hashType];
return value;
}
/// <summary>
@@ -618,12 +618,12 @@ namespace SabreTools.Hashing
/// </summary>
/// <param name="hashType">Hash type to get the value for</param>
/// <returns>Non-empty string containing the value on success, empty string on failure</returns>
public static string GetString(HashType hashType)
public static string GetString(string hashType)
{
if (!_strings.ContainsKey(hashType))
if (!_strings.TryGetValue(hashType, out string? value))
return string.Empty;
return _strings[hashType];
return value;
}
}
}