From de2c345b46dcc7aa913531f4c40f7af22490c5b6 Mon Sep 17 00:00:00 2001 From: Loretta Date: Sat, 6 Jul 2024 07:22:26 +0200 Subject: [PATCH] AcCharsGenerator.NewPassword improvements --- AyCode.Core/Consts/AcConst.cs | 2 +- .../Helpers/{AcCharGenerator.cs => AcCharsGenerator.cs} | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) rename AyCode.Core/Helpers/{AcCharGenerator.cs => AcCharsGenerator.cs} (81%) diff --git a/AyCode.Core/Consts/AcConst.cs b/AyCode.Core/Consts/AcConst.cs index 9d7a685..4851db8 100644 --- a/AyCode.Core/Consts/AcConst.cs +++ b/AyCode.Core/Consts/AcConst.cs @@ -25,7 +25,7 @@ namespace AyCode.Core.Consts public const int MinPlayerNameLength = MinUserNameLength; public const int MaxPlayerNameLength = MaxUserNameLength; - public const int MinPasswordLength = 6; + public const int MinPasswordLength = 8; public const int MaxPasswordLength = 32; public const int MinUserTokenLength = 8; public const int MaxUserTokenLength = 12; diff --git a/AyCode.Core/Helpers/AcCharGenerator.cs b/AyCode.Core/Helpers/AcCharsGenerator.cs similarity index 81% rename from AyCode.Core/Helpers/AcCharGenerator.cs rename to AyCode.Core/Helpers/AcCharsGenerator.cs index 8062a50..4acb704 100644 --- a/AyCode.Core/Helpers/AcCharGenerator.cs +++ b/AyCode.Core/Helpers/AcCharsGenerator.cs @@ -2,13 +2,13 @@ namespace AyCode.Core.Helpers { - public static class AcCharGenerator + public static class AcCharsGenerator { public static readonly char[] Letters; public static readonly char[] Numbers; public static readonly char[] LettersAndNumbers; - static AcCharGenerator() + static AcCharsGenerator() { //"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(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(); 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)); } - public static string NewPassword() + public static string NewPassword(int minLength = AcConst.MinPasswordLength, int maxLength = AcConst.MaxPasswordLength) { return new string(GetRandomChars(AcConst.MinPasswordLength, AcConst.MaxPasswordLength)); }