From f2499d9ae1fd371d51049f67f71362683473b05a Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sun, 22 Mar 2026 02:29:17 -0400 Subject: [PATCH] Unify read names with IO --- SabreTools.Hashing/CryptographicHash/MD4.cs | 4 +- .../CryptographicHash/RipeMD128.cs | 4 +- .../CryptographicHash/RipeMD160.cs | 4 +- .../CryptographicHash/RipeMD256.cs | 4 +- .../CryptographicHash/RipeMD320.cs | 4 +- .../CryptographicHash/TigerHashBase.cs | 4 +- SabreTools.Hashing/HashOperations.cs | 106 +++++++----------- .../NonCryptographicHash/XxHash32State.cs | 18 +-- .../NonCryptographicHash/XxHash64State.cs | 20 ++-- SabreTools.Hashing/XxHash/Utility.cs | 26 ++--- 10 files changed, 83 insertions(+), 111 deletions(-) diff --git a/SabreTools.Hashing/CryptographicHash/MD4.cs b/SabreTools.Hashing/CryptographicHash/MD4.cs index 8c6fead..f170474 100644 --- a/SabreTools.Hashing/CryptographicHash/MD4.cs +++ b/SabreTools.Hashing/CryptographicHash/MD4.cs @@ -50,7 +50,7 @@ namespace SabreTools.Hashing.CryptographicHash // Split the buffer for the round for (int i = 0; i < 16; i++) { - _block[i] = ReadLE32(_buffer, i * 4); + _block[i] = ToUInt32LittleEndian(_buffer, i * 4); } // Run the round @@ -71,7 +71,7 @@ namespace SabreTools.Hashing.CryptographicHash // Split the buffer for the round for (int i = 0; i < 16; i++) { - _block[i] = ReadLE32(_buffer, i * 4); + _block[i] = ToUInt32LittleEndian(_buffer, i * 4); } // Run the round diff --git a/SabreTools.Hashing/CryptographicHash/RipeMD128.cs b/SabreTools.Hashing/CryptographicHash/RipeMD128.cs index 4bd8e75..1e517b8 100644 --- a/SabreTools.Hashing/CryptographicHash/RipeMD128.cs +++ b/SabreTools.Hashing/CryptographicHash/RipeMD128.cs @@ -51,7 +51,7 @@ namespace SabreTools.Hashing.CryptographicHash // Split the buffer for the round for (int i = 0; i < 16; i++) { - _block[i] = ReadLE32(_buffer, i * 4); + _block[i] = ToUInt32LittleEndian(_buffer, i * 4); } // Run the round @@ -72,7 +72,7 @@ namespace SabreTools.Hashing.CryptographicHash // Split the buffer for the round for (int i = 0; i < 16; i++) { - _block[i] = ReadLE32(_buffer, i * 4); + _block[i] = ToUInt32LittleEndian(_buffer, i * 4); } // Run the round diff --git a/SabreTools.Hashing/CryptographicHash/RipeMD160.cs b/SabreTools.Hashing/CryptographicHash/RipeMD160.cs index cc4f5d2..833e920 100644 --- a/SabreTools.Hashing/CryptographicHash/RipeMD160.cs +++ b/SabreTools.Hashing/CryptographicHash/RipeMD160.cs @@ -52,7 +52,7 @@ namespace SabreTools.Hashing.CryptographicHash // Split the buffer for the round for (int i = 0; i < 16; i++) { - _block[i] = ReadLE32(_buffer, i * 4); + _block[i] = ToUInt32LittleEndian(_buffer, i * 4); } // Run the round @@ -73,7 +73,7 @@ namespace SabreTools.Hashing.CryptographicHash // Split the buffer for the round for (int i = 0; i < 16; i++) { - _block[i] = ReadLE32(_buffer, i * 4); + _block[i] = ToUInt32LittleEndian(_buffer, i * 4); } // Run the round diff --git a/SabreTools.Hashing/CryptographicHash/RipeMD256.cs b/SabreTools.Hashing/CryptographicHash/RipeMD256.cs index 2b617d8..c785945 100644 --- a/SabreTools.Hashing/CryptographicHash/RipeMD256.cs +++ b/SabreTools.Hashing/CryptographicHash/RipeMD256.cs @@ -55,7 +55,7 @@ namespace SabreTools.Hashing.CryptographicHash // Split the buffer for the round for (int i = 0; i < 16; i++) { - _block[i] = ReadLE32(_buffer, i * 4); + _block[i] = ToUInt32LittleEndian(_buffer, i * 4); } // Run the round @@ -76,7 +76,7 @@ namespace SabreTools.Hashing.CryptographicHash // Split the buffer for the round for (int i = 0; i < 16; i++) { - _block[i] = ReadLE32(_buffer, i * 4); + _block[i] = ToUInt32LittleEndian(_buffer, i * 4); } // Run the round diff --git a/SabreTools.Hashing/CryptographicHash/RipeMD320.cs b/SabreTools.Hashing/CryptographicHash/RipeMD320.cs index 8a9df44..533de73 100644 --- a/SabreTools.Hashing/CryptographicHash/RipeMD320.cs +++ b/SabreTools.Hashing/CryptographicHash/RipeMD320.cs @@ -57,7 +57,7 @@ namespace SabreTools.Hashing.CryptographicHash // Split the buffer for the round for (int i = 0; i < 16; i++) { - _block[i] = ReadLE32(_buffer, i * 4); + _block[i] = ToUInt32LittleEndian(_buffer, i * 4); } // Run the round @@ -78,7 +78,7 @@ namespace SabreTools.Hashing.CryptographicHash // Split the buffer for the round for (int i = 0; i < 16; i++) { - _block[i] = ReadLE32(_buffer, i * 4); + _block[i] = ToUInt32LittleEndian(_buffer, i * 4); } // Run the round diff --git a/SabreTools.Hashing/CryptographicHash/TigerHashBase.cs b/SabreTools.Hashing/CryptographicHash/TigerHashBase.cs index 8fda180..06db021 100644 --- a/SabreTools.Hashing/CryptographicHash/TigerHashBase.cs +++ b/SabreTools.Hashing/CryptographicHash/TigerHashBase.cs @@ -56,7 +56,7 @@ namespace SabreTools.Hashing.CryptographicHash // Split the buffer for the round for (int i = 0; i < 8; i++) { - _block[i] = ReadLE64(_buffer, i * 8); + _block[i] = ToUInt64LittleEndian(_buffer, i * 8); } // Run the round @@ -77,7 +77,7 @@ namespace SabreTools.Hashing.CryptographicHash // Split the buffer for the round for (int i = 0; i < 8; i++) { - _block[i] = ReadLE64(_buffer, i * 8); + _block[i] = ToUInt64LittleEndian(_buffer, i * 8); } // Run the round diff --git a/SabreTools.Hashing/HashOperations.cs b/SabreTools.Hashing/HashOperations.cs index fa499f5..ad69607 100644 --- a/SabreTools.Hashing/HashOperations.cs +++ b/SabreTools.Hashing/HashOperations.cs @@ -29,27 +29,6 @@ namespace SabreTools.Hashing } } - /// - /// Convert a byte array to a UInt64 - /// - /// Byte array to convert - /// UInt64 representing the byte array - /// https://stackoverflow.com/questions/66750224/how-to-convert-a-byte-array-of-any-size-to-ulong-in-c - public static ulong BytesToUInt64(byte[]? bytes) - { - // If we get null in, we send 0 out - if (bytes is null) - return default; - - ulong result = 0; - for (int i = 0; i < bytes.Length; i++) - { - result |= (ulong)bytes[i] << (i * 8); - } - - return result; - } - #if NET7_0_OR_GREATER /// /// Convert a byte array to a UInt64 @@ -69,68 +48,61 @@ namespace SabreTools.Hashing result |= (UInt128)bytes[i] << (i * 8); } + return result; + } +#else + /// + /// Convert a byte array to a UInt64 + /// + /// Byte array to convert + /// UInt64 representing the byte array + /// https://stackoverflow.com/questions/66750224/how-to-convert-a-byte-array-of-any-size-to-ulong-in-c + public static ulong BytesToUInt64(byte[]? bytes) + { + // If we get null in, we send 0 out + if (bytes is null) + return default; + + ulong result = 0; + for (int i = 0; i < bytes.Length; i++) + { + result |= (ulong)bytes[i] << (i * 8); + } + return result; } #endif #endregion - #region Read Big-Endian - - /// - /// 32-bit big-endian read - /// - public static uint ReadBE32(byte[] data, int offset) - { - return (uint)(data[offset + 3] - | (data[offset + 2] << 8) - | (data[offset + 1] << 16) - | (data[offset + 0] << 24)); - } - - /// - /// 64-bit big-endian read - /// - public static ulong ReadBE64(byte[] data, int offset) - { - return data[offset + 7] - | ((ulong)data[offset + 6] << 8) - | ((ulong)data[offset + 5] << 16) - | ((ulong)data[offset + 4] << 24) - | ((ulong)data[offset + 3] << 32) - | ((ulong)data[offset + 2] << 40) - | ((ulong)data[offset + 1] << 48) - | ((ulong)data[offset + 0] << 56); - } - - #endregion - #region Read Litte-Endian /// - /// 32-bit little-endian read + /// Convert a byte array at an offset to a UInt32 /// - public static uint ReadLE32(byte[] data, int offset) + /// Reads in little-endian format + public static uint ToUInt32LittleEndian(byte[] value, int offset) { - return (uint)(data[offset + 0] - | (data[offset + 1] << 8) - | (data[offset + 2] << 16) - | (data[offset + 3] << 24)); + return (uint)(value[offset + 0] + | (value[offset + 1] << 8) + | (value[offset + 2] << 16) + | (value[offset + 3] << 24)); } /// - /// 64-bit little-endian read + /// Convert a byte array at an offset to a UInt64 /// - public static ulong ReadLE64(byte[] data, int offset) + /// Reads in little-endian format + public static ulong ToUInt64LittleEndian(byte[] value, int offset) { - return data[offset + 0] - | ((ulong)data[offset + 1] << 8) - | ((ulong)data[offset + 2] << 16) - | ((ulong)data[offset + 3] << 24) - | ((ulong)data[offset + 4] << 32) - | ((ulong)data[offset + 5] << 40) - | ((ulong)data[offset + 6] << 48) - | ((ulong)data[offset + 7] << 56); + return value[offset + 0] + | ((ulong)value[offset + 1] << 8) + | ((ulong)value[offset + 2] << 16) + | ((ulong)value[offset + 3] << 24) + | ((ulong)value[offset + 4] << 32) + | ((ulong)value[offset + 5] << 40) + | ((ulong)value[offset + 6] << 48) + | ((ulong)value[offset + 7] << 56); } #endregion diff --git a/SabreTools.Hashing/NonCryptographicHash/XxHash32State.cs b/SabreTools.Hashing/NonCryptographicHash/XxHash32State.cs index 8790770..324191d 100644 --- a/SabreTools.Hashing/NonCryptographicHash/XxHash32State.cs +++ b/SabreTools.Hashing/NonCryptographicHash/XxHash32State.cs @@ -83,10 +83,10 @@ namespace SabreTools.Hashing.NonCryptographicHash Array.Copy(data, offset, _mem32, _memsize, 16 - _memsize); int p32 = 0; - _acc[0] = Round(_acc[0], ReadLE32(_mem32, p32)); p32 += 4; - _acc[1] = Round(_acc[1], ReadLE32(_mem32, p32)); p32 += 4; - _acc[2] = Round(_acc[2], ReadLE32(_mem32, p32)); p32 += 4; - _acc[3] = Round(_acc[3], ReadLE32(_mem32, p32)); + _acc[0] = Round(_acc[0], ToUInt32LittleEndian(_mem32, p32)); p32 += 4; + _acc[1] = Round(_acc[1], ToUInt32LittleEndian(_mem32, p32)); p32 += 4; + _acc[2] = Round(_acc[2], ToUInt32LittleEndian(_mem32, p32)); p32 += 4; + _acc[3] = Round(_acc[3], ToUInt32LittleEndian(_mem32, p32)); offset += 16 - _memsize; _memsize = 0; @@ -97,10 +97,10 @@ namespace SabreTools.Hashing.NonCryptographicHash int limit = bEnd - 16; do { - _acc[0] = Round(_acc[0], ReadLE32(data, offset)); offset += 4; - _acc[1] = Round(_acc[1], ReadLE32(data, offset)); offset += 4; - _acc[2] = Round(_acc[2], ReadLE32(data, offset)); offset += 4; - _acc[3] = Round(_acc[3], ReadLE32(data, offset)); offset += 4; + _acc[0] = Round(_acc[0], ToUInt32LittleEndian(data, offset)); offset += 4; + _acc[1] = Round(_acc[1], ToUInt32LittleEndian(data, offset)); offset += 4; + _acc[2] = Round(_acc[2], ToUInt32LittleEndian(data, offset)); offset += 4; + _acc[3] = Round(_acc[3], ToUInt32LittleEndian(data, offset)); offset += 4; } while (offset <= limit); } @@ -185,7 +185,7 @@ namespace SabreTools.Hashing.NonCryptographicHash length &= 15; while (length >= 4) { - hash += ReadLE32(data, offset) * XXH_PRIME32_3; + hash += ToUInt32LittleEndian(data, offset) * XXH_PRIME32_3; offset += 4; hash = RotateLeft32(hash, 17) * XXH_PRIME32_4; length -= 4; diff --git a/SabreTools.Hashing/NonCryptographicHash/XxHash64State.cs b/SabreTools.Hashing/NonCryptographicHash/XxHash64State.cs index d94acda..e27e848 100644 --- a/SabreTools.Hashing/NonCryptographicHash/XxHash64State.cs +++ b/SabreTools.Hashing/NonCryptographicHash/XxHash64State.cs @@ -76,10 +76,10 @@ namespace SabreTools.Hashing.NonCryptographicHash Array.Copy(data, offset, _mem64, _memsize, 32 - _memsize); int p64 = 0; - _acc[0] = Round(_acc[0], ReadLE64(_mem64, p64)); p64 += 8; - _acc[1] = Round(_acc[1], ReadLE64(_mem64, p64)); p64 += 8; - _acc[2] = Round(_acc[2], ReadLE64(_mem64, p64)); p64 += 8; - _acc[3] = Round(_acc[3], ReadLE64(_mem64, p64)); + _acc[0] = Round(_acc[0], ToUInt64LittleEndian(_mem64, p64)); p64 += 8; + _acc[1] = Round(_acc[1], ToUInt64LittleEndian(_mem64, p64)); p64 += 8; + _acc[2] = Round(_acc[2], ToUInt64LittleEndian(_mem64, p64)); p64 += 8; + _acc[3] = Round(_acc[3], ToUInt64LittleEndian(_mem64, p64)); offset += 32 - _memsize; _memsize = 0; @@ -90,10 +90,10 @@ namespace SabreTools.Hashing.NonCryptographicHash int limit = bEnd - 32; do { - _acc[0] = Round(_acc[0], ReadLE64(data, offset)); offset += 8; - _acc[1] = Round(_acc[1], ReadLE64(data, offset)); offset += 8; - _acc[2] = Round(_acc[2], ReadLE64(data, offset)); offset += 8; - _acc[3] = Round(_acc[3], ReadLE64(data, offset)); offset += 8; + _acc[0] = Round(_acc[0], ToUInt64LittleEndian(data, offset)); offset += 8; + _acc[1] = Round(_acc[1], ToUInt64LittleEndian(data, offset)); offset += 8; + _acc[2] = Round(_acc[2], ToUInt64LittleEndian(data, offset)); offset += 8; + _acc[3] = Round(_acc[3], ToUInt64LittleEndian(data, offset)); offset += 8; } while (offset <= limit); } @@ -175,7 +175,7 @@ namespace SabreTools.Hashing.NonCryptographicHash length &= 31; while (length >= 8) { - ulong k1 = Round(0, ReadLE64(data, offset)); + ulong k1 = Round(0, ToUInt64LittleEndian(data, offset)); offset += 8; hash ^= k1; hash = (RotateLeft64(hash, 27) * XXH_PRIME64_1) + XXH_PRIME64_4; @@ -184,7 +184,7 @@ namespace SabreTools.Hashing.NonCryptographicHash if (length >= 4) { - hash ^= ReadLE32(data, offset) * XXH_PRIME64_1; + hash ^= ToUInt32LittleEndian(data, offset) * XXH_PRIME64_1; offset += 4; hash = (RotateLeft64(hash, 23) * XXH_PRIME64_2) + XXH_PRIME64_3; length -= 4; diff --git a/SabreTools.Hashing/XxHash/Utility.cs b/SabreTools.Hashing/XxHash/Utility.cs index 4c0574a..ad6f4de 100644 --- a/SabreTools.Hashing/XxHash/Utility.cs +++ b/SabreTools.Hashing/XxHash/Utility.cs @@ -121,7 +121,7 @@ namespace SabreTools.Hashing.XxHash | ((uint)c2 << 24) | ((uint)c3 << 0) | ((uint)length << 8); - ulong bitflip = (ReadLE32(secret, 0) ^ ReadLE32(secret, 4)) + seed; + ulong bitflip = (ToUInt32LittleEndian(secret, 0) ^ ToUInt32LittleEndian(secret, 4)) + seed; ulong keyed = combined ^ bitflip; return XXH64Avalanche(keyed); @@ -134,9 +134,9 @@ namespace SabreTools.Hashing.XxHash { seed ^= (ulong)Swap32((uint)seed) << 32; - uint input1 = ReadLE32(data, offset); - uint input2 = ReadLE32(data, offset + length - 4); - ulong bitflip = (ReadLE64(secret, 8) ^ ReadLE64(secret, 16)) - seed; + uint input1 = ToUInt32LittleEndian(data, offset); + uint input2 = ToUInt32LittleEndian(data, offset + length - 4); + ulong bitflip = (ToUInt64LittleEndian(secret, 8) ^ ToUInt64LittleEndian(secret, 16)) - seed; ulong input64 = input2 + (((ulong)input1) << 32); ulong keyed = input64 ^ bitflip; @@ -148,10 +148,10 @@ namespace SabreTools.Hashing.XxHash /// public static ulong Len9To16Out64(byte[] data, int offset, int length, byte[] secret, ulong seed) { - ulong bitflip1 = (ReadLE64(secret, 24) ^ ReadLE64(secret, 32)) + seed; - ulong bitflip2 = (ReadLE64(secret, 40) ^ ReadLE64(secret, 48)) - seed; - ulong input_lo = ReadLE64(data, offset) ^ bitflip1; - ulong input_hi = ReadLE64(data, offset + length - 8) ^ bitflip2; + ulong bitflip1 = (ToUInt64LittleEndian(secret, 24) ^ ToUInt64LittleEndian(secret, 32)) + seed; + ulong bitflip2 = (ToUInt64LittleEndian(secret, 40) ^ ToUInt64LittleEndian(secret, 48)) - seed; + ulong input_lo = ToUInt64LittleEndian(data, offset) ^ bitflip1; + ulong input_hi = ToUInt64LittleEndian(data, offset + length - 8) ^ bitflip2; ulong acc = (ulong)length + Swap64(input_lo) + input_hi @@ -172,17 +172,17 @@ namespace SabreTools.Hashing.XxHash if (length > 0) return Len1To3Out64(data, offset, length, secret, seed); - return XXH64Avalanche(seed ^ ReadLE64(secret, 56) ^ ReadLE64(secret, 64)); + return XXH64Avalanche(seed ^ ToUInt64LittleEndian(secret, 56) ^ ToUInt64LittleEndian(secret, 64)); } public static ulong Mix16B(byte[] data, int offset, byte[] secret, int secretOffset, ulong seed) { - ulong input_lo = ReadLE64(data, offset + 0); - ulong input_hi = ReadLE64(data, offset + 8); + ulong input_lo = ToUInt64LittleEndian(data, offset + 0); + ulong input_hi = ToUInt64LittleEndian(data, offset + 8); return MultiplyTo128Fold64( - input_lo ^ (ReadLE64(secret, secretOffset + 0) + seed), - input_hi ^ (ReadLE64(secret, secretOffset + 8) - seed) + input_lo ^ (ToUInt64LittleEndian(secret, secretOffset + 0) + seed), + input_hi ^ (ToUInt64LittleEndian(secret, secretOffset + 8) - seed) ); }