diff --git a/src/nHash/Features/HashAlgorithmFeature.cs b/src/nHash/Features/HashAlgorithmFeature.cs index 2710a5d..bb3a35b 100644 --- a/src/nHash/Features/HashAlgorithmFeature.cs +++ b/src/nHash/Features/HashAlgorithmFeature.cs @@ -1,4 +1,5 @@ using System.Security.Cryptography; +using nHash.Providers.Hashing; namespace nHash.Features; @@ -55,13 +56,14 @@ private static void CalculateText(string text, bool lowerCase, string fileName) private static void CalculateHash(byte[] inputBytes, bool lowerCase) { - var algorithms = new Dictionary() + var algorithms = new Dictionary() { - { "MD5", MD5.Create() }, - { "SHA-1", SHA1.Create() }, - { "SHA-256", SHA256.Create() }, - { "SHA-384", SHA384.Create() }, - { "SHA-512", SHA512.Create() } + { "MD5", new MD5Hash() }, + { "SHA-1", new SHA1Hash() }, + { "SHA-256", new SHA256Hash() }, + { "SHA-384", new SHA384Hash() }, + { "SHA-512", new SHA512Hash() }, + { "CRC32", new CRC32Hash() } }; foreach (var algorithm in algorithms) diff --git a/src/nHash/Providers/Hashing/CRC32Hash.cs b/src/nHash/Providers/Hashing/CRC32Hash.cs new file mode 100644 index 0000000..68175bf --- /dev/null +++ b/src/nHash/Providers/Hashing/CRC32Hash.cs @@ -0,0 +1,39 @@ +namespace nHash.Providers.Hashing; + +public class CRC32Hash : IHash +{ + private readonly uint[] _checksumTable; + private const uint Polynomial = 0xEDB88320; + + public CRC32Hash() + { + _checksumTable = new uint[0x100]; + + for (uint index = 0; index < 0x100; ++index) + { + var item = index; + for (int bit = 0; bit < 8; ++bit) + item = ((item & 1) != 0) ? (Polynomial ^ (item >> 1)) : (item >> 1); + _checksumTable[index] = item; + } + } + + public byte[] ComputeHash(byte[] buffer) + { + using var stream = new MemoryStream(buffer); + return ComputeHash(stream); + } + + private byte[] ComputeHash(Stream stream) + { + var result = 0xFFFFFFFF; + + int current; + while ((current = stream.ReadByte()) != -1) + result = _checksumTable[(result & 0xFF) ^ (byte)current] ^ (result >> 8); + + var hash = BitConverter.GetBytes(~result); + Array.Reverse(hash); + return hash; + } +} \ No newline at end of file diff --git a/src/nHash/Providers/Hashing/IHash.cs b/src/nHash/Providers/Hashing/IHash.cs new file mode 100644 index 0000000..8bb7666 --- /dev/null +++ b/src/nHash/Providers/Hashing/IHash.cs @@ -0,0 +1,6 @@ +namespace nHash.Providers.Hashing; + +public interface IHash +{ + public byte[] ComputeHash(byte[] buffer); +} \ No newline at end of file diff --git a/src/nHash/Providers/Hashing/MD5Hash.cs b/src/nHash/Providers/Hashing/MD5Hash.cs new file mode 100644 index 0000000..daefdde --- /dev/null +++ b/src/nHash/Providers/Hashing/MD5Hash.cs @@ -0,0 +1,13 @@ +using System.Security.Cryptography; + +namespace nHash.Providers.Hashing; + +public class MD5Hash : IHash +{ + private readonly MD5 _provider = MD5.Create(); + + public byte[] ComputeHash(byte[] buffer) + { + return _provider.ComputeHash(buffer); + } +} \ No newline at end of file diff --git a/src/nHash/Providers/Hashing/SHA1Hash.cs b/src/nHash/Providers/Hashing/SHA1Hash.cs new file mode 100644 index 0000000..3cf8295 --- /dev/null +++ b/src/nHash/Providers/Hashing/SHA1Hash.cs @@ -0,0 +1,13 @@ +using System.Security.Cryptography; + +namespace nHash.Providers.Hashing; + +public class SHA1Hash : IHash +{ + private readonly SHA1 _provider = SHA1.Create(); + + public byte[] ComputeHash(byte[] buffer) + { + return _provider.ComputeHash(buffer); + } +} \ No newline at end of file diff --git a/src/nHash/Providers/Hashing/SHA256Hash.cs b/src/nHash/Providers/Hashing/SHA256Hash.cs new file mode 100644 index 0000000..b68c77d --- /dev/null +++ b/src/nHash/Providers/Hashing/SHA256Hash.cs @@ -0,0 +1,13 @@ +using System.Security.Cryptography; + +namespace nHash.Providers.Hashing; + +public class SHA256Hash : IHash +{ + private readonly SHA256 _provider = SHA256.Create(); + + public byte[] ComputeHash(byte[] buffer) + { + return _provider.ComputeHash(buffer); + } +} \ No newline at end of file diff --git a/src/nHash/Providers/Hashing/SHA384Hash.cs b/src/nHash/Providers/Hashing/SHA384Hash.cs new file mode 100644 index 0000000..8a534a3 --- /dev/null +++ b/src/nHash/Providers/Hashing/SHA384Hash.cs @@ -0,0 +1,13 @@ +using System.Security.Cryptography; + +namespace nHash.Providers.Hashing; + +public class SHA384Hash : IHash +{ + private readonly SHA384 _provider = SHA384.Create(); + + public byte[] ComputeHash(byte[] buffer) + { + return _provider.ComputeHash(buffer); + } +} \ No newline at end of file diff --git a/src/nHash/Providers/Hashing/SHA512Hash.cs b/src/nHash/Providers/Hashing/SHA512Hash.cs new file mode 100644 index 0000000..1a1656f --- /dev/null +++ b/src/nHash/Providers/Hashing/SHA512Hash.cs @@ -0,0 +1,13 @@ +using System.Security.Cryptography; + +namespace nHash.Providers.Hashing; + +public class SHA512Hash : IHash +{ + private readonly SHA512 _provider = SHA512.Create(); + + public byte[] ComputeHash(byte[] buffer) + { + return _provider.ComputeHash(buffer); + } +} \ No newline at end of file diff --git a/src/nHash/nHash.csproj b/src/nHash/nHash.csproj index f6ec287..079fbc6 100644 --- a/src/nHash/nHash.csproj +++ b/src/nHash/nHash.csproj @@ -16,4 +16,8 @@ + + + +