mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-05 23:52:00 +01:00
Remove unnecessary dependencies
This commit is contained in:
parent
709e5be7cc
commit
6d2c77f5ef
|
|
@ -15,7 +15,6 @@ It's a perfect fit for any developer who would like to send data directly to Tel
|
|||
# Table of contents
|
||||
|
||||
- [How do I add this to my project?](#how-do-i-add-this-to-my-project)
|
||||
- [Dependencies](#dependencies)
|
||||
- [Starter Guide](#starter-guide)
|
||||
- [Quick configuration](#quick-configuration)
|
||||
- [First requests](#first-requests)
|
||||
|
|
@ -41,11 +40,6 @@ or build from source
|
|||
1. Compile source with VS2015 or MonoDevelop
|
||||
1. Add reference to ```TLSharp.Core.dll``` to your awesome project.
|
||||
|
||||
# Dependencies
|
||||
|
||||
TLSharp has a few dependenices, most of functionality implemented from scratch.
|
||||
All dependencies listed in [package.conf file](https://github.com/sochix/TLSharp/blob/master/TLSharp.Core/packages.config).
|
||||
|
||||
# Starter Guide
|
||||
|
||||
## Quick Configuration
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Ionic.Zlib;
|
||||
using TeleSharp.TL;
|
||||
using TLSharp.Core.Exceptions;
|
||||
using TLSharp.Core.MTProto;
|
||||
|
|
@ -251,11 +251,20 @@ namespace TLSharp.Core.Network
|
|||
token.ThrowIfCancellationRequested();
|
||||
|
||||
uint code = messageReader.ReadUInt32();
|
||||
byte[] packedData = GZipStream.UncompressBuffer(Serializers.Bytes.Read(messageReader));
|
||||
using (MemoryStream packedStream = new MemoryStream(packedData, false))
|
||||
using (BinaryReader compressedReader = new BinaryReader(packedStream))
|
||||
|
||||
byte[] packedData = Serializers.Bytes.Read(messageReader);
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
processMessage(messageId, sequence, compressedReader, request, token);
|
||||
using (var packedStream = new MemoryStream(packedData, false))
|
||||
using (var zipStream = new GZipStream(packedStream, CompressionMode.Decompress))
|
||||
{
|
||||
zipStream.CopyTo(ms);
|
||||
ms.Position = 0;
|
||||
}
|
||||
using (BinaryReader compressedReader = new BinaryReader(ms))
|
||||
{
|
||||
processMessage(messageId, sequence, compressedReader, request, token);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
@ -337,27 +346,20 @@ namespace TLSharp.Core.Network
|
|||
}
|
||||
else if (innerCode == 0x3072cfa1)
|
||||
{
|
||||
try
|
||||
// gzip_packed
|
||||
byte[] packedData = Serializers.Bytes.Read(messageReader);
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
// gzip_packed
|
||||
byte[] packedData = Serializers.Bytes.Read(messageReader);
|
||||
using (var ms = new MemoryStream())
|
||||
using (var packedStream = new MemoryStream(packedData, false))
|
||||
using (var zipStream = new GZipStream(packedStream, CompressionMode.Decompress))
|
||||
{
|
||||
using (var packedStream = new MemoryStream(packedData, false))
|
||||
using (var zipStream = new GZipStream(packedStream, CompressionMode.Decompress))
|
||||
{
|
||||
zipStream.CopyTo(ms);
|
||||
ms.Position = 0;
|
||||
}
|
||||
using (var compressedReader = new BinaryReader(ms))
|
||||
{
|
||||
request.DeserializeResponse(compressedReader);
|
||||
}
|
||||
zipStream.CopyTo(ms);
|
||||
ms.Position = 0;
|
||||
}
|
||||
using (var compressedReader = new BinaryReader(ms))
|
||||
{
|
||||
request.DeserializeResponse(compressedReader);
|
||||
}
|
||||
}
|
||||
catch (ZlibException ex)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using Ionic.Crc;
|
||||
using System.Linq;
|
||||
using TLSharp.Core.MTProto.Crypto;
|
||||
|
||||
namespace TLSharp.Core.Network
|
||||
{
|
||||
|
|
@ -35,9 +36,9 @@ namespace TLSharp.Core.Network
|
|||
binaryWriter.Write(Body.Length + 12);
|
||||
binaryWriter.Write(SequneceNumber);
|
||||
binaryWriter.Write(Body);
|
||||
var crc32 = new CRC32();
|
||||
crc32.SlurpBlock(memoryStream.GetBuffer(), 0, 8 + Body.Length);
|
||||
binaryWriter.Write(crc32.Crc32Result);
|
||||
var crc32 = new Crc32();
|
||||
var checksum = crc32.ComputeHash(memoryStream.GetBuffer(), 0, 8 + Body.Length).Reverse().ToArray();
|
||||
binaryWriter.Write(checksum);
|
||||
|
||||
var transportPacket = memoryStream.ToArray();
|
||||
|
||||
|
|
@ -67,13 +68,12 @@ namespace TLSharp.Core.Network
|
|||
|
||||
var seq = binaryReader.ReadInt32();
|
||||
byte[] packet = binaryReader.ReadBytes(packetLength - 12);
|
||||
var checksum = (int)binaryReader.ReadInt32();
|
||||
var checksum = binaryReader.ReadBytes(4);
|
||||
|
||||
var crc32 = new CRC32();
|
||||
crc32.SlurpBlock(body, 0, packetLength - 4);
|
||||
var validChecksum = crc32.Crc32Result;
|
||||
var crc32 = new Crc32();
|
||||
var computedChecksum = crc32.ComputeHash(body, 0, packetLength - 4).Reverse();
|
||||
|
||||
if (checksum != validChecksum)
|
||||
if (!checksum.SequenceEqual(computedChecksum))
|
||||
{
|
||||
throw new InvalidOperationException("invalid checksum! skip");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using TLSharp.Core.MTProto.Crypto;
|
||||
|
||||
namespace TLSharp.Core.Network
|
||||
{
|
||||
|
|
@ -73,18 +75,16 @@ namespace TLSharp.Core.Network
|
|||
var crcBytes = new byte[4];
|
||||
if (await stream.ReadAsync(crcBytes, 0, 4, token).ConfigureAwait(false) != 4)
|
||||
throw new InvalidOperationException("Couldn't read the crc");
|
||||
int checksum = BitConverter.ToInt32(crcBytes, 0);
|
||||
|
||||
byte[] rv = new byte[packetLengthBytes.Length + seqBytes.Length + body.Length];
|
||||
|
||||
Buffer.BlockCopy(packetLengthBytes, 0, rv, 0, packetLengthBytes.Length);
|
||||
Buffer.BlockCopy(seqBytes, 0, rv, packetLengthBytes.Length, seqBytes.Length);
|
||||
Buffer.BlockCopy(body, 0, rv, packetLengthBytes.Length + seqBytes.Length, body.Length);
|
||||
var crc32 = new Ionic.Crc.CRC32();
|
||||
crc32.SlurpBlock(rv, 0, rv.Length);
|
||||
var validChecksum = crc32.Crc32Result;
|
||||
var crc32 = new Crc32();
|
||||
var computedChecksum = crc32.ComputeHash(rv).Reverse();
|
||||
|
||||
if (checksum != validChecksum)
|
||||
if (!crcBytes.SequenceEqual(computedChecksum))
|
||||
{
|
||||
throw new InvalidOperationException("invalid checksum! skip");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,10 +31,6 @@
|
|||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Ionic.ZLib, Version=2.0.0.14, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MarkerMetro.Unity.Ionic.Zlib.2.0.0.14\lib\net35\Ionic.ZLib.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
|
|
|
|||
|
|
@ -20,8 +20,6 @@ It's a perfect fit for any developer who would like to send data directly to Tel
|
|||
<copyright>Copyright 2016</copyright>
|
||||
</metadata>
|
||||
<files>
|
||||
<file src="bin\Debug\BigMath.dll" target="lib\net45\BigMath.dll" />
|
||||
<file src="bin\Debug\Ionic.ZLib.dll" target="lib\net45\Ionic.ZLib.dll" />
|
||||
<file src="bin\Debug\TeleSharp.TL.dll" target="lib\net45\TeleSharp.TL.dll" />
|
||||
</files>
|
||||
</package>
|
||||
|
|
@ -1,5 +1,3 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="DotNetZip" version="1.11.0" targetFramework="net451" />
|
||||
<package id="MarkerMetro.Unity.Ionic.Zlib" version="2.0.0.14" targetFramework="net452" />
|
||||
</packages>
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
using BigMath;
|
||||
using BigMath.Utils;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
|
@ -159,26 +158,4 @@ namespace TeleSharp.TL
|
|||
writer.Write(src);
|
||||
}
|
||||
}
|
||||
public class Int128Util
|
||||
{
|
||||
public static Int128 Deserialize(BinaryReader reader)
|
||||
{
|
||||
return reader.ReadBytes(16).ToInt128(0, true);
|
||||
}
|
||||
public static void Serialize(Int128 src, BinaryWriter writer)
|
||||
{
|
||||
writer.Write(src.ToBytes(true));
|
||||
}
|
||||
}
|
||||
public class Int256Util
|
||||
{
|
||||
public static Int256 Deserialize(BinaryReader reader)
|
||||
{
|
||||
return reader.ReadBytes(32).ToInt256(0, true);
|
||||
}
|
||||
public static void Serialize(Int256 src, BinaryWriter writer)
|
||||
{
|
||||
writer.Write(src.ToBytes(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,10 +31,6 @@
|
|||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="BigMath, Version=0.5.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\BigMath.0.5.0\lib\portable-net45+netcore45+wpa81+wp8+MonoAndroid1+MonoTouch1\BigMath.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="BigMath" version="0.5.0" targetFramework="net452" />
|
||||
</packages>
|
||||
Loading…
Reference in a new issue