-
Notifications
You must be signed in to change notification settings - Fork 731
Add PasswordUtil.GeneratePassword
#1469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,4 +6,47 @@ namespace Aspire.Hosting.Utils; | |
| internal static class PasswordUtil | ||
| { | ||
| internal static string EscapePassword(string password) => password.Replace("\"", "\"\""); | ||
|
|
||
| /// <summary> | ||
| /// Returns a random password of length <paramref name="length"/> that does not contain any characters that would be escaped by <see cref="EscapePassword(string)"/>. | ||
| /// </summary> | ||
| /// <remarks>If <paramref name="length"/> is greater than or equal to four, the password is guaranteed to contain an uppercase ASCII letter, a lowercase ASCII letter, a digit, and a symbol.</remarks> | ||
| internal static string GeneratePassword(int length = 20) | ||
| { | ||
| return string.Create(length, 0, (buffer, _) => | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this password generation might be a problematic. Imagine that we set a password length of 6. We would already know that the first character is going to be in the range A-Z, second a-z, third 0-9, and forth one of the specials. Obviously, that becomes less of an issue the longer you get. |
||
| { | ||
| if (length >= 1) | ||
| { | ||
| // add an uppercase ASCII letter | ||
| Random.Shared.GetItems(PasswordChars.Slice(0, 26), buffer[..1]); | ||
|
|
||
| if (length >= 2) | ||
| { | ||
| // add a lowercase ASCII letter | ||
| Random.Shared.GetItems(PasswordChars.Slice(26, 26), buffer[1..2]); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect range? |
||
|
|
||
| if (length >= 3) | ||
| { | ||
| // add a digit | ||
| Random.Shared.GetItems(PasswordChars.Slice(52, 10), buffer[2..3]); | ||
|
|
||
| if (length >= 4) | ||
| { | ||
| // add a symbol | ||
| Random.Shared.GetItems(PasswordChars.Slice(62), buffer[3..4]); | ||
|
|
||
| if (length >= 5) | ||
| { | ||
| // use random password characters for the rest of the password | ||
| Random.Shared.GetItems(PasswordChars, buffer[4..]); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // Characters to use in a password that do not need to be escaped. They should be in the order: UPPER, lower, digits, symbols | ||
| private static ReadOnlySpan<char> PasswordChars => "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmonpqrstuvwxyz0123456789!@$^&*()[]{}':,._-"; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How cryptographically secure is this implementation? Writing a good password generator is probably more complicated than writing own cache.
There are quite a few related Qs on the web, e.g., https://stackoverflow.com/questions/54991/generating-random-passwords.
Also, having this implementation static essentially forces it for everyone. Shouldn't it be instead be injectable and replaceable?