From 00a5c6c2c5a7a2aed3d331a2e98f2fe9e7dae124 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Fri, 20 Mar 2026 00:18:40 -0400 Subject: [PATCH] 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. --- Hasher/Features/ListFeature.cs | 3 +- Hasher/Features/MainFeature.cs | 20 +- SabreTools.Hashing.Test/HashToolTests.cs | 9 +- SabreTools.Hashing.Test/TestHelper.cs | 6 +- SabreTools.Hashing.Test/ZeroHashTests.cs | 11 +- SabreTools.Hashing/Extensions.cs | 4 +- SabreTools.Hashing/HashTool.cs | 151 +++---- SabreTools.Hashing/HashType.cs | 543 ++++++++++++++++------- SabreTools.Hashing/HashWrapper.cs | 8 +- SabreTools.Hashing/ZeroHash.cs | 16 +- 10 files changed, 470 insertions(+), 301 deletions(-) diff --git a/Hasher/Features/ListFeature.cs b/Hasher/Features/ListFeature.cs index 22c6ab2..61998d2 100644 --- a/Hasher/Features/ListFeature.cs +++ b/Hasher/Features/ListFeature.cs @@ -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}"; diff --git a/Hasher/Features/MainFeature.cs b/Hasher/Features/MainFeature.cs index 7fcd8a6..76b2e01 100644 --- a/Hasher/Features/MainFeature.cs +++ b/Hasher/Features/MainFeature.cs @@ -46,7 +46,7 @@ namespace Hasher.Features { // Get the required variables bool debug = GetBoolean(_debugName); - List hashTypes = GetHashTypes(GetStringList(_typeName)); + List 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 /// /// Derive a list of hash types from a list of strings /// - private static List GetHashTypes(List types) + private static List GetHashTypes(List types) { - List hashTypes = []; + List 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 /// File or directory path /// Set of hashes to retrieve /// Enable debug output - private static void PrintPathHashes(string path, List hashTypes, bool debug) + private static void PrintPathHashes(string path, List hashTypes, bool debug) { Console.WriteLine($"Checking possible path: {path}"); @@ -125,7 +125,7 @@ namespace Hasher.Features /// File path /// Set of hashes to retrieve /// Enable debug output - private static void PrintFileHashes(string file, List hashTypes, bool debug) + private static void PrintFileHashes(string file, List 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) diff --git a/SabreTools.Hashing.Test/HashToolTests.cs b/SabreTools.Hashing.Test/HashToolTests.cs index c5f3160..29813fc 100644 --- a/SabreTools.Hashing.Test/HashToolTests.cs +++ b/SabreTools.Hashing.Test/HashToolTests.cs @@ -15,13 +15,12 @@ namespace SabreTools.Hashing.Test /// /// Get an array of all hash types /// - public static TheoryData AllHashTypes + public static TheoryData AllHashTypes { get { - var values = Enum.GetValues(); - var set = new TheoryData(); - foreach (var value in values) + var set = new TheoryData(); + 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); diff --git a/SabreTools.Hashing.Test/TestHelper.cs b/SabreTools.Hashing.Test/TestHelper.cs index 4712632..6a5f9c8 100644 --- a/SabreTools.Hashing.Test/TestHelper.cs +++ b/SabreTools.Hashing.Test/TestHelper.cs @@ -13,7 +13,7 @@ namespace SabreTools.Hashing.Test private const long _hashFileSize = 125; - private static readonly Dictionary _knownHashes = new() + private static readonly Dictionary _knownHashes = new() { {HashType.Adler32, "08562d95"}, @@ -224,7 +224,7 @@ namespace SabreTools.Hashing.Test /// /// Validate the hashes in a hash dictionary /// - public static void ValidateHashes(Dictionary? hashDict) + public static void ValidateHashes(Dictionary? hashDict) { Assert.NotNull(hashDict); foreach (var hashType in _knownHashes.Keys) @@ -236,7 +236,7 @@ namespace SabreTools.Hashing.Test /// /// Validate a single hash /// - public static void ValidateHash(HashType hashType, string? hashValue) + public static void ValidateHash(string hashType, string? hashValue) => Assert.Equal(_knownHashes[hashType], hashValue); /// diff --git a/SabreTools.Hashing.Test/ZeroHashTests.cs b/SabreTools.Hashing.Test/ZeroHashTests.cs index f043453..c2104c3 100644 --- a/SabreTools.Hashing.Test/ZeroHashTests.cs +++ b/SabreTools.Hashing.Test/ZeroHashTests.cs @@ -9,13 +9,12 @@ namespace SabreTools.Hashing.Test /// /// Get an array of all hash types /// - public static TheoryData AllHashTypes + public static TheoryData AllHashTypes { get { - var values = Enum.GetValues(); - var set = new TheoryData(); - foreach (var value in values) + var set = new TheoryData(); + 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); diff --git a/SabreTools.Hashing/Extensions.cs b/SabreTools.Hashing/Extensions.cs index a6f3390..2510a22 100644 --- a/SabreTools.Hashing/Extensions.cs +++ b/SabreTools.Hashing/Extensions.cs @@ -6,7 +6,7 @@ namespace SabreTools.Hashing /// Get the name of a given hash type, if possible /// /// 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 /// /// 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)) diff --git a/SabreTools.Hashing/HashTool.cs b/SabreTools.Hashing/HashTool.cs index c8539ca..b3962b4 100644 --- a/SabreTools.Hashing/HashTool.cs +++ b/SabreTools.Hashing/HashTool.cs @@ -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 /// /// Path to the input file /// Dictionary containing hashes on success, null on error - public static Dictionary? GetFileHashes(string filename) + public static Dictionary? GetFileHashes(string filename) => GetFileHashesAndSize(filename, out _); /// @@ -112,7 +111,7 @@ namespace SabreTools.Hashing /// /// Path to the input file /// Dictionary containing hashes on success, null on error - public static Dictionary? GetFileHashArrays(string filename) + public static Dictionary? GetFileHashArrays(string filename) => GetFileHashArraysAndSize(filename, out _); /// @@ -121,7 +120,7 @@ namespace SabreTools.Hashing /// Path to the input file /// Hash type to get from the file /// Hash on success, null on error - public static string? GetFileHash(string filename, HashType hashType) + public static string? GetFileHash(string filename, string hashType) => GetFileHashAndSize(filename, hashType, out _); /// @@ -130,7 +129,7 @@ namespace SabreTools.Hashing /// Path to the input file /// Hash type to get from the file /// Hash on success, null on error - public static byte[]? GetFileHashArray(string filename, HashType hashType) + public static byte[]? GetFileHashArray(string filename, string hashType) => GetFileHashArrayAndSize(filename, hashType, out _); /// @@ -139,7 +138,7 @@ namespace SabreTools.Hashing /// Path to the input file /// Array of hash types to get from the file /// Dictionary containing hashes on success, null on error - public static Dictionary? GetFileHashes(string filename, HashType[] hashTypes) + public static Dictionary? GetFileHashes(string filename, string[] hashTypes) => GetFileHashesAndSize(filename, hashTypes, out _); /// @@ -148,7 +147,7 @@ namespace SabreTools.Hashing /// Path to the input file /// Array of hash types to get from the file /// Dictionary containing hashes on success, null on error - public static Dictionary? GetFileHashArrays(string filename, HashType[] hashTypes) + public static Dictionary? GetFileHashArrays(string filename, string[] hashTypes) => GetFileHashArraysAndSize(filename, hashTypes, out _); #endregion @@ -161,14 +160,8 @@ namespace SabreTools.Hashing /// Path to the input file /// Amount of bytes read during hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? 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? GetFileHashesAndSize(string filename, out long size) + => GetFileHashesAndSize(filename, HashType.AllHashes, out size); /// /// Get hashes and size from an input file path @@ -176,14 +169,8 @@ namespace SabreTools.Hashing /// Path to the input file /// Amount of bytes read during hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? 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? GetFileHashArraysAndSize(string filename, out long size) + => GetFileHashArraysAndSize(filename, HashType.AllHashes, out size); /// /// Get a hash and size from an input file path @@ -192,7 +179,7 @@ namespace SabreTools.Hashing /// Hash type to get from the file /// Amount of bytes read during hashing /// Hash and size on success, null on error - 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 /// Hash type to get from the file /// Amount of bytes read during hashing /// Hash and size on success, null on error - 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 /// Array of hash types to get from the file /// Amount of bytes read during hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? GetFileHashesAndSize(string filename, HashType[] hashTypes, out long size) + public static Dictionary? 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 /// Array of hash types to get from the file /// Amount of bytes read during hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? GetFileHashArraysAndSize(string filename, HashType[] hashTypes, out long size) + public static Dictionary? 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 /// /// Byte array to hash /// Dictionary containing hashes on success, null on error - public static Dictionary? GetByteArrayHashes(byte[] input) + public static Dictionary? GetByteArrayHashes(byte[] input) => GetByteArrayHashesAndSize(input, out _); /// @@ -274,7 +261,7 @@ namespace SabreTools.Hashing /// /// Byte array to hash /// Dictionary containing hashes on success, null on error - public static Dictionary? GetByteArrayHashArrays(byte[] input) + public static Dictionary? GetByteArrayHashArrays(byte[] input) => GetByteArrayHashArraysAndSize(input, out _); /// @@ -283,7 +270,7 @@ namespace SabreTools.Hashing /// Byte array to hash /// Hash type to get from the file /// Hash on success, null on error - public static string? GetByteArrayHash(byte[] input, HashType hashType) + public static string? GetByteArrayHash(byte[] input, string hashType) => GetByteArrayHashAndSize(input, hashType, out _); /// @@ -292,7 +279,7 @@ namespace SabreTools.Hashing /// Byte array to hash /// Hash type to get from the file /// Hash on success, null on error - public static byte[]? GetByteArrayHashArray(byte[] input, HashType hashType) + public static byte[]? GetByteArrayHashArray(byte[] input, string hashType) => GetByteArrayHashArrayAndSize(input, hashType, out _); /// @@ -301,7 +288,7 @@ namespace SabreTools.Hashing /// Byte array to hash /// Array of hash types to get from the file /// Dictionary containing hashes on success, null on error - public static Dictionary? GetByteArrayHashes(byte[] input, HashType[] hashTypes) + public static Dictionary? GetByteArrayHashes(byte[] input, string[] hashTypes) => GetByteArrayHashesAndSize(input, hashTypes, out _); /// @@ -310,7 +297,7 @@ namespace SabreTools.Hashing /// Byte array to hash /// Array of hash types to get from the file /// Dictionary containing hashes on success, null on error - public static Dictionary? GetByteArrayHashArrays(byte[] input, HashType[] hashTypes) + public static Dictionary? GetByteArrayHashArrays(byte[] input, string[] hashTypes) => GetByteArrayHashArraysAndSize(input, hashTypes, out _); #endregion @@ -323,14 +310,8 @@ namespace SabreTools.Hashing /// Byte array to hash /// Amount of bytes read during hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? 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? GetByteArrayHashesAndSize(byte[] input, out long size) + => GetStreamHashesAndSize(new MemoryStream(input), HashType.AllHashes, out size); /// /// Get hashes from an input byte array @@ -338,14 +319,8 @@ namespace SabreTools.Hashing /// Byte array to hash /// Amount of bytes read during hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? 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? GetByteArrayHashArraysAndSize(byte[] input, out long size) + => GetStreamHashArraysAndSize(new MemoryStream(input), HashType.AllHashes, out size); /// /// Get a hash from an input byte array @@ -354,7 +329,7 @@ namespace SabreTools.Hashing /// Hash type to get from the file /// Amount of bytes read during hashing /// Hash on success, null on error - 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 /// Hash type to get from the file /// Amount of bytes read during hashing /// Hash on success, null on error - 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 /// Array of hash types to get from the file /// Amount of bytes read during hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? GetByteArrayHashesAndSize(byte[] input, HashType[] hashTypes, out long size) + public static Dictionary? GetByteArrayHashesAndSize(byte[] input, string[] hashTypes, out long size) => GetStreamHashesAndSize(new MemoryStream(input), hashTypes, out size); /// @@ -390,7 +365,7 @@ namespace SabreTools.Hashing /// Array of hash types to get from the file /// Amount of bytes read during hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? GetByteArrayHashArraysAndSize(byte[] input, HashType[] hashTypes, out long size) + public static Dictionary? GetByteArrayHashArraysAndSize(byte[] input, string[] hashTypes, out long size) => GetStreamHashArraysAndSize(new MemoryStream(input), hashTypes, out size); #endregion @@ -403,7 +378,7 @@ namespace SabreTools.Hashing /// Stream to hash /// Indicates if the source stream should be left open after hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? GetStreamHashes(Stream input, bool leaveOpen = false) + public static Dictionary? GetStreamHashes(Stream input, bool leaveOpen = false) => GetStreamHashesAndSize(input, leaveOpen, out _); /// @@ -412,7 +387,7 @@ namespace SabreTools.Hashing /// Stream to hash /// Indicates if the source stream should be left open after hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? GetStreamHashArrays(Stream input, bool leaveOpen = false) + public static Dictionary? GetStreamHashArrays(Stream input, bool leaveOpen = false) => GetStreamHashArraysAndSize(input, leaveOpen, out _); /// @@ -422,7 +397,7 @@ namespace SabreTools.Hashing /// Hash type to get from the file /// Indicates if the source stream should be left open after hashing /// Hash on success, null on error - 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 _); /// @@ -432,7 +407,7 @@ namespace SabreTools.Hashing /// Hash type to get from the file /// Indicates if the source stream should be left open after hashing /// Hash on success, null on error - 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 _); /// @@ -442,7 +417,7 @@ namespace SabreTools.Hashing /// Array of hash types to get from the file /// Indicates if the source stream should be left open after hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? GetStreamHashes(Stream input, HashType[] hashTypes, bool leaveOpen = false) + public static Dictionary? GetStreamHashes(Stream input, string[] hashTypes, bool leaveOpen = false) => GetStreamHashesAndSize(input, hashTypes, leaveOpen, out _); /// @@ -452,7 +427,7 @@ namespace SabreTools.Hashing /// Array of hash types to get from the file /// Indicates if the source stream should be left open after hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? GetStreamHashArrays(Stream input, HashType[] hashTypes, bool leaveOpen = false) + public static Dictionary? GetStreamHashArrays(Stream input, string[] hashTypes, bool leaveOpen = false) => GetStreamHashArraysAndSize(input, hashTypes, leaveOpen, out _); #endregion @@ -465,7 +440,7 @@ namespace SabreTools.Hashing /// Stream to hash /// Amount of bytes read during hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? GetStreamHashesAndSize(Stream input, out long size) + public static Dictionary? GetStreamHashesAndSize(Stream input, out long size) => GetStreamHashesAndSize(input, leaveOpen: false, out size); /// @@ -475,14 +450,8 @@ namespace SabreTools.Hashing /// Indicates if the source stream should be left open after hashing /// Amount of bytes read during hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? 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? GetStreamHashesAndSize(Stream input, bool leaveOpen, out long size) + => GetStreamHashesAndSize(input, HashType.AllHashes, leaveOpen, out size); /// /// Get hashes and size from an input Stream @@ -490,7 +459,7 @@ namespace SabreTools.Hashing /// Stream to hash /// Amount of bytes read during hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? GetStreamHashArraysAndSize(Stream input, out long size) + public static Dictionary? GetStreamHashArraysAndSize(Stream input, out long size) => GetStreamHashArraysAndSize(input, leaveOpen: false, out size); /// @@ -500,14 +469,8 @@ namespace SabreTools.Hashing /// Indicates if the source stream should be left open after hashing /// Amount of bytes read during hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? 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? GetStreamHashArraysAndSize(Stream input, bool leaveOpen, out long size) + => GetStreamHashArraysAndSize(input, HashType.AllHashes, leaveOpen, out size); /// /// Get a hash and size from an input Stream @@ -516,7 +479,7 @@ namespace SabreTools.Hashing /// Hash type to get from the file /// Amount of bytes read during hashing /// Hash on success, null on error - 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); /// @@ -527,7 +490,7 @@ namespace SabreTools.Hashing /// Indicates if the source stream should be left open after hashing /// Amount of bytes read during hashing /// Hash on success, null on error - 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 /// Hash type to get from the file /// Amount of bytes read during hashing /// Hash on success, null on error - 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); /// @@ -551,7 +514,7 @@ namespace SabreTools.Hashing /// Indicates if the source stream should be left open after hashing /// Amount of bytes read during hashing /// Hash on success, null on error - 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 /// Array of hash types to get from the file /// Amount of bytes read during hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? GetStreamHashesAndSize(Stream input, HashType[] hashTypes, out long size) + public static Dictionary? GetStreamHashesAndSize(Stream input, string[] hashTypes, out long size) => GetStreamHashesAndSize(input, hashTypes, leaveOpen: false, out size); /// @@ -575,10 +538,10 @@ namespace SabreTools.Hashing /// Indicates if the source stream should be left open after hashing /// Amount of bytes read during hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? GetStreamHashesAndSize(Stream input, HashType[] hashTypes, bool leaveOpen, out long size) + public static Dictionary? GetStreamHashesAndSize(Stream input, string[] hashTypes, bool leaveOpen, out long size) { // Create the output dictionary - var hashDict = new Dictionary(); + var hashDict = new Dictionary(); try { @@ -623,7 +586,7 @@ namespace SabreTools.Hashing /// Array of hash types to get from the file /// Amount of bytes read during hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? GetStreamHashArraysAndSize(Stream input, HashType[] hashTypes, out long size) + public static Dictionary? GetStreamHashArraysAndSize(Stream input, string[] hashTypes, out long size) => GetStreamHashArraysAndSize(input, hashTypes, leaveOpen: false, out size); /// @@ -634,10 +597,10 @@ namespace SabreTools.Hashing /// Indicates if the source stream should be left open after hashing /// Amount of bytes read during hashing /// Dictionary containing hashes on success, null on error - public static Dictionary? GetStreamHashArraysAndSize(Stream input, HashType[] hashTypes, bool leaveOpen, out long size) + public static Dictionary? GetStreamHashArraysAndSize(Stream input, string[] hashTypes, bool leaveOpen, out long size) { // Create the output dictionary - var hashDict = new Dictionary(); + var hashDict = new Dictionary(); try { @@ -683,19 +646,19 @@ namespace SabreTools.Hashing /// Indicates if the source stream should be left open after hashing /// Amount of bytes read during hashing /// - private static Dictionary? GetStreamHashesInternal(Stream input, HashType[] hashTypes, bool leaveOpen, out long size) + private static Dictionary? GetStreamHashesInternal(Stream input, string[] hashTypes, bool leaveOpen, out long size) { // Create the output dictionary and size counter - var hashDict = new Dictionary(); + var hashDict = new Dictionary(); size = 0; try { // Get a list of hashers to run over the buffer - var hashers = new Dictionary(); + var hashers = new Dictionary(); // Add hashers based on requested types - foreach (HashType hashType in hashTypes) + foreach (string hashType in hashTypes) { hashers[hashType] = new HashWrapper(hashType); } diff --git a/SabreTools.Hashing/HashType.cs b/SabreTools.Hashing/HashType.cs index 5229efe..d97bba7 100644 --- a/SabreTools.Hashing/HashType.cs +++ b/SabreTools.Hashing/HashType.cs @@ -3,20 +3,18 @@ namespace SabreTools.Hashing /// /// Available hashing and checksumming types /// - /// TODO: Investigate ways of dealing with consistent ordering - /// TODO: Investigate turning this into a string enum analogue - public enum HashType + public static class HashType { /// /// Mark Adler's 32-bit checksum /// - Adler32, + public const string Adler32 = "Adler32"; #if NET7_0_OR_GREATER /// /// BLAKE3 512-bit digest /// - BLAKE3, + public const string BLAKE3 = "BLAKE3"; #endif #region CRC @@ -26,12 +24,12 @@ namespace SabreTools.Hashing /// /// CRC 1-bit checksum (CRC-1/ZERO [Parity bit with 0 start]) /// - CRC1_ZERO, + public const string CRC1_ZERO = "CRC1_ZERO"; /// /// CRC 1-bit checksum (CRC-1/ONE [Parity bit with 1 start]) /// - CRC1_ONE, + public const string CRC1_ONE = "CRC1_ONE"; #endregion @@ -40,12 +38,12 @@ namespace SabreTools.Hashing /// /// CRC 3-bit checksum (CRC-3/GSM) /// - CRC3_GSM, + public const string CRC3_GSM = "CRC3_GSM"; /// /// CRC 3-bit checksum (CRC-3/ROHC) /// - CRC3_ROHC, + public const string CRC3_ROHC = "CRC3_ROHC"; #endregion @@ -54,12 +52,12 @@ namespace SabreTools.Hashing /// /// CRC 4-bit checksum (CRC-4/G-704 [CRC-4/ITU]) /// - CRC4_G704, + public const string CRC4_G704 = "CRC4_G704"; /// /// CRC 4-bit checksum (CRC-4/INTERLAKEN) /// - CRC4_INTERLAKEN, + public const string CRC4_INTERLAKEN = "CRC4_INTERLAKEN"; #endregion @@ -68,17 +66,17 @@ namespace SabreTools.Hashing /// /// CRC 5-bit checksum (CRC-5/EPC-C1G2 [CRC-5/EPC]) /// - CRC5_EPCC1G2, + public const string CRC5_EPCC1G2 = "CRC5_EPCC1G2"; /// /// CRC 5-bit checksum (CRC-5/G-704 [CRC-5/ITU]) /// - CRC5_G704, + public const string CRC5_G704 = "CRC5_G704"; /// /// CRC 5-bit checksum (CRC-5/USB) /// - CRC5_USB, + public const string CRC5_USB = "CRC5_USB"; #endregion @@ -87,27 +85,27 @@ namespace SabreTools.Hashing /// /// CRC 6-bit checksum (CRC-6/CDMA2000-A) /// - CRC6_CDMA2000A, + public const string CRC6_CDMA2000A = "CRC6_CDMA2000A"; /// /// CRC 6-bit checksum (CRC-6/CDMA2000-B) /// - CRC6_CDMA2000B, + public const string CRC6_CDMA2000B = "CRC6_CDMA2000B"; /// /// CRC 6-bit checksum (CRC-6/DARC) /// - CRC6_DARC, + public const string CRC6_DARC = "CRC6_DARC"; /// /// CRC 6-bit checksum (CRC-6/G-704 [CRC-6/ITU]) /// - CRC6_G704, + public const string CRC6_G704 = "CRC6_G704"; /// /// CRC 6-bit checksum (CRC-6/GSM) /// - CRC6_GSM, + public const string CRC6_GSM = "CRC6_GSM"; #endregion @@ -116,17 +114,17 @@ namespace SabreTools.Hashing /// /// CRC 7-bit checksum (CRC-7/MMC [CRC-7]) /// - CRC7_MMC, + public const string CRC7_MMC = "CRC7_MMC"; /// /// CRC 7-bit checksum (CRC-7/ROHC) /// - CRC7_ROHC, + public const string CRC7_ROHC = "CRC7_ROHC"; /// /// CRC 7-bit checksum (CRC-7/UMTS) /// - CRC7_UMTS, + public const string CRC7_UMTS = "CRC7_UMTS"; #endregion @@ -136,107 +134,107 @@ namespace SabreTools.Hashing /// CRC 8-bit checksum /// /// Identical to - CRC8, + public const string CRC8 = "CRC8"; /// /// CRC 8-bit checksum (CRC-8/AUTOSAR) /// - CRC8_AUTOSAR, + public const string CRC8_AUTOSAR = "CRC8_AUTOSAR"; /// /// CRC 8-bit checksum (CRC-8/BLUETOOTH) /// - CRC8_BLUETOOTH, + public const string CRC8_BLUETOOTH = "CRC8_BLUETOOTH"; /// /// CRC 8-bit checksum (CRC-8/CDMA2000) /// - CRC8_CDMA2000, + public const string CRC8_CDMA2000 = "CRC8_CDMA2000"; /// /// CRC 8-bit checksum (CRC-8/DARC) /// - CRC8_DARC, + public const string CRC8_DARC = "CRC8_DARC"; /// /// CRC 8-bit checksum (CRC-8/DVB-S2) /// - CRC8_DVBS2, + public const string CRC8_DVBS2 = "CRC8_DVBS2"; /// /// CRC 8-bit checksum (CRC-8/GSM-A) /// - CRC8_GSMA, + public const string CRC8_GSMA = "CRC8_GSMA"; /// /// CRC 8-bit checksum (CRC-8/GSM-B) /// - CRC8_GSMB, + public const string CRC8_GSMB = "CRC8_GSMB"; /// /// CRC 8-bit checksum (CRC-8/HITAG) /// - CRC8_HITAG, + public const string CRC8_HITAG = "CRC8_HITAG"; /// /// CRC 8-bit checksum (CRC-8/I-432-1 [CRC-8/ITU]) /// - CRC8_I4321, + public const string CRC8_I4321 = "CRC8_I4321"; /// /// CRC 8-bit checksum (CRC-8/I-CODE) /// - CRC8_ICODE, + public const string CRC8_ICODE = "CRC8_ICODE"; /// /// CRC 8-bit checksum (CRC-8/LTE) /// - CRC8_LTE, + public const string CRC8_LTE = "CRC8_LTE"; /// /// CRC 8-bit checksum (CRC-8/MAXIM-DOW [CRC-8/MAXIM, DOW-CRC]) /// - CRC8_MAXIMDOW, + public const string CRC8_MAXIMDOW = "CRC8_MAXIMDOW"; /// /// CRC 8-bit checksum (CRC-8/MIFARE-MAD) /// - CRC8_MIFAREMAD, + public const string CRC8_MIFAREMAD = "CRC8_MIFAREMAD"; /// /// CRC 8-bit checksum (CRC-8/NRSC-5) /// - CRC8_NRSC5, + public const string CRC8_NRSC5 = "CRC8_NRSC5"; /// /// CRC 8-bit checksum (CRC-8/OPENSAFETY) /// - CRC8_OPENSAFETY, + public const string CRC8_OPENSAFETY = "CRC8_OPENSAFETY"; /// /// CRC 8-bit checksum (CRC-8/ROHC) /// - CRC8_ROHC, + public const string CRC8_ROHC = "CRC8_ROHC"; /// /// CRC 8-bit checksum (CRC-8/SAE-J1850) /// - CRC8_SAEJ1850, + public const string CRC8_SAEJ1850 = "CRC8_SAEJ1850"; /// /// CRC 8-bit checksum (CRC-8/SMBUS [CRC-8]) /// - CRC8_SMBUS, + public const string CRC8_SMBUS = "CRC8_SMBUS"; /// /// CRC 8-bit checksum (CRC-8/TECH-3250 [CRC-8/AES, CRC-8/EBU]) /// - CRC8_TECH3250, + public const string CRC8_TECH3250 = "CRC8_TECH3250"; /// /// CRC 8-bit checksum (CRC-8/WCDMA) /// - CRC8_WCDMA, + public const string CRC8_WCDMA = "CRC8_WCDMA"; #endregion @@ -245,17 +243,17 @@ namespace SabreTools.Hashing /// /// CRC 10-bit checksum (CRC-10/ATM [CRC-10, CRC-10/I-610]) /// - CRC10_ATM, + public const string CRC10_ATM = "CRC10_ATM"; /// /// CRC 10-bit checksum (CRC-10/CDMA2000) /// - CRC10_CDMA2000, + public const string CRC10_CDMA2000 = "CRC10_CDMA2000"; /// /// CRC 10-bit checksum (CRC-10/GSM) /// - CRC10_GSM, + public const string CRC10_GSM = "CRC10_GSM"; #endregion @@ -264,12 +262,12 @@ namespace SabreTools.Hashing /// /// CRC 11-bit checksum (CRC-11/FLEXRAY [CRC-11]) /// - CRC11_FLEXRAY, + public const string CRC11_FLEXRAY = "CRC11_FLEXRAY"; /// /// CRC 11-bit checksum (CRC-11/UMTS) /// - CRC11_UMTS, + public const string CRC11_UMTS = "CRC11_UMTS"; #endregion @@ -278,22 +276,22 @@ namespace SabreTools.Hashing /// /// CRC 12-bit checksum (CRC-12/CDMA2000) /// - CRC12_CDMA2000, + public const string CRC12_CDMA2000 = "CRC12_CDMA2000"; /// /// CRC 12-bit checksum (CRC-12/DECT [X-CRC-12]) /// - CRC12_DECT, + public const string CRC12_DECT = "CRC12_DECT"; /// /// CRC 12-bit checksum (CRC-12/GSM) /// - CRC12_GSM, + public const string CRC12_GSM = "CRC12_GSM"; /// /// CRC 12-bit checksum (CRC-12/UMTS [CRC-12/3GPP]) /// - CRC12_UMTS, + public const string CRC12_UMTS = "CRC12_UMTS"; #endregion @@ -302,7 +300,7 @@ namespace SabreTools.Hashing /// /// CRC 13-bit checksum (CRC-13/BBC) /// - CRC13_BBC, + public const string CRC13_BBC = "CRC13_BBC"; #endregion @@ -311,12 +309,12 @@ namespace SabreTools.Hashing /// /// CRC 14-bit checksum (CRC-14/DARC) /// - CRC14_DARC, + public const string CRC14_DARC = "CRC14_DARC"; /// /// CRC 14-bit checksum (CRC-14/GSM) /// - CRC14_GSM, + public const string CRC14_GSM = "CRC14_GSM"; #endregion @@ -325,12 +323,12 @@ namespace SabreTools.Hashing /// /// CRC 15-bit checksum (CRC-15/CAN [CRC-15]) /// - CRC15_CAN, + public const string CRC15_CAN = "CRC15_CAN"; /// /// CRC 15-bit checksum (CRC-15/MPT1327) /// - CRC15_MPT1327, + public const string CRC15_MPT1327 = "CRC15_MPT1327"; #endregion @@ -340,162 +338,162 @@ namespace SabreTools.Hashing /// CRC 16-bit checksum /// /// Identical to - CRC16, + public const string CRC16 = "CRC16"; /// /// CRC 16-bit checksum (CRC-16/ARC [ARC, CRC-16, CRC-16/LHA, CRC-IBM]) /// - CRC16_ARC, + public const string CRC16_ARC = "CRC16_ARC"; /// /// CRC 16-bit checksum (CRC-16/CDMA2000) /// - CRC16_CDMA2000, + public const string CRC16_CDMA2000 = "CRC16_CDMA2000"; /// /// CRC 16-bit checksum (CRC-16/CMS) /// - CRC16_CMS, + public const string CRC16_CMS = "CRC16_CMS"; /// /// CRC 16-bit checksum (CRC-16/DDS-110) /// - CRC16_DDS110, + public const string CRC16_DDS110 = "CRC16_DDS110"; /// /// CRC 16-bit checksum (CRC-16/DECT-R [R-CRC-16]) /// - CRC16_DECTR, + public const string CRC16_DECTR = "CRC16_DECTR"; /// /// CRC 16-bit checksum (CRC-16/DECT-X [X-CRC-16]) /// - CRC16_DECTX, + public const string CRC16_DECTX = "CRC16_DECTX"; /// /// CRC 16-bit checksum (CRC-16/DNP) /// - CRC16_DNP, + public const string CRC16_DNP = "CRC16_DNP"; /// /// CRC 16-bit checksum (CRC-16/EN-13757) /// - CRC16_EN13757, + public const string CRC16_EN13757 = "CRC16_EN13757"; /// /// CRC 16-bit checksum (CRC-16/GENIBUS [CRC-16/DARC, CRC-16/EPC, CRC-16/EPC-C1G2, CRC-16/I-CODE]) /// - CRC16_GENIBUS, + public const string CRC16_GENIBUS = "CRC16_GENIBUS"; /// /// CRC 16-bit checksum (CRC-16/GSM) /// - CRC16_GSM, + public const string CRC16_GSM = "CRC16_GSM"; /// /// CRC 16-bit checksum (CRC-16/IBM-3740 [CRC-16/AUTOSAR, CRC-16/CCITT-FALSE]) /// - CRC16_IBM3740, + public const string CRC16_IBM3740 = "CRC16_IBM3740"; /// /// CRC 16-bit checksum (CRC-16/IBM-SDLC [CRC-16/ISO-HDLC, CRC-16/ISO-IEC-14443-3-B, CRC-16/X-25, CRC-B, X-25]) /// - CRC16_IBMSDLC, + public const string CRC16_IBMSDLC = "CRC16_IBMSDLC"; /// /// CRC 16-bit checksum (CRC-16/ISO-IEC-14443-3-A [CRC-A]) /// - CRC16_ISOIEC144433A, + public const string CRC16_ISOIEC144433A = "CRC16_ISOIEC144433A"; /// /// CRC 16-bit checksum (CRC-16/KERMIT [CRC-16/BLUETOOTH, CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-16/V-41-LSB, CRC-CCITT, KERMIT]) /// - CRC16_KERMIT, + public const string CRC16_KERMIT = "CRC16_KERMIT"; /// /// CRC 16-bit checksum (CRC-16/LJ1200) /// - CRC16_LJ1200, + public const string CRC16_LJ1200 = "CRC16_LJ1200"; /// /// CRC 16-bit checksum (CRC-16/M17) /// - CRC16_M17, + public const string CRC16_M17 = "CRC16_M17"; /// /// CRC 16-bit checksum (CRC-16/MAXIM-DOW [CRC-16/MAXIM]) /// - CRC16_MAXIMDOW, + public const string CRC16_MAXIMDOW = "CRC16_MAXIMDOW"; /// /// CRC 16-bit checksum (CRC-16/MCRF4XX) /// - CRC16_MCRF4XX, + public const string CRC16_MCRF4XX = "CRC16_MCRF4XX"; /// /// CRC 16-bit checksum (CRC-16/MODBUS [MODBUS]) /// - CRC16_MODBUS, + public const string CRC16_MODBUS = "CRC16_MODBUS"; /// /// CRC 16-bit checksum (CRC-16/NRSC-5) /// - CRC16_NRSC5, + public const string CRC16_NRSC5 = "CRC16_NRSC5"; /// /// CRC 16-bit checksum (CRC-16/OPENSAFETY-A) /// - CRC16_OPENSAFETYA, + public const string CRC16_OPENSAFETYA = "CRC16_OPENSAFETYA"; /// /// CRC 16-bit checksum (CRC-16/OPENSAFETY-B) /// - CRC16_OPENSAFETYB, + public const string CRC16_OPENSAFETYB = "CRC16_OPENSAFETYB"; /// /// CRC 16-bit checksum (CRC-16/PROFIBUS [CRC-16/IEC-61158-2]) /// - CRC16_PROFIBUS, + public const string CRC16_PROFIBUS = "CRC16_PROFIBUS"; /// /// CRC 16-bit checksum (CRC-16/RIELLO) /// - CRC16_RIELLO, + public const string CRC16_RIELLO = "CRC16_RIELLO"; /// /// CRC 16-bit checksum (CRC-16/SPI-FUJITSU [CRC-16/AUG-CCITT]) /// - CRC16_SPIFUJITSU, + public const string CRC16_SPIFUJITSU = "CRC16_SPIFUJITSU"; /// /// CRC 16-bit checksum (CRC-16/T10-DIF) /// - CRC16_T10DIF, + public const string CRC16_T10DIF = "CRC16_T10DIF"; /// /// CRC 16-bit checksum (CRC-16/TELEDISK) /// - CRC16_TELEDISK, + public const string CRC16_TELEDISK = "CRC16_TELEDISK"; /// /// CRC 16-bit checksum (CRC-16/TMS37157) /// - CRC16_TMS37157, + public const string CRC16_TMS37157 = "CRC16_TMS37157"; /// /// CRC 16-bit checksum (CRC-16/UMTS [CRC-16/BUYPASS, CRC-16/VERIFONE]) /// - CRC16_UMTS, + public const string CRC16_UMTS = "CRC16_UMTS"; /// /// CRC 16-bit checksum (CRC-16/USB) /// - CRC16_USB, + public const string CRC16_USB = "CRC16_USB"; /// /// CRC 16-bit checksum (CRC-16/XMODEM [CRC-16/ACORN, CRC-16/LTE, CRC-16/V-41-MSB, XMODEM, ZMODEM]) /// - CRC16_XMODEM, + public const string CRC16_XMODEM = "CRC16_XMODEM"; #endregion @@ -504,7 +502,7 @@ namespace SabreTools.Hashing /// /// CRC 17-bit checksum (CRC-17/CAN-FD) /// - CRC17_CANFD, + public const string CRC17_CANFD = "CRC17_CANFD"; #endregion @@ -513,7 +511,7 @@ namespace SabreTools.Hashing /// /// CRC 21-bit checksum (CRC-21/CAN-FD) /// - CRC21_CANFD, + public const string CRC21_CANFD = "CRC21_CANFD"; #endregion @@ -522,42 +520,42 @@ namespace SabreTools.Hashing /// /// CRC 24-bit checksum (CRC-24/BLE) /// - CRC24_BLE, + public const string CRC24_BLE = "CRC24_BLE"; /// /// CRC 24-bit checksum (CRC-24/FLEXRAY-A) /// - CRC24_FLEXRAYA, + public const string CRC24_FLEXRAYA = "CRC24_FLEXRAYA"; /// /// CRC 24-bit checksum (CRC-24/FLEXRAY-B) /// - CRC24_FLEXRAYB, + public const string CRC24_FLEXRAYB = "CRC24_FLEXRAYB"; /// /// CRC 24-bit checksum (CRC-24/INTERLAKEN) /// - CRC24_INTERLAKEN, + public const string CRC24_INTERLAKEN = "CRC24_INTERLAKEN"; /// /// CRC 24-bit checksum (CRC-24/LTE-A) /// - CRC24_LTEA, + public const string CRC24_LTEA = "CRC24_LTEA"; /// /// CRC 24-bit checksum (CRC-24/LTE-B) /// - CRC24_LTEB, + public const string CRC24_LTEB = "CRC24_LTEB"; /// /// CRC 24-bit checksum (CRC-24/OPENPGP) /// - CRC24_OPENPGP, + public const string CRC24_OPENPGP = "CRC24_OPENPGP"; /// /// CRC 24-bit checksum (CRC-24/OS-9) /// - CRC24_OS9, + public const string CRC24_OS9 = "CRC24_OS9"; #endregion @@ -566,7 +564,7 @@ namespace SabreTools.Hashing /// /// CRC 30-bit checksum (CRC-30/CDMA) /// - CRC30_CDMA, + public const string CRC30_CDMA = "CRC30_CDMA"; #endregion @@ -575,7 +573,7 @@ namespace SabreTools.Hashing /// /// CRC 31-bit checksum (CRC-31/PHILIPS) /// - CRC31_PHILIPS, + public const string CRC31_PHILIPS = "CRC31_PHILIPS"; #endregion @@ -585,72 +583,72 @@ namespace SabreTools.Hashing /// CRC 32-bit checksum /// /// Identical to - CRC32, + public const string CRC32 = "CRC32"; /// /// CRC 32-bit checksum (CRC-32/AIXM) /// - CRC32_AIXM, + public const string CRC32_AIXM = "CRC32_AIXM"; /// /// CRC 32-bit checksum (CRC-32/AUTOSAR) /// - CRC32_AUTOSAR, + public const string CRC32_AUTOSAR = "CRC32_AUTOSAR"; /// /// CRC 32-bit checksum (CRC-32/BASE91-D) /// - CRC32_BASE91D, + public const string CRC32_BASE91D = "CRC32_BASE91D"; /// /// CRC 32-bit checksum (BZIP2) /// - CRC32_BZIP2, + public const string CRC32_BZIP2 = "CRC32_BZIP2"; /// /// CRC 32-bit checksum (CRC-32/CD-ROM-EDC) /// - CRC32_CDROMEDC, + public const string CRC32_CDROMEDC = "CRC32_CDROMEDC"; /// /// CRC 32-bit checksum (CRC-32/CKSUM) /// - CRC32_CKSUM, + public const string CRC32_CKSUM = "CRC32_CKSUM"; /// /// CRC 32-bit checksum (CRC-32/DVD-ROM-EDC) /// - CRC32_DVDROMEDC, + public const string CRC32_DVDROMEDC = "CRC32_DVDROMEDC"; /// /// CRC 32-bit checksum (CRC-32/ISCSI) /// - CRC32_ISCSI, + public const string CRC32_ISCSI = "CRC32_ISCSI"; /// /// CRC 32-bit checksum (CRC-32/ISO-HDLC) /// - CRC32_ISOHDLC, + public const string CRC32_ISOHDLC = "CRC32_ISOHDLC"; /// /// CRC 32-bit checksum (CRC-32/JAMCRC) /// - CRC32_JAMCRC, + public const string CRC32_JAMCRC = "CRC32_JAMCRC"; /// /// CRC 32-bit checksum (CRC-32/MEF) /// - CRC32_MEF, + public const string CRC32_MEF = "CRC32_MEF"; /// /// CRC 32-bit checksum (CRC-32/MPEG-2) /// - CRC32_MPEG2, + public const string CRC32_MPEG2 = "CRC32_MPEG2"; /// /// CRC 32-bit checksum (CRC-32/XFER) /// - CRC32_XFER, + public const string CRC32_XFER = "CRC32_XFER"; #endregion @@ -659,7 +657,7 @@ namespace SabreTools.Hashing /// /// CRC 40-bit checksum (CRC-40/GSM) /// - CRC40_GSM, + public const string CRC40_GSM = "CRC40_GSM"; #endregion @@ -669,42 +667,42 @@ namespace SabreTools.Hashing /// CRC 64-bit checksum /// /// Identical to - CRC64, + public const string CRC64 = "CRC64"; /// /// CRC 64-bit checksum (CRC-64/ECMA-182, Microsoft implementation) /// - CRC64_ECMA182, + public const string CRC64_ECMA182 = "CRC64_ECMA182"; /// /// CRC 64-bit checksum (CRC-64/GO-ISO) /// - CRC64_GOISO, + public const string CRC64_GOISO = "CRC64_GOISO"; /// /// CRC 64-bit checksum (CRC-64/MS) /// - CRC64_MS, + public const string CRC64_MS = "CRC64_MS"; /// /// CRC 64-bit checksum (CRC-64/NVME) /// - CRC64_NVME, + public const string CRC64_NVME = "CRC64_NVME"; /// /// CRC 64-bit checksum (CRC-64/REDIS) /// - CRC64_REDIS, + public const string CRC64_REDIS = "CRC64_REDIS"; /// /// CRC 64-bit checksum (CRC-64/WE) /// - CRC64_WE, + public const string CRC64_WE = "CRC64_WE"; /// /// CRC 64-bit checksum (CRC-64/XZ) /// - CRC64_XZ, + public const string CRC64_XZ = "CRC64_XZ"; #endregion @@ -715,17 +713,17 @@ namespace SabreTools.Hashing /// /// John G. Fletcher's 16-bit checksum /// - Fletcher16, + public const string Fletcher16 = "Fletcher16"; /// /// John G. Fletcher's 32-bit checksum /// - Fletcher32, + public const string Fletcher32 = "Fletcher32"; /// /// John G. Fletcher's 64-bit checksum /// - Fletcher64, + public const string Fletcher64 = "Fletcher64"; #endregion @@ -734,56 +732,56 @@ namespace SabreTools.Hashing /// /// FNV hash (Variant 0, 32-bit) /// - FNV0_32, + public const string FNV0_32 = "FNV0_32"; /// /// FNV hash (Variant 0, 64-bit) /// - FNV0_64, + public const string FNV0_64 = "FNV0_64"; /// /// FNV hash (Variant 1, 32-bit) /// - FNV1_32, + public const string FNV1_32 = "FNV1_32"; /// /// FNV hash (Variant 1, 64-bit) /// - FNV1_64, + public const string FNV1_64 = "FNV1_64"; /// /// FNV hash (Variant 1a, 32-bit) /// - FNV1a_32, + public const string FNV1a_32 = "FNV1a_32"; /// /// FNV hash (Variant 1a, 64-bit) /// - FNV1a_64, + public const string FNV1a_64 = "FNV1a_64"; #endregion /// /// Custom checksum used by MEKA /// - MekaCrc, + public const string MekaCrc = "MekaCrc"; #region Message Digest /// /// MD2 message-digest algorithm /// - MD2, + public const string MD2 = "MD2"; /// /// MD4 message-digest algorithm /// - MD4, + public const string MD4 = "MD4"; /// /// MD5 message-digest algorithm /// - MD5, + public const string MD5 = "MD5"; #endregion @@ -792,22 +790,22 @@ namespace SabreTools.Hashing /// /// RIPEMD-128 hash /// - RIPEMD128, + public const string RIPEMD128 = "RIPEMD128"; /// /// RIPEMD-160 hash /// - RIPEMD160, + public const string RIPEMD160 = "RIPEMD160"; /// /// RIPEMD-256 hash /// - RIPEMD256, + public const string RIPEMD256 = "RIPEMD256"; /// /// RIPEMD-320 hash /// - RIPEMD320, + public const string RIPEMD320 = "RIPEMD320"; #endregion @@ -816,50 +814,50 @@ namespace SabreTools.Hashing /// /// SHA-1 hash /// - SHA1, + public const string SHA1 = "SHA1"; /// /// SHA-256 hash /// - SHA256, + public const string SHA256 = "SHA256"; /// /// SHA-384 hash /// - SHA384, + public const string SHA384 = "SHA384"; /// /// SHA-512 hash /// - SHA512, + public const string SHA512 = "SHA512"; #if NET8_0_OR_GREATER /// /// SHA3-256 hash /// - SHA3_256, + public const string SHA3_256 = "SHA3_256"; /// /// SHA3-384 hash /// - SHA3_384, + public const string SHA3_384 = "SHA3_384"; /// /// SHA3-512 hash /// - SHA3_512, + public const string SHA3_512 = "SHA3_512"; /// /// SHAKE128 SHA-3 family hash /// /// Outputs a 256-bit (32-byte) hash - SHAKE128, + public const string SHAKE128 = "SHAKE128"; /// /// SHAKE256 SHA-3 family hash /// /// Outputs a 512-bit (64-byte) hash - SHAKE256, + public const string SHAKE256 = "SHAKE256"; #endif #endregion @@ -867,69 +865,69 @@ namespace SabreTools.Hashing /// /// spamsum fuzzy hash /// - SpamSum, + public const string SpamSum = "SpamSum"; #region Tiger /// /// Tiger 128-bit hash, 3 passes /// - Tiger128_3, + public const string Tiger128_3 = "Tiger128_3"; /// /// Tiger 128-bit hash, 4 passes /// - Tiger128_4, + public const string Tiger128_4 = "Tiger128_4"; /// /// Tiger 160-bit hash, 3 passes /// - Tiger160_3, + public const string Tiger160_3 = "Tiger160_3"; /// /// Tiger 160-bit hash, 4 passes /// - Tiger160_4, + public const string Tiger160_4 = "Tiger160_4"; /// /// Tiger 192-bit hash, 3 passes /// - Tiger192_3, + public const string Tiger192_3 = "Tiger192_3"; /// /// Tiger 192-bit hash, 4 passes /// - Tiger192_4, + public const string Tiger192_4 = "Tiger192_4"; /// /// Tiger2 128-bit hash, 3 passes /// - Tiger2_128_3, + public const string Tiger2_128_3 = "Tiger2_128_3"; /// /// Tiger2 128-bit hash, 4 passes /// - Tiger2_128_4, + public const string Tiger2_128_4 = "Tiger2_128_4"; /// /// Tiger2 160-bit hash, 3 passes /// - Tiger2_160_3, + public const string Tiger2_160_3 = "Tiger2_160_3"; /// /// Tiger2 160-bit hash, 4 passes /// - Tiger2_160_4, + public const string Tiger2_160_4 = "Tiger2_160_4"; /// /// Tiger2 192-bit hash, 3 passes /// - Tiger2_192_3, + public const string Tiger2_192_3 = "Tiger2_192_3"; /// /// Tiger2 192-bit hash, 4 passes /// - Tiger2_192_4, + public const string Tiger2_192_4 = "Tiger2_192_4"; #endregion @@ -938,25 +936,236 @@ namespace SabreTools.Hashing /// /// xxHash32 hash /// - XxHash32, + public const string XxHash32 = "XxHash32"; /// /// xxHash64 hash /// - XxHash64, + public const string XxHash64 = "XxHash64"; #if NET462_OR_GREATER || NETCOREAPP /// /// XXH3 64-bit hash /// - XxHash3, + public const string XxHash3 = "XxHash3"; /// /// XXH128 128-bit hash /// - XxHash128, + public const string XxHash128 = "XxHash128"; #endif #endregion + + /// + /// All supported hashes + /// + /// TODO: This shouldn't be hardcoded + public static readonly string[] AllHashes = + [ + Adler32, + + #if NET7_0_OR_GREATER + BLAKE3, + #endif + + CRC1_ZERO, + CRC1_ONE, + + CRC3_GSM, + CRC3_ROHC, + + CRC4_G704, + CRC4_INTERLAKEN, + + CRC5_EPCC1G2, + CRC5_G704, + CRC5_USB, + + CRC6_CDMA2000A, + CRC6_CDMA2000B, + CRC6_DARC, + CRC6_G704, + CRC6_GSM, + + CRC7_MMC, + CRC7_ROHC, + CRC7_UMTS, + + CRC8, + CRC8_AUTOSAR, + CRC8_BLUETOOTH, + CRC8_CDMA2000, + CRC8_DARC, + CRC8_DVBS2, + CRC8_GSMA, + CRC8_GSMB, + CRC8_HITAG, + CRC8_I4321, + CRC8_ICODE, + CRC8_LTE, + CRC8_MAXIMDOW, + CRC8_MIFAREMAD, + CRC8_NRSC5, + CRC8_OPENSAFETY, + CRC8_ROHC, + CRC8_SAEJ1850, + CRC8_SMBUS, + CRC8_TECH3250, + CRC8_WCDMA, + + CRC10_ATM, + CRC10_CDMA2000, + CRC10_GSM, + + CRC11_FLEXRAY, + CRC11_UMTS, + + CRC12_CDMA2000, + CRC12_DECT, + CRC12_GSM, + CRC12_UMTS, + + CRC13_BBC, + + CRC14_DARC, + CRC14_GSM, + + CRC15_CAN, + CRC15_MPT1327, + + CRC16, + CRC16_ARC, + CRC16_CDMA2000, + CRC16_CMS, + CRC16_DDS110, + CRC16_DECTR, + CRC16_DECTX, + CRC16_DNP, + CRC16_EN13757, + CRC16_GENIBUS, + CRC16_GSM, + CRC16_IBM3740, + CRC16_IBMSDLC, + CRC16_ISOIEC144433A, + CRC16_KERMIT, + CRC16_LJ1200, + CRC16_M17, + CRC16_MAXIMDOW, + CRC16_MCRF4XX, + CRC16_MODBUS, + CRC16_NRSC5, + CRC16_OPENSAFETYA, + CRC16_OPENSAFETYB, + CRC16_PROFIBUS, + CRC16_RIELLO, + CRC16_SPIFUJITSU, + CRC16_T10DIF, + CRC16_TELEDISK, + CRC16_TMS37157, + CRC16_UMTS, + CRC16_USB, + CRC16_XMODEM, + + CRC17_CANFD, + + CRC21_CANFD, + + CRC24_BLE, + CRC24_FLEXRAYA, + CRC24_FLEXRAYB, + CRC24_INTERLAKEN, + CRC24_LTEA, + CRC24_LTEB, + CRC24_OPENPGP, + CRC24_OS9, + + CRC30_CDMA, + + CRC31_PHILIPS, + + CRC32, + CRC32_AIXM, + CRC32_AUTOSAR, + CRC32_BASE91D, + CRC32_BZIP2, + CRC32_CDROMEDC, + CRC32_CKSUM, + CRC32_DVDROMEDC, + CRC32_ISCSI, + CRC32_ISOHDLC, + CRC32_JAMCRC, + CRC32_MEF, + CRC32_MPEG2, + CRC32_XFER, + + CRC40_GSM, + + CRC64, + CRC64_ECMA182, + CRC64_GOISO, + CRC64_MS, + CRC64_NVME, + CRC64_REDIS, + CRC64_WE, + CRC64_XZ, + + Fletcher16, + Fletcher32, + Fletcher64, + + FNV0_32, + FNV0_64, + FNV1_32, + FNV1_64, + FNV1a_32, + FNV1a_64, + + MekaCrc, + + MD2, + MD4, + MD5, + + RIPEMD128, + RIPEMD160, + RIPEMD256, + RIPEMD320, + + SHA1, + SHA256, + SHA384, + SHA512, +#if NET8_0_OR_GREATER + SHA3_256, + SHA3_384, + SHA3_512, + SHAKE128, + SHAKE256, +#endif + + SpamSum, + + Tiger128_3, + Tiger128_4, + Tiger160_3, + Tiger160_4, + Tiger192_3, + Tiger192_4, + + Tiger2_128_3, + Tiger2_128_4, + Tiger2_160_3, + Tiger2_160_4, + Tiger2_192_3, + Tiger2_192_4, + + XxHash32, + XxHash64, +#if NET462_OR_GREATER || NETCOREAPP + XxHash3, + XxHash128, +#endif + ]; } } diff --git a/SabreTools.Hashing/HashWrapper.cs b/SabreTools.Hashing/HashWrapper.cs index cec9988..cc6c5ed 100644 --- a/SabreTools.Hashing/HashWrapper.cs +++ b/SabreTools.Hashing/HashWrapper.cs @@ -17,7 +17,7 @@ namespace SabreTools.Hashing /// /// Hash type associated with the current state /// - public readonly HashType HashType; + public readonly string HashName; /// /// Current hash in bytes @@ -67,9 +67,9 @@ namespace SabreTools.Hashing /// Constructor /// /// Hash type to instantiate - public HashWrapper(HashType hashType) + public HashWrapper(string hashType) { - HashType = hashType; + HashName = hashType.GetHashType() ?? string.Empty; GetHasher(); } @@ -78,7 +78,7 @@ namespace SabreTools.Hashing /// private void GetHasher() { - _hasher = HashType switch + _hasher = HashName switch { HashType.Adler32 => new Adler32(), diff --git a/SabreTools.Hashing/ZeroHash.cs b/SabreTools.Hashing/ZeroHash.cs index 276935d..8bf2e80 100644 --- a/SabreTools.Hashing/ZeroHash.cs +++ b/SabreTools.Hashing/ZeroHash.cs @@ -88,7 +88,7 @@ namespace SabreTools.Hashing /// /// Set of all known 0-byte outputs as strings /// - private static readonly Dictionary _bytes = new() + private static readonly Dictionary _bytes = new() { {HashType.Adler32, [0x00, 0x00, 0x00, 0x01]}, @@ -394,7 +394,7 @@ namespace SabreTools.Hashing /// /// Set of all known 0-byte outputs as strings /// - private static readonly Dictionary _strings = new() + private static readonly Dictionary _strings = new() { {HashType.Adler32, "00000001"}, @@ -605,12 +605,12 @@ namespace SabreTools.Hashing /// /// Hash type to get the value for /// Non-empty array containing the value on success, empty array on failure - 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; } /// @@ -618,12 +618,12 @@ namespace SabreTools.Hashing /// /// Hash type to get the value for /// Non-empty string containing the value on success, empty string on failure - 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; } } }