mirror of
https://github.com/sochix/TLSharp.git
synced 2026-04-07 07:25:24 +00:00
Remove unnecessary dependencies
This commit is contained in:
parent
709e5be7cc
commit
6d2c77f5ef
11 changed files with 87 additions and 126 deletions
|
|
@ -4,32 +4,42 @@ using System.Linq;
|
|||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Ionic.Crc;
|
||||
|
||||
namespace TLSharp.Core.MTProto.Crypto
|
||||
{
|
||||
public class Crc32 : HashAlgorithm
|
||||
/// <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
|
||||
{
|
||||
public const UInt32 DefaultPolynomial = 0xedb88320u;
|
||||
public const UInt32 DefaultSeed = 0xffffffffu;
|
||||
|
||||
private UInt32 hash;
|
||||
private UInt32 seed;
|
||||
private UInt32[] table;
|
||||
private static UInt32[] defaultTable;
|
||||
static UInt32[] defaultTable;
|
||||
|
||||
readonly UInt32 seed;
|
||||
readonly UInt32[] table;
|
||||
UInt32 hash;
|
||||
|
||||
public Crc32()
|
||||
: this(DefaultPolynomial, DefaultSeed)
|
||||
{
|
||||
table = InitializeTable(DefaultPolynomial);
|
||||
seed = DefaultSeed;
|
||||
hash = seed;
|
||||
}
|
||||
|
||||
public Crc32(UInt32 polynomial, UInt32 seed)
|
||||
{
|
||||
if (!BitConverter.IsLittleEndian)
|
||||
throw new PlatformNotSupportedException("Not supported on Big Endian processors");
|
||||
|
||||
table = InitializeTable(polynomial);
|
||||
this.seed = seed;
|
||||
hash = seed;
|
||||
this.seed = hash = seed;
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
|
|
@ -37,35 +47,28 @@ namespace TLSharp.Core.MTProto.Crypto
|
|||
hash = seed;
|
||||
}
|
||||
|
||||
protected override void HashCore(byte[] buffer, int start, int length)
|
||||
protected override void HashCore(byte[] array, int ibStart, int cbSize)
|
||||
{
|
||||
hash = CalculateHash(table, hash, buffer, start, length);
|
||||
hash = CalculateHash(table, hash, array, ibStart, cbSize);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Возвращает хеш в BigEndian
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected override byte[] HashFinal()
|
||||
{
|
||||
byte[] hashBuffer = UInt32ToBigEndianBytes(~hash);
|
||||
this.HashValue = hashBuffer;
|
||||
var hashBuffer = UInt32ToBigEndianBytes(~hash);
|
||||
HashValue = hashBuffer;
|
||||
return hashBuffer;
|
||||
}
|
||||
|
||||
public override int HashSize
|
||||
{
|
||||
get { return 32; }
|
||||
}
|
||||
public override int HashSize { get { return 32; } }
|
||||
|
||||
public static UInt32 Compute(byte[] buffer)
|
||||
{
|
||||
return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length);
|
||||
return Compute(DefaultSeed, buffer);
|
||||
}
|
||||
|
||||
public static UInt32 Compute(UInt32 seed, byte[] buffer)
|
||||
{
|
||||
return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length);
|
||||
return Compute(DefaultPolynomial, seed, buffer);
|
||||
}
|
||||
|
||||
public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
|
||||
|
|
@ -73,20 +76,20 @@ namespace TLSharp.Core.MTProto.Crypto
|
|||
return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
private static UInt32[] InitializeTable(UInt32 polynomial)
|
||||
static UInt32[] InitializeTable(UInt32 polynomial)
|
||||
{
|
||||
if (polynomial == DefaultPolynomial && defaultTable != null)
|
||||
return defaultTable;
|
||||
|
||||
UInt32[] createTable = new UInt32[256];
|
||||
for (int i = 0; i < 256; i++)
|
||||
var createTable = new UInt32[256];
|
||||
for (var i = 0; i < 256; i++)
|
||||
{
|
||||
UInt32 entry = (UInt32)i;
|
||||
for (int j = 0; j < 8; j++)
|
||||
var entry = (UInt32)i;
|
||||
for (var j = 0; j < 8; j++)
|
||||
if ((entry & 1) == 1)
|
||||
entry = (entry >> 1) ^ polynomial;
|
||||
else
|
||||
entry = entry >> 1;
|
||||
entry >>= 1;
|
||||
createTable[i] = entry;
|
||||
}
|
||||
|
||||
|
|
@ -96,25 +99,23 @@ namespace TLSharp.Core.MTProto.Crypto
|
|||
return createTable;
|
||||
}
|
||||
|
||||
private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size)
|
||||
static UInt32 CalculateHash(UInt32[] table, UInt32 seed, IList<byte> buffer, int start, int size)
|
||||
{
|
||||
UInt32 crc = seed;
|
||||
for (int i = start; i < size; i++)
|
||||
unchecked
|
||||
{
|
||||
crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
|
||||
}
|
||||
return crc;
|
||||
var hash = seed;
|
||||
for (var i = start; i < start + size; i++)
|
||||
hash = (hash >> 8) ^ table[buffer[i] ^ hash & 0xff];
|
||||
return hash;
|
||||
}
|
||||
|
||||
private byte[] UInt32ToBigEndianBytes(UInt32 x)
|
||||
static byte[] UInt32ToBigEndianBytes(UInt32 uint32)
|
||||
{
|
||||
return new byte[] {
|
||||
(byte)((x >> 24) & 0xff),
|
||||
(byte)((x >> 16) & 0xff),
|
||||
(byte)((x >> 8) & 0xff),
|
||||
(byte)(x & 0xff)
|
||||
};
|
||||
var result = BitConverter.GetBytes(uint32);
|
||||
|
||||
if (BitConverter.IsLittleEndian)
|
||||
Array.Reverse(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue