Runtime: API proposal: RandomNumberGenerator.GetBytes(int)

Created on 26 Sep 2020  路  8Comments  路  Source: dotnet/runtime

Motiviation

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.

API proposal

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;
        }
    }
}

Usage throughout the framework

(representative, non-exhaustive list)

https://github.com/dotnet/aspnetcore/blob/957b58c094156a98a51e959ebdaf9e0654d3ee7c/src/Antiforgery/src/Internal/BinaryBlob.cs#L93-L94

https://github.com/dotnet/aspnetcore/blob/957b58c094156a98a51e959ebdaf9e0654d3ee7c/src/Shared/CertificateGeneration/MacOSCertificateManager.cs#L106-L107

https://github.com/dotnet/aspnetcore/blob/957b58c094156a98a51e959ebdaf9e0654d3ee7c/src/Security/Authentication/Core/src/RemoteAuthenticationHandler.cs#L195-L196

https://github.com/dotnet/runtime/blob/932cc5e7e38098c75aba0596b6375a0ddba8a0d8/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/AesImplementation.cs#L33-L34

https://github.com/dotnet/runtime/blob/932cc5e7e38098c75aba0596b6375a0ddba8a0d8/src/libraries/System.Security.Cryptography.X509Certificates/src/System/Security/Cryptography/X509Certificates/CertificateRequest.cs#L311-L312

api-approved area-System.Security

Most helpful comment

runtime/src/libraries/System.Security.Cryptography.X509Certificates/src/System/Security/Cryptography/X509Certificates/CertificateRequest.cs

Check again.... Mwahahahahaha :-)

All 8 comments

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.

Video

  • Looks good as proposed
  • We decided we don't want an overload for GetNonZeroBytes() because we don't like that API to begin with

C# 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.

Was this page helpful?
0 / 5 - 0 ratings