A simple API for instantiating and populating a byte array from a CSPRNG. Saves the caller from having to allocate the array themselves, then call the Fill routine.
The caller would still use Fill if they wanted to populate an existing array instance or if they wanted to populate a manually-managed span.
namespace System.Security.Cryptography
{
public class RandomNumberGenerator
{
public static byte[] GetBytes(int byteCount)
{
// sample implementation, not showing argument checking
byte[] retVal = new byte[byteCount];
Fill(retVal);
return retVal;
}
}
}
(representative, non-exhaustive list)
Tagging subscribers to this area: @bartonjs, @vcsjones, @krwq, @jeffhandley
See info in area-owners.md if you want to be subscribed.
runtime/src/libraries/System.Security.Cryptography.X509Certificates/src/System/Security/Cryptography/X509Certificates/CertificateRequest.cs
Check again.... Mwahahahahaha :-)
public static byte[] GetBytes(int byteCount)
Seems reasonable. We have that same helper (though generally with new instead of AllocateUninitializedArray) in a number of places; it'd be nice to consolidate.
Yeah, AllocateUninitializedArray is probably unnecessary here. It's unlikely callers will pass arguments anywhere near the 2KB threshold to make that function worthwhile.
GetNonZeroBytes() because we don't like that API to begin withC#
namespace System.Security.Cryptography
{
public partial class RandomNumberGenerator
{
public static byte[] GetBytes(int byteCount);
}
}
@GrabYourPitchforks were you going to implement this or is it available?
@vcsjones, go for it :) Thanks! (As part of the addition, please also use it in places where it makes sense to do so.)
Thanks Kevin for jumping on it! :)
I've been busy with 5.0 release management stuff so I'm not really working on 6.0 feature work just yet.
Most helpful comment
Check again.... Mwahahahahaha :-)