Skip to content

Commit

Permalink
Added new overload for GenerateAsync() in Password (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpeyr committed Dec 2, 2023
1 parent 733dddb commit b80a637
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions PeyrSharp.Core/Password.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,40 @@ public static Task<string> GenerateAsync(int length, string chars, string separa
return task;
}

/// <summary>
/// Asynchronously generates a string of a specified length using a given set of characters.
/// </summary>
/// <param name="length">The length of the string to generate.</param>
/// <param name="chars">An array of characters to use for generating the string.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the generated string.</returns>
/// <exception cref="Exception">Throws an exception if the length parameter is not greater than 0.</exception>
public static Task<string> GenerateAsync(int length, string[] chars)
{
Task<string> 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;
}

/// <summary>
/// Generates a password asynchronously.
/// </summary>
Expand Down

0 comments on commit b80a637

Please sign in to comment.