-
Notifications
You must be signed in to change notification settings - Fork 5k
Implement Shuffle, GetString, and GetItems on Random{NumberGenerator} #78598
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
Conversation
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
Tagging subscribers to this area: @dotnet/area-system-security, @vcsjones Issue DetailsImplements:
This does both Closes #73864.
|
Can you search around the repo and use these where it makes sense, e.g. Line 179 in 6251d49
etc? |
A quick search suggests other opportunities, e.g.
|
I excluded searching in |
I checked in a few more. Several of them I left unchanged because they have configurations that build for NetCoreAppMinimum or NetFramework. It didn't make sense to |
...aries/System.Security.Cryptography/src/System/Security/Cryptography/RandomNumberGenerator.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Benjamin Moir <[email protected]> Co-authored-by: Theodore Tsirpanis <[email protected]> Co-authored-by: Michał Petryka <[email protected]>
3c6aa37
to
d87b234
Compare
Sorry for the force push. I goofed on |
...braries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/TrustHelper.cs
Show resolved
Hide resolved
...aries/System.Security.Cryptography/src/System/Security/Cryptography/RandomNumberGenerator.cs
Outdated
Show resolved
Hide resolved
...aries/System.Security.Cryptography/src/System/Security/Cryptography/RandomNumberGenerator.cs
Outdated
Show resolved
Hide resolved
...aries/System.Security.Cryptography/src/System/Security/Cryptography/RandomNumberGenerator.cs
Show resolved
Hide resolved
Co-authored-by: Dan Moseley <[email protected]>
@vcsjones, when you get a chance, could you take a look at the pending comments/questions? Then we can get this merged. Thanks. |
...aries/System.Security.Cryptography/src/System/Security/Cryptography/RandomNumberGenerator.cs
Outdated
Show resolved
Hide resolved
for (int i = 0; i < destination.Length; i++) | ||
{ | ||
destination[i] = choices[GetInt32(choices.Length)]; | ||
} |
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.
Similar to the N virtual calls concern in System.Random, it might be useful to change this to do something like
Span<byte> randomBytes = stackalloc byte[128];
Span<int> randomIntStorage = stackalloc int[randomBytes.Length / sizeof(int)];
int randomInts = 0;
for (int i = destination.Length - 1; i >= 0; i--)
{
while (randomInts <= 0)
{
if (i < randomIntStorage.Length)
{
randomBytes = randomBytes.Slice(0, i * sizeof(int));
}
Fill(randomBytes);
randomInts = FilterInt32(randomBytes, randomIntStorage);
}
randomInts--;
destination[i] = choices[randomInts[randomInts]];
}
to reduce P/Invokes by a factor of randomIntStorage.Length.
Though that requires writing FilterInt32, and the complexity of above, so it should be checked to see if the perf justifies it.
(This is an idea for later, not a "right now")
…/Cryptography/RandomNumberGenerator.cs Co-authored-by: Jeremy Barton <[email protected]>
Very nice APIs! I wonder if we should have |
Concept seems reasonable, though to nit pick, to match the other APIs we'd probably want it to be called GetItem rather than PickRandom. Can you open a separate issue? Are there places it would be used in ASP.NET? I assume this is essentially shorthand for: |
I can.
Not sure, I was building something with YARP when this came up. Also would be used here
Yep! |
Filed an issue here |
Implements:
Random.GetItems
Random.Shuffle
RandomNumberGenerator.GetString
RandomNumberGenerator.GetHexString
RandomNumberGenerator.Shuffle
This does both
RandomNumberGenerator
andRandom
in one go. If folks prefer I can split the PR.Closes #73864.