Skip to content

Commit

Permalink
Add CRC32 hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
nRafinia committed Mar 11, 2023
1 parent 34340b8 commit 5f903c6
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/nHash/Features/HashAlgorithmFeature.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Security.Cryptography;
using nHash.Providers.Hashing;

namespace nHash.Features;

Expand Down Expand Up @@ -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<string, HashAlgorithm>()
var algorithms = new Dictionary<string, IHash>()
{
{ "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)
Expand Down
39 changes: 39 additions & 0 deletions src/nHash/Providers/Hashing/CRC32Hash.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
6 changes: 6 additions & 0 deletions src/nHash/Providers/Hashing/IHash.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace nHash.Providers.Hashing;

public interface IHash
{
public byte[] ComputeHash(byte[] buffer);
}
13 changes: 13 additions & 0 deletions src/nHash/Providers/Hashing/MD5Hash.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
13 changes: 13 additions & 0 deletions src/nHash/Providers/Hashing/SHA1Hash.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
13 changes: 13 additions & 0 deletions src/nHash/Providers/Hashing/SHA256Hash.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
13 changes: 13 additions & 0 deletions src/nHash/Providers/Hashing/SHA384Hash.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
13 changes: 13 additions & 0 deletions src/nHash/Providers/Hashing/SHA512Hash.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 4 additions & 0 deletions src/nHash/nHash.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>

<ItemGroup>
<Folder Include="Helpers" />
</ItemGroup>
</Project>

0 comments on commit 5f903c6

Please sign in to comment.