AcCharsGenerator.NewPassword improvements

This commit is contained in:
Loretta 2024-07-06 07:22:26 +02:00
parent 8e8ec873b2
commit de2c345b46
2 changed files with 5 additions and 4 deletions

View File

@ -25,7 +25,7 @@ namespace AyCode.Core.Consts
public const int MinPlayerNameLength = MinUserNameLength; public const int MinPlayerNameLength = MinUserNameLength;
public const int MaxPlayerNameLength = MaxUserNameLength; public const int MaxPlayerNameLength = MaxUserNameLength;
public const int MinPasswordLength = 6; public const int MinPasswordLength = 8;
public const int MaxPasswordLength = 32; public const int MaxPasswordLength = 32;
public const int MinUserTokenLength = 8; public const int MinUserTokenLength = 8;
public const int MaxUserTokenLength = 12; public const int MaxUserTokenLength = 12;

View File

@ -2,13 +2,13 @@
namespace AyCode.Core.Helpers namespace AyCode.Core.Helpers
{ {
public static class AcCharGenerator public static class AcCharsGenerator
{ {
public static readonly char[] Letters; public static readonly char[] Letters;
public static readonly char[] Numbers; public static readonly char[] Numbers;
public static readonly char[] LettersAndNumbers; public static readonly char[] LettersAndNumbers;
static AcCharGenerator() static AcCharsGenerator()
{ {
//"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" //"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
@ -23,6 +23,7 @@ namespace AyCode.Core.Helpers
private static char[] GetRandomChars(int minLength, int maxLength) => GetRandomChars(LettersAndNumbers, minLength, maxLength); private static char[] GetRandomChars(int minLength, int maxLength) => GetRandomChars(LettersAndNumbers, minLength, maxLength);
private static char[] GetRandomChars(char[] sourceChars, int minLength, int maxLength) private static char[] GetRandomChars(char[] sourceChars, int minLength, int maxLength)
{ {
if (minLength < 1) throw new ArgumentOutOfRangeException(nameof(minLength), "must have length greater than or equal to 1");
var random = new Random(); var random = new Random();
return Enumerable.Repeat(sourceChars, random.Next(minLength, maxLength)).Select(s => s[random.Next(s.Length)]).ToArray(); return Enumerable.Repeat(sourceChars, random.Next(minLength, maxLength)).Select(s => s[random.Next(s.Length)]).ToArray();
} }
@ -32,7 +33,7 @@ namespace AyCode.Core.Helpers
return new string(GetRandomChars(AcConst.MinUserTokenLength, AcConst.MaxUserTokenLength)); return new string(GetRandomChars(AcConst.MinUserTokenLength, AcConst.MaxUserTokenLength));
} }
public static string NewPassword() public static string NewPassword(int minLength = AcConst.MinPasswordLength, int maxLength = AcConst.MaxPasswordLength)
{ {
return new string(GetRandomChars(AcConst.MinPasswordLength, AcConst.MaxPasswordLength)); return new string(GetRandomChars(AcConst.MinPasswordLength, AcConst.MaxPasswordLength));
} }