diff --git a/src/Client.cs b/src/Client.cs index f3bc232..492e81a 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -1175,9 +1175,8 @@ namespace WTelegram lock (_accessHashes) _accessHashes.GetOrCreate(typeof(T))[id] = access_hash; } - internal void MonitorField(FieldInfo fieldInfo, object obj, object access_hash) + internal void CollectField(FieldInfo fieldInfo, object obj, object access_hash) { - if (!CollectAccessHash) return; if (fieldInfo.Name != "access_hash") return; if (access_hash is not long accessHash) return; var type = fieldInfo.ReflectedType; diff --git a/src/Encryption.cs b/src/Encryption.cs index 448eff6..970143f 100644 --- a/src/Encryption.cs +++ b/src/Encryption.cs @@ -213,9 +213,6 @@ namespace WTelegram throw new ApplicationException("g^a or g^b is not between 2^{2048-64} and dh_prime - 2^{2048-64}"); } - [TLDef(0x7A19CB76)] //RSA_public_key#7a19cb76 n:bytes e:bytes = RSAPublicKey - public class RSAPublicKey : IObject { public byte[] n, e; } - public static void LoadPublicKey(string pem) { using var rsa = RSA.Create(); @@ -224,7 +221,10 @@ namespace WTelegram var rsaParam = rsa.ExportParameters(false); if (rsaParam.Modulus[0] == 0) rsaParam.Modulus = rsaParam.Modulus[1..]; var publicKey = new RSAPublicKey { n = rsaParam.Modulus, e = rsaParam.Exponent }; - var bareData = publicKey.Serialize(); // bare serialization + using var memStream = new MemoryStream(280); + using (var writer = new BinaryWriter(memStream)) + writer.WriteTLObject(publicKey); + var bareData = memStream.ToArray(); var fingerprint = BinaryPrimitives.ReadInt64LittleEndian(sha1.ComputeHash(bareData, 4, bareData.Length - 4).AsSpan(12)); // 64 lower-order bits of SHA1 PublicKeys[fingerprint] = publicKey; Helpers.Log(1, $"Loaded a public key with fingerprint {fingerprint:X}"); diff --git a/src/TL.cs b/src/TL.cs index 1d99702..8673811 100644 --- a/src/TL.cs +++ b/src/TL.cs @@ -15,21 +15,6 @@ namespace TL public static class Serialization { - internal static byte[] Serialize(this IObject msg) - { - using var memStream = new MemoryStream(1024); - using (var writer = new BinaryWriter(memStream)) - WriteTLObject(writer, msg); - return memStream.ToArray(); - } - - internal static T Deserialize(byte[] bytes) where T : IObject - { - using var memStream = new MemoryStream(bytes); - using var reader = new BinaryReader(memStream, null); - return (T)reader.ReadTLObject(); - } - internal static void WriteTLObject(this BinaryWriter writer, T obj) where T : IObject { if (obj == null) { writer.WriteTLNull(typeof(T)); return; } @@ -71,7 +56,7 @@ namespace TL object value = reader.ReadTLValue(field.FieldType); field.SetValue(obj, value); if (field.FieldType.IsEnum && field.Name == "flags") flags = (uint)value; - reader.Client?.MonitorField(field, obj, value); + if (reader.Client.CollectAccessHash) reader.Client.CollectField(field, obj, value); } return (IObject)obj; } @@ -371,6 +356,13 @@ namespace TL // Below TL types are commented "parsed manually" from https://github.com/telegramdesktop/tdesktop/blob/dev/Telegram/Resources/tl/mtproto.tl + [TLDef(0x7A19CB76)] //RSA_public_key#7a19cb76 n:bytes e:bytes = RSAPublicKey + public class RSAPublicKey : IObject + { + public byte[] n; + public byte[] e; + } + [TLDef(0xF35C6D01)] //rpc_result#f35c6d01 req_msg_id:long result:Object = RpcResult public class RpcResult : IObject {