TLSharp/TLSharp.Core/MTProto/Crypto/Crc32.cs

112 lines
3.2 KiB
C#
Raw Normal View History

2017-04-13 08:38:01 +02:00
using System.Security.Cryptography;
2015-09-28 04:01:17 +02:00
2016-04-18 12:50:57 +02:00
namespace TLSharp.Core.MTProto.Crypto
{
public class Crc32 : HashAlgorithm
{
2017-04-13 08:38:01 +02:00
public const uint DefaultPolynomial = 0xedb88320u;
public const uint DefaultSeed = 0xffffffffu;
private static uint[] defaultTable;
2015-09-28 04:01:17 +02:00
2017-04-13 08:38:01 +02:00
private uint hash;
private readonly uint seed;
private readonly uint[] table;
2015-09-28 04:01:17 +02:00
2016-04-18 12:50:57 +02:00
public Crc32()
{
2015-09-28 04:01:17 +02:00
table = InitializeTable(DefaultPolynomial);
seed = DefaultSeed;
hash = seed;
}
2017-04-13 08:38:01 +02:00
public Crc32(uint polynomial, uint seed)
2016-04-18 12:50:57 +02:00
{
2015-09-28 04:01:17 +02:00
table = InitializeTable(polynomial);
this.seed = seed;
hash = seed;
}
2017-04-13 08:38:01 +02:00
public override int HashSize => 32;
2016-04-18 12:50:57 +02:00
public override void Initialize()
{
2015-09-28 04:01:17 +02:00
hash = seed;
}
2016-04-18 12:50:57 +02:00
protected override void HashCore(byte[] buffer, int start, int length)
{
2015-09-28 04:01:17 +02:00
hash = CalculateHash(table, hash, buffer, start, length);
}
/// <summary>
2017-04-13 08:38:01 +02:00
/// Возвращает хеш в BigEndian
2015-09-28 04:01:17 +02:00
/// </summary>
/// <returns></returns>
2016-04-18 12:50:57 +02:00
protected override byte[] HashFinal()
{
2017-04-13 08:38:01 +02:00
var hashBuffer = UInt32ToBigEndianBytes(~hash);
HashValue = hashBuffer;
2015-09-28 04:01:17 +02:00
return hashBuffer;
}
2017-04-13 08:38:01 +02:00
public static uint Compute(byte[] buffer)
2016-04-18 12:50:57 +02:00
{
2015-09-28 04:01:17 +02:00
return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length);
}
2017-04-13 08:38:01 +02:00
public static uint Compute(uint seed, byte[] buffer)
2016-04-18 12:50:57 +02:00
{
2015-09-28 04:01:17 +02:00
return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length);
}
2017-04-13 08:38:01 +02:00
public static uint Compute(uint polynomial, uint seed, byte[] buffer)
2016-04-18 12:50:57 +02:00
{
2015-09-28 04:01:17 +02:00
return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
}
2017-04-13 08:38:01 +02:00
private static uint[] InitializeTable(uint polynomial)
2016-04-18 12:50:57 +02:00
{
2015-09-28 04:01:17 +02:00
if (polynomial == DefaultPolynomial && defaultTable != null)
return defaultTable;
2017-04-13 08:38:01 +02:00
var createTable = new uint[256];
for (var i = 0; i < 256; i++)
2016-04-18 12:50:57 +02:00
{
2017-04-13 08:38:01 +02:00
var entry = (uint) i;
for (var j = 0; j < 8; j++)
2015-09-28 04:01:17 +02:00
if ((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial;
else
entry = entry >> 1;
createTable[i] = entry;
}
if (polynomial == DefaultPolynomial)
defaultTable = createTable;
return createTable;
}
2017-04-13 08:38:01 +02:00
private static uint CalculateHash(uint[] table, uint seed, byte[] buffer, int start, int size)
2016-04-18 12:50:57 +02:00
{
2017-04-13 08:38:01 +02:00
var crc = seed;
for (var i = start; i < size; i++)
2016-04-18 12:50:57 +02:00
unchecked
{
2017-04-13 08:38:01 +02:00
crc = (crc >> 8) ^ table[buffer[i] ^ (crc & 0xff)];
2015-09-28 04:01:17 +02:00
}
return crc;
}
2017-04-13 08:38:01 +02:00
private byte[] UInt32ToBigEndianBytes(uint x)
2016-04-18 12:50:57 +02:00
{
2017-04-13 08:38:01 +02:00
return new[]
{
(byte) ((x >> 24) & 0xff),
(byte) ((x >> 16) & 0xff),
(byte) ((x >> 8) & 0xff),
(byte) (x & 0xff)
2016-04-18 12:50:57 +02:00
};
2015-09-28 04:01:17 +02:00
}
}
2017-04-13 08:38:01 +02:00
}