diff --git a/SabreTools.Hashing/HashWrapper.cs b/SabreTools.Hashing/HashWrapper.cs index f828992..b92209c 100644 --- a/SabreTools.Hashing/HashWrapper.cs +++ b/SabreTools.Hashing/HashWrapper.cs @@ -45,9 +45,6 @@ namespace SabreTools.Hashing case IChecksum ic: return ic.Final(); - case MessageDigest.MessageDigestBase mdb: - return mdb.GetHash(); - #if NET462_OR_GREATER || NETCOREAPP case XxHash3 xxh3: return xxh3.GetCurrentHash(); @@ -370,10 +367,6 @@ namespace SabreTools.Hashing ic.Update(icBlock); break; - case MessageDigest.MessageDigestBase mdb: - mdb.TransformBlock(buffer, offset, size); - break; - #if NET462_OR_GREATER || NETCOREAPP case NonCryptographicHashAlgorithm ncha: var nchaBufferSpan = new ReadOnlySpan(buffer, offset, size); @@ -413,10 +406,6 @@ namespace SabreTools.Hashing case HashAlgorithm ha: ha.TransformFinalBlock(emptyBuffer, 0, 0); break; - - case MessageDigest.MessageDigestBase mdb: - mdb.Terminate(); - break; } } diff --git a/SabreTools.Hashing/MessageDigest/MD2.cs b/SabreTools.Hashing/MessageDigest/MD2.cs index f308418..8064c53 100644 --- a/SabreTools.Hashing/MessageDigest/MD2.cs +++ b/SabreTools.Hashing/MessageDigest/MD2.cs @@ -41,7 +41,7 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override void TransformBlock(byte[] data, int offset, int length) + protected override void HashCore(byte[] data, int offset, int length) { // Figure out how much buffer is needed int bufferLen = 16 - _byteCount; @@ -116,7 +116,7 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override void Terminate() + protected override byte[] HashFinal() { // Determine the pad length byte padLength = (byte)(16 - _byteCount); @@ -131,18 +131,14 @@ namespace SabreTools.Hashing.MessageDigest #else Array.Fill(padding, padLength); #endif - TransformBlock(padding, 0, padLength); - TransformBlock(_checksum, 0, _checksum.Length); - } - /// - public override byte[] GetHash() - { + // Pad the block + HashCore(padding, 0, padLength); + HashCore(_checksum, 0, _checksum.Length); + + // Get the hash var hash = new byte[16]; Array.Copy(_digest, hash, 16); - - // Reset the state and return - Reset(); return hash; } diff --git a/SabreTools.Hashing/MessageDigest/MD4.cs b/SabreTools.Hashing/MessageDigest/MD4.cs index 1daec70..e2270eb 100644 --- a/SabreTools.Hashing/MessageDigest/MD4.cs +++ b/SabreTools.Hashing/MessageDigest/MD4.cs @@ -26,7 +26,7 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override void TransformBlock(byte[] data, int offset, int length) + protected override void HashCore(byte[] data, int offset, int length) { // Figure out how much buffer is needed int bufferLen = (int)(_totalBytes & 0x3f); @@ -81,7 +81,7 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override void Terminate() + protected override byte[] HashFinal() { // Determine the pad length int padLength = 64 - (int)(_totalBytes & 0x3f); @@ -104,12 +104,9 @@ namespace SabreTools.Hashing.MessageDigest padding[padLength - 8] = (byte)((totalBitCount >> 0) & 0xff); // Pad the block - TransformBlock(padding, 0, padding.Length); - } + HashCore(padding, 0, padding.Length); - /// - public override byte[] GetHash() - { + // Get the hash var hash = new byte[16]; int hashOffset = 0; @@ -121,8 +118,6 @@ namespace SabreTools.Hashing.MessageDigest hashOffset += 4; } - // Reset the state and return - Reset(); return hash; } diff --git a/SabreTools.Hashing/MessageDigest/MessageDigestBase.cs b/SabreTools.Hashing/MessageDigest/MessageDigestBase.cs index a42e1e5..19366f8 100644 --- a/SabreTools.Hashing/MessageDigest/MessageDigestBase.cs +++ b/SabreTools.Hashing/MessageDigest/MessageDigestBase.cs @@ -2,7 +2,7 @@ using System; namespace SabreTools.Hashing.MessageDigest { - public abstract class MessageDigestBase + public abstract class MessageDigestBase : System.Security.Cryptography.HashAlgorithm { /// /// Total number of bytes processed @@ -19,29 +19,8 @@ namespace SabreTools.Hashing.MessageDigest /// protected abstract void ResetImpl(); - /// - /// Hash a block of data and append it to the existing hash - /// - /// Byte array representing the data - /// Offset in the byte array to include - /// Length of the data to hash - public abstract void TransformBlock(byte[] data, int offset, int length); - - /// - /// End the hashing process - /// - /// TODO: Combine this when the padding byte can be set by implementing classes - public abstract void Terminate(); - - /// - /// Get the current value of the hash - /// - /// - /// If has not been run, this value - /// will not be accurate for the processed bytes so far. - /// - /// TODO: Combine this when there's an easier way of passing the state - public abstract byte[] GetHash(); + /// + protected abstract override void HashCore(byte[] array, int ibStart, int cbSize); } public abstract class MessageDigestBase : MessageDigestBase where T : struct @@ -63,13 +42,11 @@ namespace SabreTools.Hashing.MessageDigest else throw new InvalidOperationException(); - Reset(); + Initialize(); } - /// - /// Reset the internal hashing state - /// - public void Reset() + /// + public override void Initialize() { // Reset the seed values ResetImpl(); diff --git a/SabreTools.Hashing/MessageDigest/RipeMD128.cs b/SabreTools.Hashing/MessageDigest/RipeMD128.cs index 7af6f1c..4b33439 100644 --- a/SabreTools.Hashing/MessageDigest/RipeMD128.cs +++ b/SabreTools.Hashing/MessageDigest/RipeMD128.cs @@ -27,7 +27,7 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override void TransformBlock(byte[] data, int offset, int length) + protected override void HashCore(byte[] data, int offset, int length) { // Figure out how much buffer is needed int bufferLen = (int)(_totalBytes & 0x3f); @@ -82,7 +82,7 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override void Terminate() + protected override byte[] HashFinal() { // Determine the pad length int padLength = 64 - (int)(_totalBytes & 0x3f); @@ -105,12 +105,9 @@ namespace SabreTools.Hashing.MessageDigest padding[padLength - 8] = (byte)((totalBitCount >> 0) & 0xff); // Pad the block - TransformBlock(padding, 0, padding.Length); - } + HashCore(padding, 0, padding.Length); - /// - public override byte[] GetHash() - { + // Get the hash var hash = new byte[16]; int hashOffset = 0; @@ -122,8 +119,6 @@ namespace SabreTools.Hashing.MessageDigest hashOffset += 4; } - // Reset the state and return - Reset(); return hash; } diff --git a/SabreTools.Hashing/MessageDigest/RipeMD160.cs b/SabreTools.Hashing/MessageDigest/RipeMD160.cs index b50c2c6..7c0b8bc 100644 --- a/SabreTools.Hashing/MessageDigest/RipeMD160.cs +++ b/SabreTools.Hashing/MessageDigest/RipeMD160.cs @@ -28,7 +28,7 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override void TransformBlock(byte[] data, int offset, int length) + protected override void HashCore(byte[] data, int offset, int length) { // Figure out how much buffer is needed int bufferLen = (int)(_totalBytes & 0x3f); @@ -83,7 +83,7 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override void Terminate() + protected override byte[] HashFinal() { // Determine the pad length int padLength = 64 - (int)(_totalBytes & 0x3f); @@ -102,16 +102,13 @@ namespace SabreTools.Hashing.MessageDigest padding[padLength - 4] = (byte)((totalBitCount >> 32) & 0xff); padding[padLength - 5] = (byte)((totalBitCount >> 24) & 0xff); padding[padLength - 6] = (byte)((totalBitCount >> 16) & 0xff); - padding[padLength - 7] = (byte)((totalBitCount >> 8 ) & 0xff); - padding[padLength - 8] = (byte)((totalBitCount >> 0 ) & 0xff); + padding[padLength - 7] = (byte)((totalBitCount >> 8) & 0xff); + padding[padLength - 8] = (byte)((totalBitCount >> 0) & 0xff); // Pad the block - TransformBlock(padding, 0, padding.Length); - } + HashCore(padding, 0, padding.Length); - /// - public override byte[] GetHash() - { + // Get the hash var hash = new byte[20]; int hashOffset = 0; @@ -123,8 +120,6 @@ namespace SabreTools.Hashing.MessageDigest hashOffset += 4; } - // Reset the state and return - Reset(); return hash; } diff --git a/SabreTools.Hashing/MessageDigest/RipeMD256.cs b/SabreTools.Hashing/MessageDigest/RipeMD256.cs index 9b2bf6d..49e1d4b 100644 --- a/SabreTools.Hashing/MessageDigest/RipeMD256.cs +++ b/SabreTools.Hashing/MessageDigest/RipeMD256.cs @@ -31,7 +31,7 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override void TransformBlock(byte[] data, int offset, int length) + protected override void HashCore(byte[] data, int offset, int length) { // Figure out how much buffer is needed int bufferLen = (int)(_totalBytes & 0x3f); @@ -86,7 +86,7 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override void Terminate() + protected override byte[] HashFinal() { // Determine the pad length int padLength = 64 - (int)(_totalBytes & 0x3f); @@ -109,12 +109,9 @@ namespace SabreTools.Hashing.MessageDigest padding[padLength - 8] = (byte)((totalBitCount >> 0) & 0xff); // Pad the block - TransformBlock(padding, 0, padding.Length); - } + HashCore(padding, 0, padding.Length); - /// - public override byte[] GetHash() - { + // Get the hash var hash = new byte[32]; int hashOffset = 0; @@ -126,8 +123,6 @@ namespace SabreTools.Hashing.MessageDigest hashOffset += 4; } - // Reset the state and return - Reset(); return hash; } diff --git a/SabreTools.Hashing/MessageDigest/RipeMD320.cs b/SabreTools.Hashing/MessageDigest/RipeMD320.cs index dc1f937..4bcd450 100644 --- a/SabreTools.Hashing/MessageDigest/RipeMD320.cs +++ b/SabreTools.Hashing/MessageDigest/RipeMD320.cs @@ -33,7 +33,7 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override void TransformBlock(byte[] data, int offset, int length) + protected override void HashCore(byte[] data, int offset, int length) { // Figure out how much buffer is needed int bufferLen = (int)(_totalBytes & 0x3f); @@ -88,7 +88,7 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override void Terminate() + protected override byte[] HashFinal() { // Determine the pad length int padLength = 64 - (int)(_totalBytes & 0x3f); @@ -107,16 +107,13 @@ namespace SabreTools.Hashing.MessageDigest padding[padLength - 4] = (byte)((totalBitCount >> 32) & 0xff); padding[padLength - 5] = (byte)((totalBitCount >> 24) & 0xff); padding[padLength - 6] = (byte)((totalBitCount >> 16) & 0xff); - padding[padLength - 7] = (byte)((totalBitCount >> 8 ) & 0xff); - padding[padLength - 8] = (byte)((totalBitCount >> 0 ) & 0xff); + padding[padLength - 7] = (byte)((totalBitCount >> 8) & 0xff); + padding[padLength - 8] = (byte)((totalBitCount >> 0) & 0xff); // Pad the block - TransformBlock(padding, 0, padding.Length); - } + HashCore(padding, 0, padding.Length); - /// - public override byte[] GetHash() - { + // Get the hash var hash = new byte[40]; int hashOffset = 0; @@ -128,8 +125,6 @@ namespace SabreTools.Hashing.MessageDigest hashOffset += 4; } - // Reset the state and return - Reset(); return hash; } diff --git a/SabreTools.Hashing/MessageDigest/Tiger128_3.cs b/SabreTools.Hashing/MessageDigest/Tiger128_3.cs index 454e4e9..e035636 100644 --- a/SabreTools.Hashing/MessageDigest/Tiger128_3.cs +++ b/SabreTools.Hashing/MessageDigest/Tiger128_3.cs @@ -14,9 +14,9 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override byte[] GetHash() + protected override byte[] HashFinal() { - byte[] hash = base.GetHash(); + byte[] hash = base.HashFinal(); byte[] trimmedHash = new byte[16]; Array.Copy(hash, trimmedHash, 16); return trimmedHash; diff --git a/SabreTools.Hashing/MessageDigest/Tiger128_4.cs b/SabreTools.Hashing/MessageDigest/Tiger128_4.cs index 50cae4b..fc6c642 100644 --- a/SabreTools.Hashing/MessageDigest/Tiger128_4.cs +++ b/SabreTools.Hashing/MessageDigest/Tiger128_4.cs @@ -14,9 +14,9 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override byte[] GetHash() + protected override byte[] HashFinal() { - byte[] hash = base.GetHash(); + byte[] hash = base.HashFinal(); byte[] trimmedHash = new byte[16]; Array.Copy(hash, trimmedHash, 16); return trimmedHash; diff --git a/SabreTools.Hashing/MessageDigest/Tiger160_3.cs b/SabreTools.Hashing/MessageDigest/Tiger160_3.cs index e26b72b..5e120f2 100644 --- a/SabreTools.Hashing/MessageDigest/Tiger160_3.cs +++ b/SabreTools.Hashing/MessageDigest/Tiger160_3.cs @@ -14,9 +14,9 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override byte[] GetHash() + protected override byte[] HashFinal() { - byte[] hash = base.GetHash(); + byte[] hash = base.HashFinal(); byte[] trimmedHash = new byte[20]; Array.Copy(hash, trimmedHash, 20); return trimmedHash; diff --git a/SabreTools.Hashing/MessageDigest/Tiger160_4.cs b/SabreTools.Hashing/MessageDigest/Tiger160_4.cs index 56277c9..adf5825 100644 --- a/SabreTools.Hashing/MessageDigest/Tiger160_4.cs +++ b/SabreTools.Hashing/MessageDigest/Tiger160_4.cs @@ -14,9 +14,9 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override byte[] GetHash() + protected override byte[] HashFinal() { - byte[] hash = base.GetHash(); + byte[] hash = base.HashFinal(); byte[] trimmedHash = new byte[20]; Array.Copy(hash, trimmedHash, 20); return trimmedHash; diff --git a/SabreTools.Hashing/MessageDigest/Tiger2_128_3.cs b/SabreTools.Hashing/MessageDigest/Tiger2_128_3.cs index 5ad2ba5..c8014df 100644 --- a/SabreTools.Hashing/MessageDigest/Tiger2_128_3.cs +++ b/SabreTools.Hashing/MessageDigest/Tiger2_128_3.cs @@ -14,9 +14,9 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override byte[] GetHash() + protected override byte[] HashFinal() { - byte[] hash = base.GetHash(); + byte[] hash = base.HashFinal(); byte[] trimmedHash = new byte[16]; Array.Copy(hash, trimmedHash, 16); return trimmedHash; diff --git a/SabreTools.Hashing/MessageDigest/Tiger2_128_4.cs b/SabreTools.Hashing/MessageDigest/Tiger2_128_4.cs index 0f80b3c..237e2e4 100644 --- a/SabreTools.Hashing/MessageDigest/Tiger2_128_4.cs +++ b/SabreTools.Hashing/MessageDigest/Tiger2_128_4.cs @@ -14,9 +14,9 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override byte[] GetHash() + protected override byte[] HashFinal() { - byte[] hash = base.GetHash(); + byte[] hash = base.HashFinal(); byte[] trimmedHash = new byte[16]; Array.Copy(hash, trimmedHash, 16); return trimmedHash; diff --git a/SabreTools.Hashing/MessageDigest/Tiger2_160_3.cs b/SabreTools.Hashing/MessageDigest/Tiger2_160_3.cs index 8f52258..82ebb56 100644 --- a/SabreTools.Hashing/MessageDigest/Tiger2_160_3.cs +++ b/SabreTools.Hashing/MessageDigest/Tiger2_160_3.cs @@ -14,9 +14,9 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override byte[] GetHash() + protected override byte[] HashFinal() { - byte[] hash = base.GetHash(); + byte[] hash = base.HashFinal(); byte[] trimmedHash = new byte[20]; Array.Copy(hash, trimmedHash, 20); return trimmedHash; diff --git a/SabreTools.Hashing/MessageDigest/Tiger2_160_4.cs b/SabreTools.Hashing/MessageDigest/Tiger2_160_4.cs index f706b63..92dc49c 100644 --- a/SabreTools.Hashing/MessageDigest/Tiger2_160_4.cs +++ b/SabreTools.Hashing/MessageDigest/Tiger2_160_4.cs @@ -14,9 +14,9 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override byte[] GetHash() + protected override byte[] HashFinal() { - byte[] hash = base.GetHash(); + byte[] hash = base.HashFinal(); byte[] trimmedHash = new byte[20]; Array.Copy(hash, trimmedHash, 20); return trimmedHash; diff --git a/SabreTools.Hashing/MessageDigest/TigerHashBase.cs b/SabreTools.Hashing/MessageDigest/TigerHashBase.cs index 5dbe59b..b830bf0 100644 --- a/SabreTools.Hashing/MessageDigest/TigerHashBase.cs +++ b/SabreTools.Hashing/MessageDigest/TigerHashBase.cs @@ -35,7 +35,7 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override void TransformBlock(byte[] data, int offset, int length) + protected override void HashCore(byte[] data, int offset, int length) { // Figure out how much buffer is needed int bufferLen = (int)(_totalBytes & 0x3f); @@ -90,7 +90,7 @@ namespace SabreTools.Hashing.MessageDigest } /// - public override void Terminate() + protected override byte[] HashFinal() { // Determine the pad length int padLength = 64 - (int)(_totalBytes & 0x3f); @@ -113,12 +113,9 @@ namespace SabreTools.Hashing.MessageDigest padding[padLength - 8] = (byte)((totalBitCount >> 0) & 0xff); // Pad the block - TransformBlock(padding, 0, padding.Length); - } + HashCore(padding, 0, padding.Length); - /// - public override byte[] GetHash() - { + // Get the hash var hash = new byte[24]; int hashOffset = 0; @@ -130,8 +127,6 @@ namespace SabreTools.Hashing.MessageDigest hashOffset += 8; } - // Reset the state and return - Reset(); return hash; }