mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2025-08-17 01:31:42 +02:00
.github
.idea
app
.idea
Desktop
Resources
Server
Database
Endpoints
Service
AppWebServer.cs
ServerAccessToken.cs
ServerStartup.cs
ServerUtils.cs
Server.csproj
Session.cs
SessionSettings.cs
Utils
.gitignore
Directory.build.props
DiscordHistoryTracker.sln
Version.cs
build.bat
build.sh
empty.dht
global.json
lib
tools
web
.gitattributes
.gitignore
LICENSE.md
README.md
37 lines
1.0 KiB
C#
37 lines
1.0 KiB
C#
using System;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace DHT.Server.Service;
|
|
|
|
public sealed partial class ServerAccessToken {
|
|
private readonly byte[] token;
|
|
|
|
internal ServerAccessToken(string token) {
|
|
this.token = Encoding.UTF8.GetBytes(token);
|
|
}
|
|
|
|
internal bool IsValid(string providedToken) {
|
|
return CryptographicOperations.FixedTimeEquals(this.token, Encoding.UTF8.GetBytes(providedToken));
|
|
}
|
|
|
|
public static partial class Random {
|
|
[GeneratedRegex("[^25679bcdfghjkmnpqrstwxyz]", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
|
|
private static partial Regex AllowedCharactersRegex();
|
|
|
|
public static string Generate(int length) {
|
|
byte[] bytes = new byte[length * 3 / 2]; // Extra bytes compensate for filtered out characters.
|
|
var rng = RandomNumberGenerator.Create();
|
|
|
|
string token = "";
|
|
while (token.Length < length) {
|
|
rng.GetBytes(bytes);
|
|
token = AllowedCharactersRegex().Replace(Convert.ToBase64String(bytes), "");
|
|
}
|
|
|
|
return token[..length];
|
|
}
|
|
}
|
|
}
|