formatting code (ctrl + k + d)

This commit is contained in:
ahmadali shafiee 2016-04-18 15:20:57 +04:30
parent d330f9614b
commit 504b63a6d7
38 changed files with 16805 additions and 16280 deletions

View file

@ -6,8 +6,10 @@ using System.Text;
using System.Threading.Tasks;
using Ionic.Crc;
namespace TLSharp.Core.MTProto.Crypto {
public class Crc32 : HashAlgorithm {
namespace TLSharp.Core.MTProto.Crypto
{
public class Crc32 : HashAlgorithm
{
public const UInt32 DefaultPolynomial = 0xedb88320u;
public const UInt32 DefaultSeed = 0xffffffffu;
@ -16,23 +18,27 @@ namespace TLSharp.Core.MTProto.Crypto {
private UInt32[] table;
private static UInt32[] defaultTable;
public Crc32() {
public Crc32()
{
table = InitializeTable(DefaultPolynomial);
seed = DefaultSeed;
hash = seed;
}
public Crc32(UInt32 polynomial, UInt32 seed) {
public Crc32(UInt32 polynomial, UInt32 seed)
{
table = InitializeTable(polynomial);
this.seed = seed;
hash = seed;
}
public override void Initialize() {
public override void Initialize()
{
hash = seed;
}
protected override void HashCore(byte[] buffer, int start, int length) {
protected override void HashCore(byte[] buffer, int start, int length)
{
hash = CalculateHash(table, hash, buffer, start, length);
}
@ -40,34 +46,41 @@ namespace TLSharp.Core.MTProto.Crypto {
/// Возвращает хеш в BigEndian
/// </summary>
/// <returns></returns>
protected override byte[] HashFinal() {
protected override byte[] HashFinal()
{
byte[] hashBuffer = UInt32ToBigEndianBytes(~hash);
this.HashValue = hashBuffer;
return hashBuffer;
}
public override int HashSize {
public override int HashSize
{
get { return 32; }
}
public static UInt32 Compute(byte[] buffer) {
public static UInt32 Compute(byte[] buffer)
{
return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length);
}
public static UInt32 Compute(UInt32 seed, byte[] buffer) {
public static UInt32 Compute(UInt32 seed, byte[] buffer)
{
return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length);
}
public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer) {
public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
{
return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
}
private static UInt32[] InitializeTable(UInt32 polynomial) {
private static UInt32[] InitializeTable(UInt32 polynomial)
{
if (polynomial == DefaultPolynomial && defaultTable != null)
return defaultTable;
UInt32[] createTable = new UInt32[256];
for (int i = 0; i < 256; i++) {
for (int i = 0; i < 256; i++)
{
UInt32 entry = (UInt32)i;
for (int j = 0; j < 8; j++)
if ((entry & 1) == 1)
@ -83,22 +96,25 @@ namespace TLSharp.Core.MTProto.Crypto {
return createTable;
}
private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size) {
private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size)
{
UInt32 crc = seed;
for (int i = start; i < size; i++)
unchecked {
unchecked
{
crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
}
return crc;
}
private byte[] UInt32ToBigEndianBytes(UInt32 x) {
private byte[] UInt32ToBigEndianBytes(UInt32 x)
{
return new byte[] {
(byte)((x >> 24) & 0xff),
(byte)((x >> 16) & 0xff),
(byte)((x >> 8) & 0xff),
(byte)(x & 0xff)
};
(byte)((x >> 24) & 0xff),
(byte)((x >> 16) & 0xff),
(byte)((x >> 8) & 0xff),
(byte)(x & 0xff)
};
}
}
}