diff --git a/PeyrSharp.Core/Password.cs b/PeyrSharp.Core/Password.cs index 98811a3..a4ba045 100644 --- a/PeyrSharp.Core/Password.cs +++ b/PeyrSharp.Core/Password.cs @@ -82,6 +82,40 @@ public static Task GenerateAsync(int length, string chars, string separa return task; } + /// + /// Asynchronously generates a string of a specified length using a given set of characters. + /// + /// The length of the string to generate. + /// An array of characters to use for generating the string. + /// A task that represents the asynchronous operation. The task result contains the generated string. + /// Throws an exception if the length parameter is not greater than 0. + public static Task GenerateAsync(int length, string[] chars) + { + Task task = new(() => + { + // Setup variables + string finalPassword = ""; + Random random = new(); + int number = 0; + + if (length > 0) // Check if the length valid + { + for (int i = 1; i < length; i++) // Generate a password of a specific length + { + number = random.Next(0, chars.Length); + finalPassword += chars[number]; // Append a character to the string + } + } + else + { + throw new Exception("The parameter 'length' (int) must be higher than 0."); + } + return finalPassword; + }); + task.Start(); + return task; + } + /// /// Generates a password asynchronously. ///