From 3768e7d077277382f14c4bff88313e3f319e88f5 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sat, 3 Feb 2018 17:01:17 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9BMoved=20checksums=20to=20interface.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Adler32Context.cs | 2 +- CRC16Context.cs | 2 +- CRC32Context.cs | 2 +- CRC64Context.cs | 2 +- DiscImageChef.Checksums.csproj | 3 +- FletcherContext.cs | 6 ++-- IChecksum.cs | 65 ++++++++++++++++++++++++++++++++++ MD5Context.cs | 2 +- RIPEMD160Context.cs | 2 +- SHA1Context.cs | 2 +- SHA256Context.cs | 2 +- SHA384Context.cs | 2 +- SHA512Context.cs | 2 +- SpamSumContext.cs | 2 +- 14 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 IChecksum.cs diff --git a/Adler32Context.cs b/Adler32Context.cs index f6fa9db6e..083ae3104 100644 --- a/Adler32Context.cs +++ b/Adler32Context.cs @@ -39,7 +39,7 @@ namespace DiscImageChef.Checksums /// /// Implements the Adler-32 algorithm /// - public class Adler32Context + public class Adler32Context : IChecksum { const ushort ADLER_MODULE = 65521; ushort sum1, sum2; diff --git a/CRC16Context.cs b/CRC16Context.cs index 7bcb2155f..ec65593b5 100644 --- a/CRC16Context.cs +++ b/CRC16Context.cs @@ -39,7 +39,7 @@ namespace DiscImageChef.Checksums /// /// Implements a CRC16-CCITT algorithm /// - public class Crc16Context + public class Crc16Context : IChecksum { const ushort CRC16_POLY = 0xA001; const ushort CRC16_SEED = 0x0000; diff --git a/CRC32Context.cs b/CRC32Context.cs index 97680b60c..e2319969c 100644 --- a/CRC32Context.cs +++ b/CRC32Context.cs @@ -39,7 +39,7 @@ namespace DiscImageChef.Checksums /// /// Implements a CRC32 algorithm /// - public class Crc32Context + public class Crc32Context : IChecksum { const uint CRC32_POLY = 0xEDB88320; const uint CRC32_SEED = 0xFFFFFFFF; diff --git a/CRC64Context.cs b/CRC64Context.cs index 35f3be4c9..b1bc62bd6 100644 --- a/CRC64Context.cs +++ b/CRC64Context.cs @@ -39,7 +39,7 @@ namespace DiscImageChef.Checksums /// /// Implements a CRC64 (ECMA) algorithm /// - public class Crc64Context + public class Crc64Context : IChecksum { const ulong CRC64_POLY = 0xC96C5795D7870F42; const ulong CRC64_SEED = 0xFFFFFFFFFFFFFFFF; diff --git a/DiscImageChef.Checksums.csproj b/DiscImageChef.Checksums.csproj index 9a5bfdda2..bc35cb3f5 100644 --- a/DiscImageChef.Checksums.csproj +++ b/DiscImageChef.Checksums.csproj @@ -1,4 +1,4 @@ - + Debug @@ -34,6 +34,7 @@ + diff --git a/FletcherContext.cs b/FletcherContext.cs index da63771f8..a79c73951 100644 --- a/FletcherContext.cs +++ b/FletcherContext.cs @@ -31,14 +31,14 @@ // ****************************************************************************/ // Disabled because the speed is abnormally slow -/* + using System; using System.IO; using System.Text; namespace DiscImageChef.Checksums { - public class Fletcher32Context + public class Fletcher32Context : IChecksum { bool inodd; byte oddValue; @@ -423,4 +423,4 @@ namespace DiscImageChef.Checksums return Data(data, (uint)data.Length, out hash); } } -}*/ \ No newline at end of file +} \ No newline at end of file diff --git a/IChecksum.cs b/IChecksum.cs new file mode 100644 index 000000000..a29a96b1a --- /dev/null +++ b/IChecksum.cs @@ -0,0 +1,65 @@ +// /*************************************************************************** +// The Disc Image Chef +// ---------------------------------------------------------------------------- +// +// Filename : IChecksum.cs +// Author(s) : Natalia Portillo +// +// Component : Checksums. +// +// --[ Description ] ---------------------------------------------------------- +// +// Provides an interface for implementing checksums and hashes. +// +// --[ License ] -------------------------------------------------------------- +// +// This library is free software; you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation; either version 2.1 of the +// License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, see . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2018 Natalia Portillo +// ****************************************************************************/ + +namespace DiscImageChef.Checksums +{ + public interface IChecksum + { + /// + /// Initializes the algorithm + /// + void Init(); + + /// + /// Updates the hash with data. + /// + /// Data buffer. + /// Length of buffer to hash. + void Update(byte[] data, uint len); + + /// + /// Updates the hash with data. + /// + /// Data buffer. + void Update(byte[] data); + + /// + /// Returns a byte array of the hash value. + /// + byte[] Final(); + + /// + /// Returns a hexadecimal representation of the hash value. + /// + string End(); + } +} \ No newline at end of file diff --git a/MD5Context.cs b/MD5Context.cs index 8e3bc3f53..6fd0dc2ed 100644 --- a/MD5Context.cs +++ b/MD5Context.cs @@ -39,7 +39,7 @@ namespace DiscImageChef.Checksums /// /// Wraps up .NET MD5 implementation to a Init(), Update(), Final() context. /// - public class Md5Context + public class Md5Context : IChecksum { MD5 md5Provider; diff --git a/RIPEMD160Context.cs b/RIPEMD160Context.cs index c9bca1911..120f2cbd3 100644 --- a/RIPEMD160Context.cs +++ b/RIPEMD160Context.cs @@ -39,7 +39,7 @@ namespace DiscImageChef.Checksums /// /// Wraps up .NET RIPEMD160 implementation to a Init(), Update(), Final() context. /// - public class Ripemd160Context + public class Ripemd160Context : IChecksum { RIPEMD160 ripemd160Provider; diff --git a/SHA1Context.cs b/SHA1Context.cs index 8de3a5d7c..f62887a52 100644 --- a/SHA1Context.cs +++ b/SHA1Context.cs @@ -39,7 +39,7 @@ namespace DiscImageChef.Checksums /// /// Wraps up .NET SHA1 implementation to a Init(), Update(), Final() context. /// - public class Sha1Context + public class Sha1Context : IChecksum { SHA1 sha1Provider; diff --git a/SHA256Context.cs b/SHA256Context.cs index 3a062e32a..43e107345 100644 --- a/SHA256Context.cs +++ b/SHA256Context.cs @@ -39,7 +39,7 @@ namespace DiscImageChef.Checksums /// /// Wraps up .NET SHA256 implementation to a Init(), Update(), Final() context. /// - public class Sha256Context + public class Sha256Context : IChecksum { SHA256 sha256Provider; diff --git a/SHA384Context.cs b/SHA384Context.cs index 225ac75a0..1fd534cba 100644 --- a/SHA384Context.cs +++ b/SHA384Context.cs @@ -39,7 +39,7 @@ namespace DiscImageChef.Checksums /// /// Wraps up .NET SHA384 implementation to a Init(), Update(), Final() context. /// - public class Sha384Context + public class Sha384Context : IChecksum { SHA384 sha384Provider; diff --git a/SHA512Context.cs b/SHA512Context.cs index 94b3bca38..5beb4cce9 100644 --- a/SHA512Context.cs +++ b/SHA512Context.cs @@ -39,7 +39,7 @@ namespace DiscImageChef.Checksums /// /// Wraps up .NET SHA512 implementation to a Init(), Update(), Final() context. /// - public class Sha512Context + public class Sha512Context : IChecksum { SHA512 sha512Provider; diff --git a/SpamSumContext.cs b/SpamSumContext.cs index 30a1c1f5d..148783225 100644 --- a/SpamSumContext.cs +++ b/SpamSumContext.cs @@ -47,7 +47,7 @@ namespace DiscImageChef.Checksums /// /// Implements the SpamSum fuzzy hashing algorithm. /// - public class SpamSumContext + public class SpamSumContext : IChecksum { const uint ROLLING_WINDOW = 7; const uint MIN_BLOCKSIZE = 3;