Initial version
This commit is contained in:
commit
e5b6226a85
68 changed files with 7338 additions and 0 deletions
54
DataTransfer.Client/Shared/ConnectionCode.cs
Normal file
54
DataTransfer.Client/Shared/ConnectionCode.cs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace DataTransfer.Client.Shared;
|
||||
|
||||
/// <summary>Generates and parses the one-time codes used to pair two participants.</summary>
|
||||
public static class ConnectionCode
|
||||
{
|
||||
public const int Length = 10;
|
||||
|
||||
// Ambiguous characters (0/O, 1/I) are excluded. 32 symbols ^ 10 = 50 bits of entropy.
|
||||
private const string Alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
||||
|
||||
public static string Generate() => RandomNumberGenerator.GetString(Alphabet, Length);
|
||||
|
||||
/// <summary>Accepts user input case-insensitively, ignoring whitespace and dashes.</summary>
|
||||
public static bool TryNormalize(string? input, [NotNullWhen(true)] out string? code)
|
||||
{
|
||||
code = null;
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Span<char> buffer = stackalloc char[Length];
|
||||
var count = 0;
|
||||
foreach (var c in input)
|
||||
{
|
||||
if (char.IsWhiteSpace(c) || c == '-')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var upper = char.ToUpperInvariant(c);
|
||||
if (count == Length || !Alphabet.Contains(upper))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer[count++] = upper;
|
||||
}
|
||||
|
||||
if (count != Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
code = new string(buffer);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static string Format(string code) =>
|
||||
code.Length == Length ? $"{code[..5]}-{code[5..]}" : code;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue