mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2024-10-30 05:42:47 +01:00
35 lines
931 B
C#
35 lines
931 B
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace Phantom.Utils.Cryptography;
|
|
|
|
public static class TokenGenerator {
|
|
private const string AllowedCharacters = "25679BCDFGHJKMNPQRSTWXYZ";
|
|
|
|
private static readonly HashSet<char> AllowedCharacterSet = new (AllowedCharacters);
|
|
|
|
public static string Create(int length) {
|
|
char[] result = new char[length];
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
result[i] = AllowedCharacters[RandomNumberGenerator.GetInt32(AllowedCharacters.Length)];
|
|
}
|
|
|
|
return new string(result);
|
|
}
|
|
|
|
public static byte[] GetBytesOrThrow(string token) {
|
|
if (token.Length == 0) {
|
|
throw new ArgumentOutOfRangeException(nameof(token), "Invalid token (empty).");
|
|
}
|
|
|
|
foreach (char c in token) {
|
|
if (!AllowedCharacterSet.Contains(c)) {
|
|
throw new ArgumentOutOfRangeException(nameof(token), "Invalid token: " + token);
|
|
}
|
|
}
|
|
|
|
return Encoding.ASCII.GetBytes(token);
|
|
}
|
|
}
|