Skip to content

Commit e232309

Browse files
committed
net8.0 target, compatible with AOT
1 parent 9fe1196 commit e232309

6 files changed

+27
-9
lines changed

src/Client.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ private async Task DoConnectAsync(bool quickResume)
918918
TLConfig = new Config { this_dc = _session.MainDC, dc_options = _session.DcOptions };
919919
else
920920
{
921-
var initParams = JSONValue.FromJsonElement(System.Text.Json.JsonSerializer.Deserialize<System.Text.Json.JsonElement>(Config("init_params")));
921+
var initParams = JSONValue.FromJsonElement(System.Text.Json.JsonDocument.Parse(Config("init_params")).RootElement);
922922
TLConfig = await this.InvokeWithLayer(Layer.Version,
923923
new TL.Methods.InitConnection<Config>
924924
{

src/Compat.cs

+7
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ static class Convert
7676
internal static string ToHexString(byte[] data) => BitConverter.ToString(data).Replace("-", "");
7777
internal static byte[] FromHexString(string hex) => Enumerable.Range(0, hex.Length / 2).Select(i => System.Convert.ToByte(hex.Substring(i * 2, 2), 16)).ToArray();
7878
}
79+
public class RandomNumberGenerator
80+
{
81+
internal static readonly RNGCryptoServiceProvider RNG = new();
82+
public static RandomNumberGenerator Create() => new();
83+
public void GetBytes(byte[] data) => RNG.GetBytes(data);
84+
public void GetBytes(byte[] data, int offset, int count) => RNG.GetBytes(data, offset, count);
85+
}
7986
#endif
8087

8188
#if NETSTANDARD2_0

src/Encryption.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace WTelegram
1616
internal static class Encryption
1717
{
1818
private static readonly Dictionary<long, RSAPublicKey> PublicKeys = [];
19-
internal static readonly RNGCryptoServiceProvider RNG = new();
19+
internal static readonly RandomNumberGenerator RNG = RandomNumberGenerator.Create();
2020
internal static readonly Aes AesECB = Aes.Create();
2121

2222
static Encryption()
@@ -33,7 +33,7 @@ internal static async Task CreateAuthorizationKey(Client client, Session.DCSessi
3333
var sha256 = SHA256.Create();
3434

3535
//1)
36-
var nonce = new Int128(RNG);
36+
var nonce = new TL.Int128(RNG);
3737
var resPQ = await client.ReqPqMulti(nonce);
3838
//2)
3939
if (resPQ.nonce != nonce) throw new WTException("Nonce mismatch");
@@ -164,7 +164,7 @@ internal static async Task CreateAuthorizationKey(Client client, Session.DCSessi
164164
session.Salt = BinaryPrimitives.ReadInt64LittleEndian(pqInnerData.new_nonce.raw) ^ BinaryPrimitives.ReadInt64LittleEndian(resPQ.server_nonce.raw);
165165
session.OldSalt = session.Salt;
166166

167-
(byte[] key, byte[] iv) ConstructTmpAESKeyIV(Int128 server_nonce, Int256 new_nonce)
167+
(byte[] key, byte[] iv) ConstructTmpAESKeyIV(TL.Int128 server_nonce, Int256 new_nonce)
168168
{
169169
byte[] tmp_aes_key = new byte[32], tmp_aes_iv = new byte[32];
170170
sha1.TransformBlock(new_nonce, 0, 32, null, 0);

src/Helpers.cs

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
using System.Threading;
88
using System.Threading.Tasks;
99

10+
#if NET8_0_OR_GREATER
11+
using System.Text.Json.Serialization;
12+
using System.Text.Json.Serialization.Metadata;
13+
[JsonSerializable(typeof(WTelegram.Session))]
14+
internal partial class WTelegramContext : JsonSerializerContext { }
15+
#endif
16+
1017
namespace WTelegram
1118
{
1219
public static class Helpers
@@ -16,6 +23,9 @@ public static class Helpers
1623

1724
/// <summary>For serializing indented Json with fields included</summary>
1825
public static readonly JsonSerializerOptions JsonOptions = new() { IncludeFields = true, WriteIndented = true,
26+
#if NET8_0_OR_GREATER
27+
TypeInfoResolver = JsonSerializer.IsReflectionEnabledByDefault ? new DefaultJsonTypeInfoResolver() : WTelegramContext.Default,
28+
#endif
1929
IgnoreReadOnlyProperties = true, DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull };
2030

2131
private static readonly ConsoleColor[] LogLevelToColor = [ ConsoleColor.DarkGray, ConsoleColor.DarkCyan,

src/TL.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ public struct Int128
328328
{
329329
public byte[] raw;
330330

331-
public Int128(System.IO.BinaryReader reader) => raw = reader.ReadBytes(16);
332-
public Int128(RNGCryptoServiceProvider rng) => rng.GetBytes(raw = new byte[16]);
331+
public Int128(BinaryReader reader) => raw = reader.ReadBytes(16);
332+
public Int128(RandomNumberGenerator rng) => rng.GetBytes(raw = new byte[16]);
333333
public static bool operator ==(Int128 left, Int128 right) { for (int i = 0; i < 16; i++) if (left.raw[i] != right.raw[i]) return false; return true; }
334334
public static bool operator !=(Int128 left, Int128 right) { for (int i = 0; i < 16; i++) if (left.raw[i] != right.raw[i]) return true; return false; }
335335
public override readonly bool Equals(object obj) => obj is Int128 other && this == other;
@@ -342,8 +342,8 @@ public struct Int256
342342
{
343343
public byte[] raw;
344344

345-
public Int256(System.IO.BinaryReader reader) => raw = reader.ReadBytes(32);
346-
public Int256(RNGCryptoServiceProvider rng) => rng.GetBytes(raw = new byte[32]);
345+
public Int256(BinaryReader reader) => raw = reader.ReadBytes(32);
346+
public Int256(RandomNumberGenerator rng) => rng.GetBytes(raw = new byte[32]);
347347
public static bool operator ==(Int256 left, Int256 right) { for (int i = 0; i < 32; i++) if (left.raw[i] != right.raw[i]) return false; return true; }
348348
public static bool operator !=(Int256 left, Int256 right) { for (int i = 0; i < 32; i++) if (left.raw[i] != right.raw[i]) return true; return false; }
349349
public override readonly bool Equals(object obj) => obj is Int256 other && this == other;

src/WTelegramClient.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Library</OutputType>
5-
<TargetFrameworks>netstandard2.0;net5.0</TargetFrameworks>
5+
<TargetFrameworks>netstandard2.0;net5.0;net8.0</TargetFrameworks>
66
<LangVersion>latest</LangVersion>
77
<RootNamespace>WTelegram</RootNamespace>
88
<Deterministic>true</Deterministic>
@@ -26,6 +26,7 @@
2626
<PackageReleaseNotes>$(ReleaseNotes.Replace("|", "%0D%0A").Replace(" - ","%0D%0A- ").Replace(" ", "%0D%0A%0D%0A"))</PackageReleaseNotes>
2727
<NoWarn>0419;1573;1591;NETSDK1138</NoWarn>
2828
<DefineConstants>TRACE;OBFUSCATION</DefineConstants>
29+
<!--<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>-->
2930
</PropertyGroup>
3031

3132
<ItemGroup>

0 commit comments

Comments
 (0)