-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
122 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters