mirror of
https://github.com/C9Glax/tranga.git
synced 2025-06-18 09:17:52 +02:00
Add API
This commit is contained in:
23
API/TokenGen.cs
Normal file
23
API/TokenGen.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace API;
|
||||
|
||||
public static class TokenGen
|
||||
{
|
||||
private const uint MinimumLength = 8;
|
||||
private const string Chars = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||
|
||||
public static string CreateToken(Type t, uint fullLength) => CreateToken(t.Name, fullLength);
|
||||
|
||||
public static string CreateToken(string prefix, uint fullLength)
|
||||
{
|
||||
if (prefix.Length + 1 >= fullLength - MinimumLength)
|
||||
throw new ArgumentException("Prefix to long to create Token of meaningful length.");
|
||||
long l = fullLength - prefix.Length - 1;
|
||||
byte[] rng = new byte[l];
|
||||
RandomNumberGenerator.Create().GetBytes(rng);
|
||||
string key = new (rng.Select(b => Chars[b % Chars.Length]).ToArray());
|
||||
key = string.Join('-', prefix, key);
|
||||
return key;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user