2015-09-28 04:01:17 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
|
|
2016-04-18 12:50:57 +02:00
|
|
|
|
namespace TLSharp.Core.MTProto.Crypto
|
|
|
|
|
|
{
|
|
|
|
|
|
public class AuthKey
|
|
|
|
|
|
{
|
2017-04-13 08:38:01 +02:00
|
|
|
|
private readonly ulong auxHash;
|
|
|
|
|
|
|
2016-04-18 12:50:57 +02:00
|
|
|
|
public AuthKey(BigInteger gab)
|
|
|
|
|
|
{
|
2017-04-13 08:38:01 +02:00
|
|
|
|
Data = gab.ToByteArrayUnsigned();
|
2016-04-18 12:50:57 +02:00
|
|
|
|
using (SHA1 hash = new SHA1Managed())
|
|
|
|
|
|
{
|
2017-04-13 08:38:01 +02:00
|
|
|
|
using (var hashStream = new MemoryStream(hash.ComputeHash(Data), false))
|
2016-04-18 12:50:57 +02:00
|
|
|
|
{
|
2017-04-13 08:38:01 +02:00
|
|
|
|
using (var hashReader = new BinaryReader(hashStream))
|
2016-04-18 12:50:57 +02:00
|
|
|
|
{
|
2015-09-28 04:01:17 +02:00
|
|
|
|
auxHash = hashReader.ReadUInt64();
|
|
|
|
|
|
hashReader.ReadBytes(4);
|
2017-04-13 08:38:01 +02:00
|
|
|
|
Id = hashReader.ReadUInt64();
|
2015-09-28 04:01:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-18 12:50:57 +02:00
|
|
|
|
public AuthKey(byte[] data)
|
|
|
|
|
|
{
|
2017-04-13 08:38:01 +02:00
|
|
|
|
Data = data;
|
2016-04-18 12:50:57 +02:00
|
|
|
|
using (SHA1 hash = new SHA1Managed())
|
|
|
|
|
|
{
|
2017-04-13 08:38:01 +02:00
|
|
|
|
using (var hashStream = new MemoryStream(hash.ComputeHash(Data), false))
|
2016-04-18 12:50:57 +02:00
|
|
|
|
{
|
2017-04-13 08:38:01 +02:00
|
|
|
|
using (var hashReader = new BinaryReader(hashStream))
|
2016-04-18 12:50:57 +02:00
|
|
|
|
{
|
2015-09-28 04:01:17 +02:00
|
|
|
|
auxHash = hashReader.ReadUInt64();
|
|
|
|
|
|
hashReader.ReadBytes(4);
|
2017-04-13 08:38:01 +02:00
|
|
|
|
Id = hashReader.ReadUInt64();
|
2015-09-28 04:01:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-13 08:38:01 +02:00
|
|
|
|
public byte[] Data { get; }
|
|
|
|
|
|
|
|
|
|
|
|
public ulong Id { get; }
|
|
|
|
|
|
|
2016-04-18 12:50:57 +02:00
|
|
|
|
public byte[] CalcNewNonceHash(byte[] newNonce, int number)
|
|
|
|
|
|
{
|
2017-04-13 08:38:01 +02:00
|
|
|
|
using (var buffer = new MemoryStream(100))
|
2016-04-18 12:50:57 +02:00
|
|
|
|
{
|
2017-04-13 08:38:01 +02:00
|
|
|
|
using (var bufferWriter = new BinaryWriter(buffer))
|
2016-04-18 12:50:57 +02:00
|
|
|
|
{
|
2015-09-28 04:01:17 +02:00
|
|
|
|
bufferWriter.Write(newNonce);
|
2017-04-13 08:38:01 +02:00
|
|
|
|
bufferWriter.Write((byte) number);
|
2015-09-28 04:01:17 +02:00
|
|
|
|
bufferWriter.Write(auxHash);
|
2016-04-18 12:50:57 +02:00
|
|
|
|
using (SHA1 sha1 = new SHA1Managed())
|
|
|
|
|
|
{
|
2017-04-13 08:38:01 +02:00
|
|
|
|
var hash = sha1.ComputeHash(buffer.GetBuffer(), 0, (int) buffer.Position);
|
|
|
|
|
|
var newNonceHash = new byte[16];
|
2015-09-28 04:01:17 +02:00
|
|
|
|
Array.Copy(hash, 4, newNonceHash, 0, 16);
|
|
|
|
|
|
return newNonceHash;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-18 12:50:57 +02:00
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
2017-04-13 08:38:01 +02:00
|
|
|
|
return string.Format("(Key: {0}, KeyId: {1}, AuxHash: {2})", Data, Id, auxHash);
|
2015-09-28 04:01:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-04-13 08:38:01 +02:00
|
|
|
|
}
|