From c968b2efbee0d09717f272e46aab5a9ad0bf190a Mon Sep 17 00:00:00 2001 From: Afshin Arani Date: Sun, 13 Sep 2020 17:22:09 +0430 Subject: [PATCH 1/6] .NET Framework Version Upgrade 4.6 -> 4.6.2 --- src/TgSharp.Core/TgSharp.Core.csproj | 2 +- src/TgSharp.TL/TgSharp.TL.csproj | 4 ++-- src/TgSharp.Tests.NUnit/TgSharp.Tests.NUnit.csproj | 6 +++--- src/TgSharp.Tests.VS/TgSharp.Tests.VS.csproj | 6 +++--- src/TgSharp.Tests/TgSharp.Tests.csproj | 4 ++-- src/TgSharp.Tests/app.config | 12 ++++++------ 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/TgSharp.Core/TgSharp.Core.csproj b/src/TgSharp.Core/TgSharp.Core.csproj index 18d23a78..db9caa4e 100644 --- a/src/TgSharp.Core/TgSharp.Core.csproj +++ b/src/TgSharp.Core/TgSharp.Core.csproj @@ -9,7 +9,7 @@ Properties TgSharp.Core TgSharp.Core - v4.6 + v4.6.2 512 diff --git a/src/TgSharp.TL/TgSharp.TL.csproj b/src/TgSharp.TL/TgSharp.TL.csproj index 0e7e43cd..b5453858 100644 --- a/src/TgSharp.TL/TgSharp.TL.csproj +++ b/src/TgSharp.TL/TgSharp.TL.csproj @@ -9,7 +9,7 @@ Properties TgSharp.TL TgSharp.TL - v4.6 + v4.6.2 512 @@ -77,4 +77,4 @@ --> - + \ No newline at end of file diff --git a/src/TgSharp.Tests.NUnit/TgSharp.Tests.NUnit.csproj b/src/TgSharp.Tests.NUnit/TgSharp.Tests.NUnit.csproj index d3480072..5065c8b6 100644 --- a/src/TgSharp.Tests.NUnit/TgSharp.Tests.NUnit.csproj +++ b/src/TgSharp.Tests.NUnit/TgSharp.Tests.NUnit.csproj @@ -1,4 +1,4 @@ - + Debug @@ -7,7 +7,7 @@ Library TgSharp.Tests.NUnit TgSharp.Tests.NUnit - v4.6 + v4.6.2 @@ -52,4 +52,4 @@ TgSharp.Tests - + \ No newline at end of file diff --git a/src/TgSharp.Tests.VS/TgSharp.Tests.VS.csproj b/src/TgSharp.Tests.VS/TgSharp.Tests.VS.csproj index 37677561..87115a92 100644 --- a/src/TgSharp.Tests.VS/TgSharp.Tests.VS.csproj +++ b/src/TgSharp.Tests.VS/TgSharp.Tests.VS.csproj @@ -1,4 +1,4 @@ - + Debug @@ -8,7 +8,7 @@ Properties TgSharp.Tests.VS TgSharp.Tests.VS - v4.6 + v4.6.2 512 {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 10.0 @@ -55,7 +55,7 @@ - + {d6144517-91d2-4880-86df-e9ff5d7f383a} TeleSharp.TL diff --git a/src/TgSharp.Tests/TgSharp.Tests.csproj b/src/TgSharp.Tests/TgSharp.Tests.csproj index ba8b83dd..0ae66210 100644 --- a/src/TgSharp.Tests/TgSharp.Tests.csproj +++ b/src/TgSharp.Tests/TgSharp.Tests.csproj @@ -1,4 +1,4 @@ - + @@ -9,7 +9,7 @@ Properties TgSharp.Tests TgSharp.Tests - v4.6 + v4.6.2 512 diff --git a/src/TgSharp.Tests/app.config b/src/TgSharp.Tests/app.config index 490261d2..4d65c695 100644 --- a/src/TgSharp.Tests/app.config +++ b/src/TgSharp.Tests/app.config @@ -1,10 +1,10 @@ - + - - - - + + + + @@ -12,4 +12,4 @@ - + From 0f0a0d510862a635ec6ba7fc7d8f7fc464bde315 Mon Sep 17 00:00:00 2001 From: Afshin Arani Date: Sun, 13 Sep 2020 18:04:19 +0430 Subject: [PATCH 2/6] .netStandard: Convert projects to .net standard 2.0 --- src/TgSharp.Core/Properties/AssemblyInfo.cs | 36 ------- src/TgSharp.Core/TgSharp.Core.csproj | 101 ++------------------ src/TgSharp.Core/TgSharp.Core.nuspec | 2 +- src/TgSharp.Core/packages.config | 3 - src/TgSharp.TL/Properties/AssemblyInfo.cs | 36 ------- src/TgSharp.TL/TgSharp.TL.csproj | 83 +--------------- src/TgSharp.TL/packages.config | 3 - src/TgSharp.sln | 30 ++++-- 8 files changed, 33 insertions(+), 261 deletions(-) delete mode 100644 src/TgSharp.Core/Properties/AssemblyInfo.cs delete mode 100644 src/TgSharp.Core/packages.config delete mode 100644 src/TgSharp.TL/Properties/AssemblyInfo.cs delete mode 100644 src/TgSharp.TL/packages.config diff --git a/src/TgSharp.Core/Properties/AssemblyInfo.cs b/src/TgSharp.Core/Properties/AssemblyInfo.cs deleted file mode 100644 index 01b79db1..00000000 --- a/src/TgSharp.Core/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("TgSharp.Core")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("TgSharp.Core")] -[assembly: AssemblyCopyright("Copyright © 2015")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("400d2544-1cc6-4d8a-a62c-2292d9947a16")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/TgSharp.Core/TgSharp.Core.csproj b/src/TgSharp.Core/TgSharp.Core.csproj index db9caa4e..44ba9c15 100644 --- a/src/TgSharp.Core/TgSharp.Core.csproj +++ b/src/TgSharp.Core/TgSharp.Core.csproj @@ -1,98 +1,11 @@ - - - + + - Debug - AnyCPU - {400D2544-1CC6-4D8A-A62C-2292D9947A16} - Library - Properties - TgSharp.Core - TgSharp.Core - v4.6.2 - 512 - + netstandard2.0 - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - {d6144517-91d2-4880-86df-e9ff5d7f383a} - TeleSharp.TL - + - - - - \ No newline at end of file + + diff --git a/src/TgSharp.Core/TgSharp.Core.nuspec b/src/TgSharp.Core/TgSharp.Core.nuspec index 61166662..7e053bc2 100644 --- a/src/TgSharp.Core/TgSharp.Core.nuspec +++ b/src/TgSharp.Core/TgSharp.Core.nuspec @@ -15,6 +15,6 @@ It's a perfect fit for any developer who would like to send data directly to Tel Copyright 2015-2020 - + diff --git a/src/TgSharp.Core/packages.config b/src/TgSharp.Core/packages.config deleted file mode 100644 index 6b8deb9c..00000000 --- a/src/TgSharp.Core/packages.config +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/src/TgSharp.TL/Properties/AssemblyInfo.cs b/src/TgSharp.TL/Properties/AssemblyInfo.cs deleted file mode 100644 index b690d86e..00000000 --- a/src/TgSharp.TL/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("TgSharp.TL")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("TgSharp.TL")] -[assembly: AssemblyCopyright("Copyright © 2016")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("d6144517-91d2-4880-86df-e9ff5d7f383a")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/TgSharp.TL/TgSharp.TL.csproj b/src/TgSharp.TL/TgSharp.TL.csproj index b5453858..9f5c4f4a 100644 --- a/src/TgSharp.TL/TgSharp.TL.csproj +++ b/src/TgSharp.TL/TgSharp.TL.csproj @@ -1,80 +1,7 @@ - - - + + - Debug - AnyCPU - {D6144517-91D2-4880-86DF-E9FF5D7F383A} - Library - Properties - TgSharp.TL - TgSharp.TL - v4.6.2 - 512 - + netstandard2.0 - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file + + diff --git a/src/TgSharp.TL/packages.config b/src/TgSharp.TL/packages.config deleted file mode 100644 index 6b8deb9c..00000000 --- a/src/TgSharp.TL/packages.config +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/src/TgSharp.sln b/src/TgSharp.sln index dbd29692..703e51ef 100644 --- a/src/TgSharp.sln +++ b/src/TgSharp.sln @@ -1,12 +1,7 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 14 -VisualStudioVersion = 14.0.24720.0 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30404.54 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TgSharp.Core", "TgSharp.Core\TgSharp.Core.csproj", "{400D2544-1CC6-4D8A-A62C-2292D9947A16}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TgSharp.TL", "TgSharp.TL\TgSharp.TL.csproj", "{D6144517-91D2-4880-86DF-E9FF5D7F383A}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TgSharp.Generator", "TgSharp.Generator\TgSharp.Generator.csproj", "{9BE3B9D4-9FF6-4DC8-B9CC-EB2E3F390129}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TgSharp.Tests", "TgSharp.Tests\TgSharp.Tests.csproj", "{DE5C0467-EE99-4734-95F2-EFF7A0B99924}" @@ -15,6 +10,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TgSharp.Tests.VS", "TgSharp EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TgSharp.Tests.NUnit", "TgSharp.Tests.NUnit\TgSharp.Tests.NUnit.csproj", "{E90B705B-19FA-43BA-B952-69957976D12C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TgSharp.Core", "TgSharp.Core\TgSharp.Core.csproj", "{6D916BF5-CF58-4F71-BC7A-5CBCB4FD0AD1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TgSharp.TL", "TgSharp.TL\TgSharp.TL.csproj", "{01B4EC9D-2D79-4A0E-B4A9-B24F072B2213}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +44,20 @@ Global {E90B705B-19FA-43BA-B952-69957976D12C}.Debug|Any CPU.Build.0 = Debug|Any CPU {E90B705B-19FA-43BA-B952-69957976D12C}.Release|Any CPU.ActiveCfg = Release|Any CPU {E90B705B-19FA-43BA-B952-69957976D12C}.Release|Any CPU.Build.0 = Release|Any CPU + {6D916BF5-CF58-4F71-BC7A-5CBCB4FD0AD1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6D916BF5-CF58-4F71-BC7A-5CBCB4FD0AD1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6D916BF5-CF58-4F71-BC7A-5CBCB4FD0AD1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6D916BF5-CF58-4F71-BC7A-5CBCB4FD0AD1}.Release|Any CPU.Build.0 = Release|Any CPU + {01B4EC9D-2D79-4A0E-B4A9-B24F072B2213}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {01B4EC9D-2D79-4A0E-B4A9-B24F072B2213}.Debug|Any CPU.Build.0 = Debug|Any CPU + {01B4EC9D-2D79-4A0E-B4A9-B24F072B2213}.Release|Any CPU.ActiveCfg = Release|Any CPU + {01B4EC9D-2D79-4A0E-B4A9-B24F072B2213}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {892941D6-2647-46DA-A8AC-5695818C4CFA} EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution Policies = $0 @@ -81,7 +94,4 @@ Global $2.inheritsScope = text/x-csharp $2.scope = text/x-csharp EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection EndGlobal From e683af18cd36a2072118d7c05ce335070231f2cc Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Sun, 13 Sep 2020 22:40:02 +0800 Subject: [PATCH 3/6] GithubCI: update to use .NETCore (because we do .NETStandard now) --- .github/workflows/CI.yml | 37 ++++++++++--------------------- .github/workflows/nugetUpload.yml | 10 ++++----- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 0f39392a..37be9739 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -1,50 +1,37 @@ name: CI on: [push, pull_request] -env: - NUGET_URL: https://dist.nuget.org/win-x86-commandline/v5.4.0/nuget.exe jobs: linux: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - - name: Download latest NuGet - run: wget $NUGET_URL - name: Install Dependencies - run: mono nuget.exe restore src/TgSharp.sln - + run: dotnet restore src + - name: Build Project - run: xbuild src/TgSharp.Core/TgSharp.Core.csproj - + run: dotnet build src + macos: runs-on: macos-latest steps: - uses: actions/checkout@v2 - - - name: Download latest NuGet - run: curl -o nuget.exe $NUGET_URL - name: Install Dependencies - run: mono nuget.exe restore src/TgSharp.sln - + run: dotnet restore src + - name: Build Project - run: MSBuild src/TgSharp.Core/TgSharp.Core.csproj + run: dotnet build src windows: runs-on: windows-latest steps: - uses: actions/checkout@v2 - - - name: Download Nuget.exe - run: Invoke-WebRequest -OutFile Nuget.exe $Env:NUGET_URL - + - name: Install Dependencies - run: ./Nuget.exe restore src/TgSharp.sln - - - name: setup-msbuild - uses: microsoft/setup-msbuild@v1 - + run: dotnet restore src + - name: Build Project - run: MSBuild src/TgSharp.sln + run: dotnet build src + diff --git a/.github/workflows/nugetUpload.yml b/.github/workflows/nugetUpload.yml index 578c3885..b631e3e9 100644 --- a/.github/workflows/nugetUpload.yml +++ b/.github/workflows/nugetUpload.yml @@ -13,12 +13,12 @@ jobs: run: Invoke-WebRequest -OutFile Nuget.exe $Env:NUGET_URL - name: Install Dependencies - run: ./Nuget.exe restore src/TgSharp.sln + run: dotnet restore - - name: setup-msbuild - uses: microsoft/setup-msbuild@v1 + - name: Build + run: dotnet build - - name: Build & Upload Package + - name: Package && Upload run: | $date = get-date -format "yyyyMMdd-HHmm" $hash = & git rev-parse --short HEAD @@ -32,7 +32,7 @@ jobs: $version = "$baseVersion--date$date.git-$hash" } - ./Nuget.exe pack src/TgSharp.Core/TgSharp.Core.csproj -Version $version -Build -Properties Configuration=Release + ./Nuget.exe pack src/TgSharp.Core/TgSharp.Core.csproj -Version $version -Properties Configuration=Release If ($Env:GITHUB_REF -eq 'refs/heads/master' -OR $Env:GITHUB_REF.StartsWith($tagPrefix)) { If ('${{ secrets.NUGET_API_KEY }}' -ne '') { ./Nuget.exe push "TgSharp.$version.nupkg" ${{secrets.NUGET_API_KEY}} -Source https://api.nuget.org/v3/index.json From c517bb41949584e700dfbddb7f65a0fdf36ad3bd Mon Sep 17 00:00:00 2001 From: Afshin Arani Date: Mon, 14 Sep 2020 02:46:29 +0430 Subject: [PATCH 4/6] SRP feature --- src/TgSharp.Core/TelegramClient.cs | 24 ++++- src/TgSharp.Core/TgSharp.Core.csproj | 4 + src/TgSharp.Core/TgSharp.Core.nuspec | 40 ++++---- src/TgSharp.Core/Utils/SRPHelper.cs | 131 +++++++++++++++++++++++++++ 4 files changed, 180 insertions(+), 19 deletions(-) create mode 100644 src/TgSharp.Core/Utils/SRPHelper.cs diff --git a/src/TgSharp.Core/TelegramClient.cs b/src/TgSharp.Core/TelegramClient.cs index 05936977..f3e11ad3 100644 --- a/src/TgSharp.Core/TelegramClient.cs +++ b/src/TgSharp.Core/TelegramClient.cs @@ -210,7 +210,7 @@ public async Task SendCodeRequestAsync(string phoneNumber, CancellationT return request.Response.PhoneCodeHash; } - public async Task MakeAuthAsync(string phoneNumber, string phoneCodeHash, string code, string firstName = "", string lastName = "", CancellationToken token = default(CancellationToken)) + public async Task MakeAuthAsync(string phoneNumber, string phoneCodeHash, string code, string password = "", string firstName = "", string lastName = "", CancellationToken token = default(CancellationToken)) { if (String.IsNullOrWhiteSpace(phoneNumber)) throw new ArgumentNullException(nameof(phoneNumber)); @@ -222,14 +222,32 @@ public async Task MakeAuthAsync(string phoneNumber, string phoneCodeHash throw new ArgumentNullException(nameof(code)); var request = new TLRequestSignIn() { PhoneNumber = phoneNumber, PhoneCodeHash = phoneCodeHash, PhoneCode = code }; + TLRequestCheckPassword requestCheckPassword = null; - await RequestWithDcMigration(request, token).ConfigureAwait(false); + try + { + await RequestWithDcMigration(request, token).ConfigureAwait(false); + } + catch (CloudPasswordNeededException ex) + { + if (password != "") + { + requestCheckPassword = new TLRequestCheckPassword { Password = await SRPHelper.CheckPassword(this, password, token) }; + await RequestWithDcMigration(requestCheckPassword, token).ConfigureAwait(false); + } + else throw ex; + } - if (request.Response is TLAuthorization) + if (requestCheckPassword == null && request.Response is TLAuthorization) { OnUserAuthenticated(((TLUser)((TLAuthorization)request.Response).User)); return ((TLUser)((TLAuthorization)request.Response).User); } + else if (requestCheckPassword != null && requestCheckPassword.Response is TLAuthorization) + { + OnUserAuthenticated(((TLUser)((TLAuthorization)requestCheckPassword.Response).User)); + return ((TLUser)((TLAuthorization)requestCheckPassword.Response).User); + } else { var signUpRequest = new TLRequestSignUp() { PhoneNumber = phoneNumber, PhoneCodeHash = phoneCodeHash, FirstName = firstName, LastName = lastName }; diff --git a/src/TgSharp.Core/TgSharp.Core.csproj b/src/TgSharp.Core/TgSharp.Core.csproj index 44ba9c15..912aa7c4 100644 --- a/src/TgSharp.Core/TgSharp.Core.csproj +++ b/src/TgSharp.Core/TgSharp.Core.csproj @@ -4,6 +4,10 @@ netstandard2.0 + + + + diff --git a/src/TgSharp.Core/TgSharp.Core.nuspec b/src/TgSharp.Core/TgSharp.Core.nuspec index 7e053bc2..8975b71d 100644 --- a/src/TgSharp.Core/TgSharp.Core.nuspec +++ b/src/TgSharp.Core/TgSharp.Core.nuspec @@ -1,20 +1,28 @@ - - - TgSharp - 0.1 - Telegram client library implemented in C# - Ilya P + + + TgSharp + 0.1 + Telegram client library implemented in C# + Ilya P aarani, knocte - https://github.com/nblockchain/TgSharp/ - false - Unofficial Telegram (http://telegram.org) client library implemented in C#. + MIT + https://github.com/nblockchain/TgSharp/ + false + + Unofficial Telegram (http://telegram.org) client library implemented in C#. -It's a perfect fit for any developer who would like to send data directly to Telegram users or write own custom Telegram client. - telegram client, telegram API - Copyright 2015-2020 - - - - + It's a perfect fit for any developer who would like to send data directly to Telegram users or write own custom Telegram client. + + telegram client, telegram API + Copyright 2015-2020 + + + + + + + + + diff --git a/src/TgSharp.Core/Utils/SRPHelper.cs b/src/TgSharp.Core/Utils/SRPHelper.cs new file mode 100644 index 00000000..77e88264 --- /dev/null +++ b/src/TgSharp.Core/Utils/SRPHelper.cs @@ -0,0 +1,131 @@ +using Org.BouncyCastle.Crypto.Generators; +using Org.BouncyCastle.Crypto.Parameters; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Runtime.CompilerServices; +using System.Security.Cryptography; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Xml.Serialization; +using TgSharp.Core.MTProto.Crypto; +using TgSharp.TL; +using TgSharp.TL.Account; + +namespace TgSharp.Core.Utils +{ + public static class SRPHelper + { + public static async Task CheckPassword(this TelegramClient client, string password, CancellationToken token = default) + { + var passwordSettings = await client.SendRequestAsync(new TLRequestGetPassword { }, token); + + var algoSettings = passwordSettings.CurrentAlgo as TLPasswordKdfAlgoSHA256SHA256PBKDF2HMACSHA512iter100000SHA256ModPow; + if (algoSettings == null) + throw new NotImplementedException(); + + var passwordInBytes = Encoding.UTF8.GetBytes(password); + + var PrivateKey = new BigInteger(1, PH2(passwordInBytes, algoSettings.Salt1, algoSettings.Salt2)); + var Generator = BigInteger.ValueOf(algoSettings.G); + var Prime = new BigInteger(1, algoSettings.P); + + var gForHash = PadBigNumForHash(Generator); + var pForHash = PadBytesForHash(algoSettings.P); + + //Random prime number + var Random = RandomNumberGenerator.Create(); + var RandomBytes = new byte[256]; + Random.GetBytes(RandomBytes); + + var G_X = Generator.ModPow(PrivateKey, Prime); + var K = new BigInteger(1, H(pForHash, gForHash)); + var KG_X = K.Multiply(G_X).Mod(Prime); + + var A = new BigInteger(1, RandomBytes.ToArray()); + var GA = Generator.ModPow(A, Prime); + var GAForHash = PadBigNumForHash(GA); + var GB = new BigInteger(1, passwordSettings.SrpB); + var GBForHash = PadBytesForHash(passwordSettings.SrpB); + + var U = new BigInteger(1, H(GAForHash, GBForHash)); + + + var T = (GB.Subtract(KG_X)).Mod(Prime); + var S = T.ModPow(U.Multiply(PrivateKey).Add(A), Prime); + var K_A = H(PadBytesForHash(S.ToByteArrayUnsigned())); + var M1 = H(XOR(H(pForHash), H(gForHash)), + H(algoSettings.Salt1), + H(algoSettings.Salt2), + GAForHash, + GBForHash, + K_A + ); + + return new TLInputCheckPasswordSRP + { + A = GAForHash, + M1 = M1, + SrpId = passwordSettings.SrpId.Value + }; + } + + #region Hashing Functions + private static byte[] H(params byte[][] data) + { + using (SHA256Managed sha256 = new SHA256Managed()) + { + var position = 0; + var outputArray = new byte[data.Sum(a => a.Length)]; + foreach (var curr in data) + { + Array.Copy(curr, 0, outputArray, position, curr.Length); + position += curr.Length; + } + return sha256.ComputeHash(outputArray); + } + } + private static byte[] Pbkdf2(byte[] password, byte[] salt, int iterations = 100000, int hashByteSize = 64) + { + var pdb = new Pkcs5S2ParametersGenerator(new Org.BouncyCastle.Crypto.Digests.Sha512Digest()); + pdb.Init(password, salt, + iterations); + var key = (KeyParameter)pdb.GenerateDerivedMacParameters(hashByteSize * 8); + return key.GetKey(); + } + private static byte[] SH(byte[] data, byte[] salt) => H(salt, data, salt); + private static byte[] PH1(byte[] data, byte[] salt1, byte[] salt2) => SH(SH(data, salt1), salt2); + private static byte[] PH2(byte[] password, byte[] salt1, byte[] salt2) => SH(Pbkdf2(PH1(password, salt1, salt2), salt1), salt2); + #endregion + #region Padding Functions + private static byte[] PadBytesForHash(byte[] data) + { + return new byte[256 - data.Length].Concat(data).ToArray(); + } + private static byte[] PadBigNumForHash(BigInteger number) + { + var data = number.ToByteArrayUnsigned(); + return new byte[256 - data.Length].Concat(data).ToArray(); + } + #endregion + #region Computational Functions + public static byte[] XOR(byte[] arr1, byte[] arr2) + { + if (arr1.Length != arr2.Length) + throw new ArgumentException("arr1 and arr2 are not the same length"); + + byte[] result = new byte[arr1.Length]; + + for (int i = 0; i < arr1.Length; ++i) + result[i] = (byte)(arr1[i] ^ arr2[i]); + + return result; + } + #endregion + + + } +} + From 39c3f96a513a29e274e60ff53b41ef711745c698 Mon Sep 17 00:00:00 2001 From: Afshin Arani Date: Mon, 14 Sep 2020 02:49:44 +0430 Subject: [PATCH 5/6] Add tests for two factor authentication --- src/TgSharp.Tests/TgSharpTests.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/TgSharp.Tests/TgSharpTests.cs b/src/TgSharp.Tests/TgSharpTests.cs index 4d8cb97c..79295b5d 100644 --- a/src/TgSharp.Tests/TgSharpTests.cs +++ b/src/TgSharp.Tests/TgSharpTests.cs @@ -144,6 +144,14 @@ public virtual async Task AuthUser() throw new Exception("CodeToAuthenticate is wrong in the app.config file, fill it with the code you just got now by SMS/Telegram", ex); } + catch (CloudPasswordNeededException ex) + { + if (!string.IsNullOrEmpty(PasswordToAuthenticate)) + user = await client.MakeAuthAsync(NumberToAuthenticate, hash, code, PasswordToAuthenticate); + else + throw ex; + } + Assert.IsNotNull(user); Assert.IsTrue(client.IsUserAuthorized()); } From 590d7b3eb7d87979bd96e1fadc431b6c2f507662 Mon Sep 17 00:00:00 2001 From: Afshin Arani Date: Mon, 14 Sep 2020 02:59:26 +0430 Subject: [PATCH 6/6] Addding bouncycastle as dependency to test projects --- src/TgSharp.Tests.NUnit/TgSharp.Tests.NUnit.csproj | 3 +++ src/TgSharp.Tests.NUnit/packages.config | 1 + src/TgSharp.Tests.VS/TgSharp.Tests.VS.csproj | 4 ++++ src/TgSharp.Tests.VS/packages.config | 4 ++++ 4 files changed, 12 insertions(+) create mode 100644 src/TgSharp.Tests.VS/packages.config diff --git a/src/TgSharp.Tests.NUnit/TgSharp.Tests.NUnit.csproj b/src/TgSharp.Tests.NUnit/TgSharp.Tests.NUnit.csproj index 5065c8b6..edbbc51f 100644 --- a/src/TgSharp.Tests.NUnit/TgSharp.Tests.NUnit.csproj +++ b/src/TgSharp.Tests.NUnit/TgSharp.Tests.NUnit.csproj @@ -29,6 +29,9 @@ false + + ..\packages\Portable.BouncyCastle.1.8.6.7\lib\net40\BouncyCastle.Crypto.dll + ..\packages\NUnit.2.6.4\lib\nunit.framework.dll diff --git a/src/TgSharp.Tests.NUnit/packages.config b/src/TgSharp.Tests.NUnit/packages.config index c714ef3a..76bd4fcb 100644 --- a/src/TgSharp.Tests.NUnit/packages.config +++ b/src/TgSharp.Tests.NUnit/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file diff --git a/src/TgSharp.Tests.VS/TgSharp.Tests.VS.csproj b/src/TgSharp.Tests.VS/TgSharp.Tests.VS.csproj index 87115a92..588c7758 100644 --- a/src/TgSharp.Tests.VS/TgSharp.Tests.VS.csproj +++ b/src/TgSharp.Tests.VS/TgSharp.Tests.VS.csproj @@ -36,6 +36,9 @@ 4 + + ..\packages\Portable.BouncyCastle.1.8.6.7\lib\net40\BouncyCastle.Crypto.dll + @@ -72,6 +75,7 @@ app.config + diff --git a/src/TgSharp.Tests.VS/packages.config b/src/TgSharp.Tests.VS/packages.config new file mode 100644 index 00000000..eb1468af --- /dev/null +++ b/src/TgSharp.Tests.VS/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file