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

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