From f5b108dc9b896170f7eb1ca871b91c58651568d3 Mon Sep 17 00:00:00 2001 From: Wizou Date: Sun, 17 Oct 2021 23:35:14 +0200 Subject: [PATCH] Fix compatibility issues with .NET Fw 4.7 --- Examples/Program_CollectAccessHash.cs | 4 ++-- src/Client.cs | 2 +- src/Encryption.cs | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Examples/Program_CollectAccessHash.cs b/Examples/Program_CollectAccessHash.cs index 19c1e6f..f67465d 100644 --- a/Examples/Program_CollectAccessHash.cs +++ b/Examples/Program_CollectAccessHash.cs @@ -27,8 +27,8 @@ namespace WTelegramClientTest Console.WriteLine("Loading previously saved access hashes from disk..."); using (var stateStream = File.OpenRead(StateFilename)) savedState = await JsonSerializer.DeserializeAsync(stateStream); - foreach ((long id, long access_hash) in savedState.Channels) client.SetAccessHashFor(id, access_hash); - foreach ((long id, long access_hash) in savedState.Users) client.SetAccessHashFor(id, access_hash); + foreach (var id_hash in savedState.Channels) client.SetAccessHashFor(id_hash.Key, id_hash.Value); + foreach (var id_hash in savedState.Users) client.SetAccessHashFor(id_hash.Key, id_hash.Value); } Console.WriteLine("Connecting to Telegram..."); diff --git a/src/Client.cs b/src/Client.cs index 0c4af1a..13a0e54 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -472,11 +472,11 @@ namespace WTelegram throw new ApplicationException($"Mismatch between MsgKey & decrypted SHA1"); #else if (decrypted_data.Length - 32 - length is < 12 or > 1024) throw new ApplicationException($"Unexpected decrypted message_data_length {length} / {decrypted_data.Length - 32}"); - _sha256Recv.Initialize(); _sha256Recv.TransformBlock(_dcSession.AuthKey, 96, 32, null, 0); _sha256Recv.TransformFinalBlock(decrypted_data, 0, decrypted_data.Length); if (!data.AsSpan(8, 16).SequenceEqual(_sha256Recv.Hash.AsSpan(8, 16))) throw new ApplicationException($"Mismatch between MsgKey & decrypted SHA1"); + _sha256Recv.Initialize(); #endif var ctorNb = reader.ReadUInt32(); if (ctorNb == Layer.MsgContainerCtor) diff --git a/src/Encryption.cs b/src/Encryption.cs index f3ce552..dc0ee0a 100644 --- a/src/Encryption.cs +++ b/src/Encryption.cs @@ -171,7 +171,6 @@ namespace WTelegram (byte[] key, byte[] iv) ConstructTmpAESKeyIV(Int128 server_nonce, Int256 new_nonce) { byte[] tmp_aes_key = new byte[32], tmp_aes_iv = new byte[32]; - sha1.Initialize(); sha1.TransformBlock(new_nonce, 0, 32, null, 0); sha1.TransformFinalBlock(server_nonce, 0, 16); sha1.Hash.CopyTo(tmp_aes_key, 0); // tmp_aes_key := SHA1(new_nonce + server_nonce) @@ -185,6 +184,7 @@ namespace WTelegram sha1.TransformFinalBlock(new_nonce, 0, 32); sha1.Hash.CopyTo(tmp_aes_iv, 8); // + SHA(new_nonce + new_nonce) Array.Copy(new_nonce, 0, tmp_aes_iv, 28, 4); // + new_nonce[0:4] + sha1.Initialize(); return (tmp_aes_key, tmp_aes_iv); } } @@ -231,6 +231,7 @@ namespace WTelegram using var sha1 = SHA1.Create(); rsa.ImportFromPem(pem); 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 var fingerprint = BinaryPrimitives.ReadInt64LittleEndian(sha1.ComputeHash(bareData, 4, bareData.Length - 4).AsSpan(12)); // 64 lower-order bits of SHA1 @@ -278,7 +279,6 @@ j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB // first, construct AES key & IV byte[] aes_key = new byte[32], aes_iv = new byte[32]; int x = encrypt ? 0 : 8; - sha1.Initialize(); sha1.TransformBlock(msgKey, msgKeyOffset, 16, null, 0); // msgKey sha1.TransformFinalBlock(authKey, x, 32); // authKey[x:32] var sha1_a = sha1.Hash; @@ -295,6 +295,7 @@ j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB sha1.TransformBlock(msgKey, msgKeyOffset, 16, null, 0); // msgKey sha1.TransformFinalBlock(authKey, 96 + x, 32); // authKey[96+x:32] var sha1_d = sha1.Hash; + sha1.Initialize(); Array.Copy(sha1_a, 0, aes_key, 0, 8); Array.Copy(sha1_b, 8, aes_key, 8, 12); Array.Copy(sha1_c, 4, aes_key, 20, 12); @@ -310,7 +311,6 @@ j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB // first, construct AES key & IV byte[] aes_key = new byte[32], aes_iv = new byte[32]; int x = encrypt ? 0 : 8; - sha256.Initialize(); sha256.TransformBlock(msgKey, msgKeyOffset, 16, null, 0); // msgKey sha256.TransformFinalBlock(authKey, x, 36); // authKey[x:36] var sha256_a = sha256.Hash; @@ -318,6 +318,7 @@ j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB sha256.TransformBlock(authKey, 40 + x, 36, null, 0); // authKey[40+x:36] sha256.TransformFinalBlock(msgKey, msgKeyOffset, 16); // msgKey var sha256_b = sha256.Hash; + sha256.Initialize(); Array.Copy(sha256_a, 0, aes_key, 0, 8); Array.Copy(sha256_b, 8, aes_key, 8, 16); Array.Copy(sha256_a, 24, aes_key, 24, 8); @@ -374,7 +375,6 @@ j4WcDuXc2CTHgH8gFTNhp/Y8/SpDOhvn9QIDAQAB ValidityChecks(p, algo.g); using var sha256 = SHA256.Create(); - sha256.Initialize(); sha256.TransformBlock(algo.salt1, 0, algo.salt1.Length, null, 0); sha256.TransformBlock(passwordBytes, 0, passwordBytes.Length, null, 0); sha256.TransformFinalBlock(algo.salt1, 0, algo.salt1.Length);