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

122 lines
3.6 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;
2016-04-18 12:50:57 +02:00
namespace TLSharp.Core.MTProto.Crypto
{
2020-04-05 13:33:18 +02:00
/// <summary>
/// Implements a 32-bit CRC hash algorithm compatible with Zip etc.
/// </summary>
/// <remarks>
/// Crc32 should only be used for backward compatibility with older file formats
/// and algorithms. It is not secure enough for new applications.
/// If you need to call multiple times for the same data either use the HashAlgorithm
/// interface or remember that the result of one Compute call needs to be ~ (XOR) before
/// being passed in as the seed for the next Compute call.
/// </remarks>
public sealed class Crc32 : HashAlgorithm
2016-04-18 12:50:57 +02:00
{
2015-09-28 04:01:17 +02:00
public const UInt32 DefaultPolynomial = 0xedb88320u;
public const UInt32 DefaultSeed = 0xffffffffu;
2020-04-05 13:33:18 +02:00
static UInt32[] defaultTable;
readonly UInt32 seed;
readonly UInt32[] table;
UInt32 hash;
2015-09-28 04:01:17 +02:00
2016-04-18 12:50:57 +02:00
public Crc32()
2020-04-05 13:33:18 +02:00
: this(DefaultPolynomial, DefaultSeed)
2016-04-18 12:50:57 +02:00
{
2015-09-28 04:01:17 +02:00
}
2016-04-18 12:50:57 +02:00
public Crc32(UInt32 polynomial, UInt32 seed)
{
2020-04-05 13:33:18 +02:00
if (!BitConverter.IsLittleEndian)
throw new PlatformNotSupportedException("Not supported on Big Endian processors");
2015-09-28 04:01:17 +02:00
table = InitializeTable(polynomial);
2020-04-05 13:33:18 +02:00
this.seed = hash = seed;
2015-09-28 04:01:17 +02:00
}
2016-04-18 12:50:57 +02:00
public override void Initialize()
{
2015-09-28 04:01:17 +02:00
hash = seed;
}
2020-04-05 13:33:18 +02:00
protected override void HashCore(byte[] array, int ibStart, int cbSize)
2016-04-18 12:50:57 +02:00
{
2020-04-05 13:33:18 +02:00
hash = CalculateHash(table, hash, array, ibStart, cbSize);
2015-09-28 04:01:17 +02:00
}
2016-04-18 12:50:57 +02:00
protected override byte[] HashFinal()
{
2020-04-05 13:33:18 +02:00
var hashBuffer = UInt32ToBigEndianBytes(~hash);
HashValue = hashBuffer;
2015-09-28 04:01:17 +02:00
return hashBuffer;
}
2020-04-05 13:33:18 +02:00
public override int HashSize { get { return 32; } }
2015-09-28 04:01:17 +02:00
2016-04-18 12:50:57 +02:00
public static UInt32 Compute(byte[] buffer)
{
2020-04-05 13:33:18 +02:00
return Compute(DefaultSeed, buffer);
2015-09-28 04:01:17 +02:00
}
2016-04-18 12:50:57 +02:00
public static UInt32 Compute(UInt32 seed, byte[] buffer)
{
2020-04-05 13:33:18 +02:00
return Compute(DefaultPolynomial, seed, buffer);
2015-09-28 04:01:17 +02:00
}
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);
}
2020-04-05 13:33:18 +02:00
static UInt32[] InitializeTable(UInt32 polynomial)
2016-04-18 12:50:57 +02:00
{
2015-09-28 04:01:17 +02:00
if (polynomial == DefaultPolynomial && defaultTable != null)
return defaultTable;
2020-04-05 13:33:18 +02:00
var createTable = new UInt32[256];
for (var i = 0; i < 256; i++)
2016-04-18 12:50:57 +02:00
{
2020-04-05 13:33:18 +02:00
var entry = (UInt32)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
2020-04-05 13:33:18 +02:00
entry >>= 1;
2015-09-28 04:01:17 +02:00
createTable[i] = entry;
}
if (polynomial == DefaultPolynomial)
defaultTable = createTable;
return createTable;
}
2020-04-05 13:33:18 +02:00
static UInt32 CalculateHash(UInt32[] table, UInt32 seed, IList<byte> buffer, int start, int size)
2016-04-18 12:50:57 +02:00
{
2020-04-05 13:33:18 +02:00
var hash = seed;
for (var i = start; i < start + size; i++)
hash = (hash >> 8) ^ table[buffer[i] ^ hash & 0xff];
return hash;
2015-09-28 04:01:17 +02:00
}
2020-04-05 13:33:18 +02:00
static byte[] UInt32ToBigEndianBytes(UInt32 uint32)
2016-04-18 12:50:57 +02:00
{
2020-04-05 13:33:18 +02:00
var result = BitConverter.GetBytes(uint32);
if (BitConverter.IsLittleEndian)
Array.Reverse(result);
return result;
2015-09-28 04:01:17 +02:00
}
}
2020-04-05 13:33:18 +02:00
2015-09-28 04:01:17 +02:00
}