Skip to content

Commit bfca175

Browse files
committed
Review fixes: TwoFactorAuth optimization, nullable compat, legacy csproj
- TwoFactorAuth.cs: fast Base32 lookup (O(1) array), precomputed MOD_VALUE, added #nullable disable for legacy C# 7 compat, added using for ConfigHelper - Legacy csproj: removed deleted GetallLists.cs, added missing file refs (CamlQueryBuilder, DataQualityBase, HealthChecker, OperationContext, OperationStatistics, RetryPolicy, SharePointAuthConfig, TwoFactorAuth)
1 parent 232174e commit bfca175

2 files changed

Lines changed: 35 additions & 13 deletions

File tree

SPOtoSQL-Snapshots/ConsoleApp1/ConsoleApp1.csproj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,19 @@
8888
<Compile Include="Sqlserver\RefreshSQLLists.cs" />
8989
<Compile Include="Sqlserver\SQLInteraction.cs" />
9090
<Compile Include="Sharepoint\ActivitiesDQ.cs" />
91-
<Compile Include="Sharepoint\GetallLists.cs" />
92-
<Compile Include="Sharepoint\InvoiceRequestDQ.cs" />
91+
<Compile Include="Sharepoint\CamlQueryBuilder.cs" />
9392
<Compile Include="Sharepoint\Context.cs" />
93+
<Compile Include="Sharepoint\DataQualityBase.cs" />
94+
<Compile Include="Sharepoint\HealthChecker.cs" />
95+
<Compile Include="Sharepoint\InvoiceRequestDQ.cs" />
96+
<Compile Include="Sharepoint\OperationContext.cs" />
97+
<Compile Include="Sharepoint\OperationStatistics.cs" />
98+
<Compile Include="Sharepoint\RetryPolicy.cs" />
99+
<Compile Include="Sharepoint\SharePointAuthConfig.cs" />
94100
<Compile Include="Sharepoint\SPOList.cs" />
95101
<Compile Include="Sharepoint\SPOUser.cs" />
96102
<Compile Include="Sharepoint\TimesheetDQ.cs" />
103+
<Compile Include="Sharepoint\TwoFactorAuth.cs" />
97104
<Compile Include="XmlConfig\ConfigHelper.cs" />
98105
<Compile Include="ConsoleLogger\Logger.cs" />
99106
<Compile Include="AssemblyInfo.cs" />

SPOtoSQL-Snapshots/ConsoleApp1/Sharepoint/TwoFactorAuth.cs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
#nullable disable
12
using System;
3+
using System.Collections.Generic;
24
using System.Security.Cryptography;
35
using System.Text;
6+
using Bring.XmlConfig;
47

58
namespace Bring.Sharepoint
69
{
@@ -9,6 +12,17 @@ public static class TwoFactorAuth
912
private const string APP_NAME = "SPO2SQL";
1013
private const int TIME_STEP = 30;
1114
private const int CODE_DIGITS = 6;
15+
private const int MOD_VALUE = 1000000;
16+
17+
private static readonly int[] Base32Lookup = new int[128];
18+
19+
static TwoFactorAuth()
20+
{
21+
for (int i = 0; i < 128; i++) Base32Lookup[i] = -1;
22+
string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
23+
for (int i = 0; i < alpha.Length; i++)
24+
Base32Lookup[alpha[i]] = i;
25+
}
1226

1327
public static bool IsEnabled => ConfigurationReader.IsTwoFactorEnabled();
1428

@@ -19,9 +33,7 @@ public static bool PerformVerification()
1933
string secret = ConfigurationReader.GetTwoFactorSecret();
2034

2135
if (string.IsNullOrEmpty(secret))
22-
{
2336
return RunSetup();
24-
}
2537

2638
return RunVerification(secret);
2739
}
@@ -40,7 +52,8 @@ private static bool RunSetup()
4052
Console.WriteLine(" otpauth://totp/" + APP_NAME + "?secret=" + secret + "&issuer=" + APP_NAME);
4153
Console.WriteLine();
4254
Console.Write("Enter the 6-digit code from your authenticator app: ");
43-
string? code = Console.ReadLine()?.Trim();
55+
string input = Console.ReadLine();
56+
string code = input?.Trim();
4457

4558
if (!string.IsNullOrEmpty(code) && ValidateCode(secret, code))
4659
{
@@ -61,12 +74,11 @@ private static bool RunVerification(string secret)
6174
for (int attempts = 0; attempts < 3; attempts++)
6275
{
6376
Console.Write("Enter 2FA code: ");
64-
string? code = Console.ReadLine()?.Trim();
77+
string input = Console.ReadLine();
78+
string code = input?.Trim();
6579

6680
if (!string.IsNullOrEmpty(code) && ValidateCode(secret, code))
67-
{
6881
return true;
69-
}
7082

7183
if (attempts < 2)
7284
Console.WriteLine("Invalid code. Try again.");
@@ -112,7 +124,7 @@ private static string ComputeTotp(byte[] secret, long counter)
112124
| ((hash[offset + 2] & 0xff) << 8)
113125
| (hash[offset + 3] & 0xff);
114126

115-
int otp = binary % (int)Math.Pow(10, CODE_DIGITS);
127+
int otp = binary % MOD_VALUE;
116128
return otp.ToString().PadLeft(CODE_DIGITS, '0');
117129
}
118130

@@ -156,16 +168,19 @@ private static string Base32Encode(byte[] data)
156168

157169
private static byte[] Base32Decode(string input)
158170
{
159-
const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
160-
input = input.Trim().ToUpperInvariant().Replace(" ", "").Replace("-", "");
171+
if (input == null)
172+
return Array.Empty<byte>();
173+
174+
input = input.Trim().ToUpperInvariant();
175+
var bytes = new List<byte>();
161176

162177
int bitBuffer = 0;
163178
int bitsInBuffer = 0;
164-
var bytes = new System.Collections.Generic.List<byte>();
165179

166180
foreach (char c in input)
167181
{
168-
int value = alphabet.IndexOf(c);
182+
if (c >= 128) continue;
183+
int value = Base32Lookup[c];
169184
if (value < 0) continue;
170185

171186
bitBuffer = (bitBuffer << 5) | value;

0 commit comments

Comments
 (0)