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

121 lines
3.4 KiB
C#
Raw Normal View History

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