-
Notifications
You must be signed in to change notification settings - Fork 42
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
zawawa
committed
Oct 16, 2017
1 parent
0230346
commit 50a8f7b
Showing
113 changed files
with
52,821 additions
and
6 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="HashLib" version="2.0.1" targetFramework="net45" /> | ||
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" /> | ||
</packages> |
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,44 @@ | ||
using System; | ||
using System.Diagnostics; | ||
|
||
namespace HashLib.Checksum | ||
{ | ||
internal class Adler32 : Hash, IChecksum, IBlockHash, IHash32 | ||
{ | ||
private const uint MOD_ADLER = 65521; | ||
|
||
private uint m_a; | ||
private uint m_b; | ||
|
||
public Adler32() | ||
: base(4, 1) | ||
{ | ||
} | ||
|
||
public override void Initialize() | ||
{ | ||
m_a = 1; | ||
m_b = 0; | ||
} | ||
|
||
public override void TransformBytes(byte[] a_data, int a_index, int a_length) | ||
{ | ||
Debug.Assert(a_index >= 0); | ||
Debug.Assert(a_length >= 0); | ||
Debug.Assert(a_index + a_length <= a_data.Length); | ||
|
||
for (int i = a_index; a_length > 0; i++, a_length--) | ||
{ | ||
m_a = (m_a + a_data[i]) % MOD_ADLER; | ||
m_b = (m_b + m_a) % MOD_ADLER; | ||
|
||
} | ||
} | ||
|
||
public override HashResult TransformFinal() | ||
{ | ||
return new HashResult((m_b << 16) | m_a); | ||
|
||
} | ||
} | ||
} |
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,102 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
|
||
namespace HashLib.Checksum | ||
{ | ||
public static class CRC32Polynomials | ||
{ | ||
public static uint IEEE_802_3 = 0xEDB88320; | ||
public static uint Castagnoli = 0x82F63B78; | ||
public static uint Koopman = 0xEB31D82E; | ||
public static uint CRC_32Q = 0xD5828281; | ||
} | ||
|
||
internal class CRC32_IEEE : CRC32 | ||
{ | ||
public CRC32_IEEE() | ||
: base(CRC32Polynomials.IEEE_802_3) | ||
{ | ||
} | ||
} | ||
|
||
internal class CRC32_CASTAGNOLI : CRC32 | ||
{ | ||
public CRC32_CASTAGNOLI() | ||
: base(CRC32Polynomials.Castagnoli) | ||
{ | ||
} | ||
} | ||
|
||
internal class CRC32_KOOPMAN : CRC32 | ||
{ | ||
public CRC32_KOOPMAN() | ||
: base(CRC32Polynomials.Koopman) | ||
{ | ||
} | ||
} | ||
|
||
internal class CRC32_Q : CRC32 | ||
{ | ||
public CRC32_Q() | ||
: base(CRC32Polynomials.CRC_32Q) | ||
{ | ||
} | ||
} | ||
|
||
internal class CRC32 : Hash, IChecksum, IBlockHash, IHash32 | ||
{ | ||
private uint[] m_crc_tab = new uint[256]; | ||
|
||
private uint m_hash; | ||
private uint m_initial_value; | ||
private uint m_final_xor; | ||
|
||
public CRC32(uint a_polynomial, uint a_initial_value = uint.MaxValue, uint a_final_xor = uint.MaxValue) | ||
: base(4, 1) | ||
{ | ||
m_initial_value = a_initial_value; | ||
m_final_xor = a_final_xor; | ||
|
||
GenerateCRCTable(a_polynomial); | ||
} | ||
|
||
private void GenerateCRCTable(uint a_poly32) | ||
{ | ||
for (uint i = 0; i < 256; ++i) | ||
{ | ||
uint crc = i; | ||
|
||
for (int j = 0; j < 8; j++) | ||
{ | ||
if ((crc & 1) == 1) | ||
crc = (crc >> 1) ^ a_poly32; | ||
else | ||
crc = crc >> 1; | ||
} | ||
|
||
m_crc_tab[i] = crc; | ||
} | ||
} | ||
|
||
public override void Initialize() | ||
{ | ||
m_hash = m_initial_value; | ||
} | ||
|
||
public override void TransformBytes(byte[] a_data, int a_index, int a_length) | ||
{ | ||
Debug.Assert(a_index >= 0); | ||
Debug.Assert(a_length >= 0); | ||
Debug.Assert(a_index + a_length <= a_data.Length); | ||
|
||
for (int i = a_index; a_length > 0; i++, a_length--) | ||
m_hash = (m_hash >> 8) ^ m_crc_tab[(byte)m_hash ^ a_data[i]]; | ||
} | ||
|
||
public override HashResult TransformFinal() | ||
{ | ||
return new HashResult(m_hash ^ m_final_xor); | ||
} | ||
} | ||
} |
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,83 @@ | ||
using System; | ||
using System.Diagnostics; | ||
|
||
namespace HashLib.Checksum | ||
{ | ||
public static class CRC64Polynomials | ||
{ | ||
public static ulong ISO = 0xD800000000000000; | ||
public static ulong ECMA_182 = 0xC96C5795D7870F42; | ||
} | ||
|
||
internal class CRC64_ISO : CRC64 | ||
{ | ||
public CRC64_ISO() | ||
: base(CRC64Polynomials.ISO) | ||
{ | ||
} | ||
} | ||
|
||
internal class CRC64_ECMA : CRC64 | ||
{ | ||
public CRC64_ECMA() | ||
: base(CRC64Polynomials.ECMA_182) | ||
{ | ||
} | ||
} | ||
|
||
internal class CRC64 : Hash, IChecksum, IBlockHash, IHash64 | ||
{ | ||
private ulong[] m_crc_tab = new ulong[256]; | ||
|
||
private ulong m_hash; | ||
private ulong m_initial_value; | ||
private ulong m_final_xor; | ||
|
||
public CRC64(ulong a_polynomial, ulong a_initial_value = ulong.MaxValue, ulong a_final_xor = ulong.MaxValue) | ||
: base(8, 1) | ||
{ | ||
m_initial_value = a_initial_value; | ||
m_final_xor = a_final_xor; | ||
|
||
GenerateCRCTable(a_polynomial); | ||
} | ||
|
||
private void GenerateCRCTable(ulong a_poly64) | ||
{ | ||
for (uint i = 0; i < 256; ++i) | ||
{ | ||
ulong crc = i; | ||
|
||
for (uint j = 0; j < 8; ++j) | ||
{ | ||
if ((crc & 1) == 1) | ||
crc = (crc >> 1) ^ a_poly64; | ||
else | ||
crc >>= 1; | ||
} | ||
|
||
m_crc_tab[i] = crc; | ||
} | ||
} | ||
|
||
public override void Initialize() | ||
{ | ||
m_hash = m_initial_value; | ||
} | ||
|
||
public override void TransformBytes(byte[] a_data, int a_index, int a_length) | ||
{ | ||
Debug.Assert(a_index >= 0); | ||
Debug.Assert(a_length >= 0); | ||
Debug.Assert(a_index + a_length <= a_data.Length); | ||
|
||
for (int i = a_index; a_length > 0; i++, a_length--) | ||
m_hash = (m_hash >> 8) ^ m_crc_tab[(byte)m_hash ^ a_data[i]]; | ||
} | ||
|
||
public override HashResult TransformFinal() | ||
{ | ||
return new HashResult(m_hash ^ m_final_xor); | ||
} | ||
} | ||
} |
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,17 @@ | ||
using System; | ||
|
||
namespace HashLib.Crypto.BuildIn | ||
{ | ||
internal class MD5CryptoServiceProvider : HashCryptoBuildIn, IHasHMACBuildIn | ||
{ | ||
public MD5CryptoServiceProvider() | ||
: base(new System.Security.Cryptography.MD5CryptoServiceProvider(), 64) | ||
{ | ||
} | ||
|
||
public virtual System.Security.Cryptography.HMAC GetBuildHMAC() | ||
{ | ||
return new System.Security.Cryptography.HMACMD5(); | ||
} | ||
} | ||
} |
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,17 @@ | ||
using System; | ||
|
||
namespace HashLib.Crypto.BuildIn | ||
{ | ||
internal class RIPEMD160Managed : HashCryptoBuildIn, IHasHMACBuildIn | ||
{ | ||
public RIPEMD160Managed() | ||
: base(new System.Security.Cryptography.RIPEMD160Managed(), 64) | ||
{ | ||
} | ||
|
||
public virtual System.Security.Cryptography.HMAC GetBuildHMAC() | ||
{ | ||
return new System.Security.Cryptography.HMACRIPEMD160(); | ||
} | ||
} | ||
} |
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,12 @@ | ||
using System; | ||
|
||
namespace HashLib.Crypto.BuildIn | ||
{ | ||
internal class SHA1Cng : HashCryptoBuildIn | ||
{ | ||
public SHA1Cng() | ||
: base(new System.Security.Cryptography.SHA1Cng(), 64) | ||
{ | ||
} | ||
} | ||
} |
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,17 @@ | ||
using System; | ||
|
||
namespace HashLib.Crypto.BuildIn | ||
{ | ||
internal class SHA1CryptoServiceProvider : HashCryptoBuildIn, IHasHMACBuildIn | ||
{ | ||
public SHA1CryptoServiceProvider() | ||
: base(new System.Security.Cryptography.SHA1CryptoServiceProvider(), 64) | ||
{ | ||
} | ||
|
||
public virtual System.Security.Cryptography.HMAC GetBuildHMAC() | ||
{ | ||
return new System.Security.Cryptography.HMACSHA1(new byte[0], false); | ||
} | ||
} | ||
} |
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,17 @@ | ||
using System; | ||
|
||
namespace HashLib.Crypto.BuildIn | ||
{ | ||
internal class SHA1Managed : HashCryptoBuildIn, IHasHMACBuildIn | ||
{ | ||
public SHA1Managed() | ||
: base(new System.Security.Cryptography.SHA1Managed(), 64) | ||
{ | ||
} | ||
|
||
public virtual System.Security.Cryptography.HMAC GetBuildHMAC() | ||
{ | ||
return new System.Security.Cryptography.HMACSHA1(new byte[0], true); | ||
} | ||
} | ||
} |
Oops, something went wrong.