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

@ -1,8 +1,10 @@
using System;
using System.Text;
namespace TLSharp.Core.MTProto.Crypto {
public interface IDigest {
namespace TLSharp.Core.MTProto.Crypto
{
public interface IDigest
{
/**
* return the algorithm name
*
@ -55,13 +57,16 @@ namespace TLSharp.Core.MTProto.Crypto {
void Reset();
}
public class MD5 {
public class MD5
{
public static string GetMd5String(string data) {
public static string GetMd5String(string data)
{
return BitConverter.ToString(GetMd5Bytes(Encoding.UTF8.GetBytes(data))).Replace("-", "").ToLower();
}
public static byte[] GetMd5Bytes(byte[] data) {
public static byte[] GetMd5Bytes(byte[] data)
{
MD5Digest digest = new MD5Digest();
digest.BlockUpdate(data, 0, data.Length);
byte[] hash = new byte[16];
@ -72,15 +77,18 @@ namespace TLSharp.Core.MTProto.Crypto {
private MD5Digest digest = new MD5Digest();
public void Update(byte[] chunk) {
public void Update(byte[] chunk)
{
digest.BlockUpdate(chunk, 0, chunk.Length);
}
public void Update(byte[] chunk, int offset, int limit) {
public void Update(byte[] chunk, int offset, int limit)
{
digest.BlockUpdate(chunk, offset, limit);
}
public string FinalString() {
public string FinalString()
{
byte[] hash = new byte[16];
digest.DoFinal(hash, 0);
return BitConverter.ToString(hash).Replace("-", "").ToLower();
@ -88,7 +96,8 @@ namespace TLSharp.Core.MTProto.Crypto {
}
public abstract class GeneralDigest
: IDigest {
: IDigest
{
private const int BYTE_LENGTH = 64;
private readonly byte[] xBuf;
@ -96,11 +105,13 @@ namespace TLSharp.Core.MTProto.Crypto {
private long byteCount;
private int xBufOff;
internal GeneralDigest() {
internal GeneralDigest()
{
xBuf = new byte[4];
}
internal GeneralDigest(GeneralDigest t) {
internal GeneralDigest(GeneralDigest t)
{
xBuf = new byte[t.xBuf.Length];
Array.Copy(t.xBuf, 0, xBuf, 0, t.xBuf.Length);
@ -108,10 +119,12 @@ namespace TLSharp.Core.MTProto.Crypto {
byteCount = t.byteCount;
}
public void Update(byte input) {
public void Update(byte input)
{
xBuf[xBufOff++] = input;
if (xBufOff == xBuf.Length) {
if (xBufOff == xBuf.Length)
{
ProcessWord(xBuf, 0);
xBufOff = 0;
}
@ -122,11 +135,13 @@ namespace TLSharp.Core.MTProto.Crypto {
public void BlockUpdate(
byte[] input,
int inOff,
int length) {
int length)
{
//
// fill the current word
//
while ((xBufOff != 0) && (length > 0)) {
while ((xBufOff != 0) && (length > 0))
{
Update(input[inOff]);
inOff++;
length--;
@ -135,7 +150,8 @@ namespace TLSharp.Core.MTProto.Crypto {
//
// process whole words.
//
while (length > xBuf.Length) {
while (length > xBuf.Length)
{
ProcessWord(input, inOff);
inOff += xBuf.Length;
@ -146,7 +162,8 @@ namespace TLSharp.Core.MTProto.Crypto {
//
// load in the remainder.
//
while (length > 0) {
while (length > 0)
{
Update(input[inOff]);
inOff++;
@ -154,13 +171,15 @@ namespace TLSharp.Core.MTProto.Crypto {
}
}
public virtual void Reset() {
public virtual void Reset()
{
byteCount = 0;
xBufOff = 0;
Array.Clear(xBuf, 0, xBuf.Length);
}
public int GetByteLength() {
public int GetByteLength()
{
return BYTE_LENGTH;
}
@ -168,7 +187,8 @@ namespace TLSharp.Core.MTProto.Crypto {
public abstract int GetDigestSize();
public abstract int DoFinal(byte[] output, int outOff);
public void Finish() {
public void Finish()
{
long bitLength = (byteCount << 3);
//
@ -187,7 +207,8 @@ namespace TLSharp.Core.MTProto.Crypto {
}
public class MD5Digest
: GeneralDigest {
: GeneralDigest
{
private const int DigestLength = 16;
//
@ -225,7 +246,8 @@ namespace TLSharp.Core.MTProto.Crypto {
private int H1, H2, H3, H4; // IV's
private int xOff;
public MD5Digest() {
public MD5Digest()
{
Reset();
}
@ -235,7 +257,8 @@ namespace TLSharp.Core.MTProto.Crypto {
*/
public MD5Digest(MD5Digest t)
: base(t) {
: base(t)
{
H1 = t.H1;
H2 = t.H2;
H3 = t.H3;
@ -245,48 +268,56 @@ namespace TLSharp.Core.MTProto.Crypto {
xOff = t.xOff;
}
public override string AlgorithmName {
public override string AlgorithmName
{
get { return "MD5"; }
}
public override int GetDigestSize() {
public override int GetDigestSize()
{
return DigestLength;
}
internal override void ProcessWord(
byte[] input,
int inOff) {
int inOff)
{
X[xOff++] = (input[inOff] & 0xff) | ((input[inOff + 1] & 0xff) << 8)
| ((input[inOff + 2] & 0xff) << 16) | ((input[inOff + 3] & 0xff) << 24);
if (xOff == 16) {
if (xOff == 16)
{
ProcessBlock();
}
}
internal override void ProcessLength(
long bitLength) {
if (xOff > 14) {
long bitLength)
{
if (xOff > 14)
{
ProcessBlock();
}
X[14] = (int) (bitLength & 0xffffffff);
X[15] = (int) ((ulong) bitLength >> 32);
X[14] = (int)(bitLength & 0xffffffff);
X[15] = (int)((ulong)bitLength >> 32);
}
private void UnpackWord(
int word,
byte[] outBytes,
int outOff) {
outBytes[outOff] = (byte) word;
outBytes[outOff + 1] = (byte) ((uint) word >> 8);
outBytes[outOff + 2] = (byte) ((uint) word >> 16);
outBytes[outOff + 3] = (byte) ((uint) word >> 24);
int outOff)
{
outBytes[outOff] = (byte)word;
outBytes[outOff + 1] = (byte)((uint)word >> 8);
outBytes[outOff + 2] = (byte)((uint)word >> 16);
outBytes[outOff + 3] = (byte)((uint)word >> 24);
}
public override int DoFinal(
byte[] output,
int outOff) {
int outOff)
{
Finish();
UnpackWord(H1, output, outOff);
@ -303,17 +334,19 @@ namespace TLSharp.Core.MTProto.Crypto {
* reset the chaining variables to the IV values.
*/
public override void Reset() {
public override void Reset()
{
base.Reset();
H1 = unchecked(0x67452301);
H2 = unchecked((int) 0xefcdab89);
H3 = unchecked((int) 0x98badcfe);
H2 = unchecked((int)0xefcdab89);
H3 = unchecked((int)0x98badcfe);
H4 = unchecked(0x10325476);
xOff = 0;
for (int i = 0; i != X.Length; i++) {
for (int i = 0; i != X.Length; i++)
{
X[i] = 0;
}
}
@ -324,8 +357,9 @@ namespace TLSharp.Core.MTProto.Crypto {
private int RotateLeft(
int x,
int n) {
return (x << n) | (int) ((uint) x >> (32 - n));
int n)
{
return (x << n) | (int)((uint)x >> (32 - n));
}
/*
@ -335,32 +369,37 @@ namespace TLSharp.Core.MTProto.Crypto {
private int F(
int u,
int v,
int w) {
int w)
{
return (u & v) | (~u & w);
}
private int G(
int u,
int v,
int w) {
int w)
{
return (u & w) | (v & ~w);
}
private int H(
int u,
int v,
int w) {
int w)
{
return u ^ v ^ w;
}
private int K(
int u,
int v,
int w) {
int w)
{
return v ^ (u | ~w);
}
internal override void ProcessBlock() {
internal override void ProcessBlock()
{
int a = H1;
int b = H2;
int c = H3;
@ -369,82 +408,82 @@ namespace TLSharp.Core.MTProto.Crypto {
//
// Round 1 - F cycle, 16 times.
//
a = RotateLeft((a + F(b, c, d) + X[0] + unchecked((int) 0xd76aa478)), S11) + b;
d = RotateLeft((d + F(a, b, c) + X[1] + unchecked((int) 0xe8c7b756)), S12) + a;
a = RotateLeft((a + F(b, c, d) + X[0] + unchecked((int)0xd76aa478)), S11) + b;
d = RotateLeft((d + F(a, b, c) + X[1] + unchecked((int)0xe8c7b756)), S12) + a;
c = RotateLeft((c + F(d, a, b) + X[2] + unchecked(0x242070db)), S13) + d;
b = RotateLeft((b + F(c, d, a) + X[3] + unchecked((int) 0xc1bdceee)), S14) + c;
a = RotateLeft((a + F(b, c, d) + X[4] + unchecked((int) 0xf57c0faf)), S11) + b;
b = RotateLeft((b + F(c, d, a) + X[3] + unchecked((int)0xc1bdceee)), S14) + c;
a = RotateLeft((a + F(b, c, d) + X[4] + unchecked((int)0xf57c0faf)), S11) + b;
d = RotateLeft((d + F(a, b, c) + X[5] + unchecked(0x4787c62a)), S12) + a;
c = RotateLeft((c + F(d, a, b) + X[6] + unchecked((int) 0xa8304613)), S13) + d;
b = RotateLeft((b + F(c, d, a) + X[7] + unchecked((int) 0xfd469501)), S14) + c;
c = RotateLeft((c + F(d, a, b) + X[6] + unchecked((int)0xa8304613)), S13) + d;
b = RotateLeft((b + F(c, d, a) + X[7] + unchecked((int)0xfd469501)), S14) + c;
a = RotateLeft((a + F(b, c, d) + X[8] + unchecked(0x698098d8)), S11) + b;
d = RotateLeft((d + F(a, b, c) + X[9] + unchecked((int) 0x8b44f7af)), S12) + a;
c = RotateLeft((c + F(d, a, b) + X[10] + unchecked((int) 0xffff5bb1)), S13) + d;
b = RotateLeft((b + F(c, d, a) + X[11] + unchecked((int) 0x895cd7be)), S14) + c;
d = RotateLeft((d + F(a, b, c) + X[9] + unchecked((int)0x8b44f7af)), S12) + a;
c = RotateLeft((c + F(d, a, b) + X[10] + unchecked((int)0xffff5bb1)), S13) + d;
b = RotateLeft((b + F(c, d, a) + X[11] + unchecked((int)0x895cd7be)), S14) + c;
a = RotateLeft((a + F(b, c, d) + X[12] + unchecked(0x6b901122)), S11) + b;
d = RotateLeft((d + F(a, b, c) + X[13] + unchecked((int) 0xfd987193)), S12) + a;
c = RotateLeft((c + F(d, a, b) + X[14] + unchecked((int) 0xa679438e)), S13) + d;
d = RotateLeft((d + F(a, b, c) + X[13] + unchecked((int)0xfd987193)), S12) + a;
c = RotateLeft((c + F(d, a, b) + X[14] + unchecked((int)0xa679438e)), S13) + d;
b = RotateLeft((b + F(c, d, a) + X[15] + unchecked(0x49b40821)), S14) + c;
//
// Round 2 - G cycle, 16 times.
//
a = RotateLeft((a + G(b, c, d) + X[1] + unchecked((int) 0xf61e2562)), S21) + b;
d = RotateLeft((d + G(a, b, c) + X[6] + unchecked((int) 0xc040b340)), S22) + a;
a = RotateLeft((a + G(b, c, d) + X[1] + unchecked((int)0xf61e2562)), S21) + b;
d = RotateLeft((d + G(a, b, c) + X[6] + unchecked((int)0xc040b340)), S22) + a;
c = RotateLeft((c + G(d, a, b) + X[11] + unchecked(0x265e5a51)), S23) + d;
b = RotateLeft((b + G(c, d, a) + X[0] + unchecked((int) 0xe9b6c7aa)), S24) + c;
a = RotateLeft((a + G(b, c, d) + X[5] + unchecked((int) 0xd62f105d)), S21) + b;
b = RotateLeft((b + G(c, d, a) + X[0] + unchecked((int)0xe9b6c7aa)), S24) + c;
a = RotateLeft((a + G(b, c, d) + X[5] + unchecked((int)0xd62f105d)), S21) + b;
d = RotateLeft((d + G(a, b, c) + X[10] + unchecked(0x02441453)), S22) + a;
c = RotateLeft((c + G(d, a, b) + X[15] + unchecked((int) 0xd8a1e681)), S23) + d;
b = RotateLeft((b + G(c, d, a) + X[4] + unchecked((int) 0xe7d3fbc8)), S24) + c;
c = RotateLeft((c + G(d, a, b) + X[15] + unchecked((int)0xd8a1e681)), S23) + d;
b = RotateLeft((b + G(c, d, a) + X[4] + unchecked((int)0xe7d3fbc8)), S24) + c;
a = RotateLeft((a + G(b, c, d) + X[9] + unchecked(0x21e1cde6)), S21) + b;
d = RotateLeft((d + G(a, b, c) + X[14] + unchecked((int) 0xc33707d6)), S22) + a;
c = RotateLeft((c + G(d, a, b) + X[3] + unchecked((int) 0xf4d50d87)), S23) + d;
d = RotateLeft((d + G(a, b, c) + X[14] + unchecked((int)0xc33707d6)), S22) + a;
c = RotateLeft((c + G(d, a, b) + X[3] + unchecked((int)0xf4d50d87)), S23) + d;
b = RotateLeft((b + G(c, d, a) + X[8] + unchecked(0x455a14ed)), S24) + c;
a = RotateLeft((a + G(b, c, d) + X[13] + unchecked((int) 0xa9e3e905)), S21) + b;
d = RotateLeft((d + G(a, b, c) + X[2] + unchecked((int) 0xfcefa3f8)), S22) + a;
a = RotateLeft((a + G(b, c, d) + X[13] + unchecked((int)0xa9e3e905)), S21) + b;
d = RotateLeft((d + G(a, b, c) + X[2] + unchecked((int)0xfcefa3f8)), S22) + a;
c = RotateLeft((c + G(d, a, b) + X[7] + unchecked(0x676f02d9)), S23) + d;
b = RotateLeft((b + G(c, d, a) + X[12] + unchecked((int) 0x8d2a4c8a)), S24) + c;
b = RotateLeft((b + G(c, d, a) + X[12] + unchecked((int)0x8d2a4c8a)), S24) + c;
//
// Round 3 - H cycle, 16 times.
//
a = RotateLeft((a + H(b, c, d) + X[5] + unchecked((int) 0xfffa3942)), S31) + b;
d = RotateLeft((d + H(a, b, c) + X[8] + unchecked((int) 0x8771f681)), S32) + a;
a = RotateLeft((a + H(b, c, d) + X[5] + unchecked((int)0xfffa3942)), S31) + b;
d = RotateLeft((d + H(a, b, c) + X[8] + unchecked((int)0x8771f681)), S32) + a;
c = RotateLeft((c + H(d, a, b) + X[11] + unchecked(0x6d9d6122)), S33) + d;
b = RotateLeft((b + H(c, d, a) + X[14] + unchecked((int) 0xfde5380c)), S34) + c;
a = RotateLeft((a + H(b, c, d) + X[1] + unchecked((int) 0xa4beea44)), S31) + b;
b = RotateLeft((b + H(c, d, a) + X[14] + unchecked((int)0xfde5380c)), S34) + c;
a = RotateLeft((a + H(b, c, d) + X[1] + unchecked((int)0xa4beea44)), S31) + b;
d = RotateLeft((d + H(a, b, c) + X[4] + unchecked(0x4bdecfa9)), S32) + a;
c = RotateLeft((c + H(d, a, b) + X[7] + unchecked((int) 0xf6bb4b60)), S33) + d;
b = RotateLeft((b + H(c, d, a) + X[10] + unchecked((int) 0xbebfbc70)), S34) + c;
c = RotateLeft((c + H(d, a, b) + X[7] + unchecked((int)0xf6bb4b60)), S33) + d;
b = RotateLeft((b + H(c, d, a) + X[10] + unchecked((int)0xbebfbc70)), S34) + c;
a = RotateLeft((a + H(b, c, d) + X[13] + unchecked(0x289b7ec6)), S31) + b;
d = RotateLeft((d + H(a, b, c) + X[0] + unchecked((int) 0xeaa127fa)), S32) + a;
c = RotateLeft((c + H(d, a, b) + X[3] + unchecked((int) 0xd4ef3085)), S33) + d;
d = RotateLeft((d + H(a, b, c) + X[0] + unchecked((int)0xeaa127fa)), S32) + a;
c = RotateLeft((c + H(d, a, b) + X[3] + unchecked((int)0xd4ef3085)), S33) + d;
b = RotateLeft((b + H(c, d, a) + X[6] + unchecked(0x04881d05)), S34) + c;
a = RotateLeft((a + H(b, c, d) + X[9] + unchecked((int) 0xd9d4d039)), S31) + b;
d = RotateLeft((d + H(a, b, c) + X[12] + unchecked((int) 0xe6db99e5)), S32) + a;
a = RotateLeft((a + H(b, c, d) + X[9] + unchecked((int)0xd9d4d039)), S31) + b;
d = RotateLeft((d + H(a, b, c) + X[12] + unchecked((int)0xe6db99e5)), S32) + a;
c = RotateLeft((c + H(d, a, b) + X[15] + unchecked(0x1fa27cf8)), S33) + d;
b = RotateLeft((b + H(c, d, a) + X[2] + unchecked((int) 0xc4ac5665)), S34) + c;
b = RotateLeft((b + H(c, d, a) + X[2] + unchecked((int)0xc4ac5665)), S34) + c;
//
// Round 4 - K cycle, 16 times.
//
a = RotateLeft((a + K(b, c, d) + X[0] + unchecked((int) 0xf4292244)), S41) + b;
a = RotateLeft((a + K(b, c, d) + X[0] + unchecked((int)0xf4292244)), S41) + b;
d = RotateLeft((d + K(a, b, c) + X[7] + unchecked(0x432aff97)), S42) + a;
c = RotateLeft((c + K(d, a, b) + X[14] + unchecked((int) 0xab9423a7)), S43) + d;
b = RotateLeft((b + K(c, d, a) + X[5] + unchecked((int) 0xfc93a039)), S44) + c;
c = RotateLeft((c + K(d, a, b) + X[14] + unchecked((int)0xab9423a7)), S43) + d;
b = RotateLeft((b + K(c, d, a) + X[5] + unchecked((int)0xfc93a039)), S44) + c;
a = RotateLeft((a + K(b, c, d) + X[12] + unchecked(0x655b59c3)), S41) + b;
d = RotateLeft((d + K(a, b, c) + X[3] + unchecked((int) 0x8f0ccc92)), S42) + a;
c = RotateLeft((c + K(d, a, b) + X[10] + unchecked((int) 0xffeff47d)), S43) + d;
b = RotateLeft((b + K(c, d, a) + X[1] + unchecked((int) 0x85845dd1)), S44) + c;
d = RotateLeft((d + K(a, b, c) + X[3] + unchecked((int)0x8f0ccc92)), S42) + a;
c = RotateLeft((c + K(d, a, b) + X[10] + unchecked((int)0xffeff47d)), S43) + d;
b = RotateLeft((b + K(c, d, a) + X[1] + unchecked((int)0x85845dd1)), S44) + c;
a = RotateLeft((a + K(b, c, d) + X[8] + unchecked(0x6fa87e4f)), S41) + b;
d = RotateLeft((d + K(a, b, c) + X[15] + unchecked((int) 0xfe2ce6e0)), S42) + a;
c = RotateLeft((c + K(d, a, b) + X[6] + unchecked((int) 0xa3014314)), S43) + d;
d = RotateLeft((d + K(a, b, c) + X[15] + unchecked((int)0xfe2ce6e0)), S42) + a;
c = RotateLeft((c + K(d, a, b) + X[6] + unchecked((int)0xa3014314)), S43) + d;
b = RotateLeft((b + K(c, d, a) + X[13] + unchecked(0x4e0811a1)), S44) + c;
a = RotateLeft((a + K(b, c, d) + X[4] + unchecked((int) 0xf7537e82)), S41) + b;
d = RotateLeft((d + K(a, b, c) + X[11] + unchecked((int) 0xbd3af235)), S42) + a;
a = RotateLeft((a + K(b, c, d) + X[4] + unchecked((int)0xf7537e82)), S41) + b;
d = RotateLeft((d + K(a, b, c) + X[11] + unchecked((int)0xbd3af235)), S42) + a;
c = RotateLeft((c + K(d, a, b) + X[2] + unchecked(0x2ad7d2bb)), S43) + d;
b = RotateLeft((b + K(c, d, a) + X[9] + unchecked((int) 0xeb86d391)), S44) + c;
b = RotateLeft((b + K(c, d, a) + X[9] + unchecked((int)0xeb86d391)), S44) + c;
H1 += a;
H2 += b;
@ -455,7 +494,8 @@ namespace TLSharp.Core.MTProto.Crypto {
// reset the offset and clean out the word buffer.
//
xOff = 0;
for (int i = 0; i != X.Length; i++) {
for (int i = 0; i != X.Length; i++)
{
X[i] = 0;
}
}