-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: Random.Get{Hex}String #111956
Comments
Tagging subscribers to this area: @dotnet/area-system-runtime |
I want to push back a little against this statement, because I don't think it's universally true. A byte stream retrieved from The This isn't to say the API should be rejected. There are valid use cases, such as generating filenames in a trusted destination directory (emphasis on trusted since you don't want to implicitly rely on unpredictability). I'm just pushing back on the "it exists <here>, therefore it should exist <there>" justification as quoted above and I personally would like to see a stronger argument. |
There are plenty of uses, as you call out. Right now we make it harder to use the cheaper option, either pushing users to the cryptographically-secure api for convenience when it's not technically warranted, or making them write more complicated code. If there were zero uses, sure, but that's not the case, and my comment is in the context of there being valid uses. For both, GetHexString is a convenience method. |
namespace System;
public partial class Random
{
// same signatures as on RandomNumberGenerator, but instance instead of static
public string GetString(ReadOnlySpan<char> choices, int length);
public string GetHexString(int stringLength, bool lowercase = false);
public void GetHexString(Span<char> destination, bool lowercase = false);
} |
Background and motivation
We added GetString and GetHexString methods to RandomNumberGenerator. Random didn't get these methods as well, but they're useful there as well. There's little about these that are specific to something that's cryptographically-secure. Developers need to work around this by using string.Create and then Random.GetItems to fill the backing space. If the simple overloads were warranted for RandomNumberGenerator, they're just as applicable to Random.
API Proposal
API Usage
Alternative Designs
Making the members virtual. But other such members, including GetItems, that have been added recently haven't been virtual.
Risks
No response
The text was updated successfully, but these errors were encountered: