40 lines
1.6 KiB
C#
40 lines
1.6 KiB
C#
using AyCode.Core.Consts;
|
|
|
|
namespace AyCode.Core.Helpers
|
|
{
|
|
public static class AcCharGenerator
|
|
{
|
|
public static readonly char[] Letters;
|
|
public static readonly char[] Numbers;
|
|
public static readonly char[] LettersAndNumbers;
|
|
|
|
static AcCharGenerator()
|
|
{
|
|
//"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
Numbers = "0123456789".ToCharArray();
|
|
Letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
|
|
|
|
LettersAndNumbers = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray();
|
|
}
|
|
|
|
private static char[] GetRandomChars() => GetRandomChars(LettersAndNumbers, 8, 12);
|
|
private static char[] GetRandomChars(int length) => GetRandomChars(LettersAndNumbers, length, length);
|
|
private static char[] GetRandomChars(int minLength, int maxLength) => GetRandomChars(LettersAndNumbers, minLength, maxLength);
|
|
private static char[] GetRandomChars(char[] sourceChars, int minLength, int maxLength)
|
|
{
|
|
var random = new Random();
|
|
return Enumerable.Repeat(sourceChars, random.Next(minLength, maxLength)).Select(s => s[random.Next(s.Length)]).ToArray();
|
|
}
|
|
|
|
public static string NewToken()
|
|
{
|
|
return new string(GetRandomChars(AcConst.MinUserTokenLength, AcConst.MaxUserTokenLength));
|
|
}
|
|
|
|
public static string NewPassword()
|
|
{
|
|
return new string(GetRandomChars(AcConst.MinPasswordLength, AcConst.MaxPasswordLength));
|
|
}
|
|
}
|
|
} |