Skip to content

Use RandomNumberGenerator.Fill() #18128

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Antiforgery/src/Internal/BinaryBlob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace Microsoft.AspNetCore.Antiforgery
[DebuggerDisplay("{DebuggerString}")]
internal sealed class BinaryBlob : IEquatable<BinaryBlob>
{
private static readonly RandomNumberGenerator _randomNumberGenerator = RandomNumberGenerator.Create();
private readonly byte[] _data;

// Generates a new token using a specified bit length.
Expand Down Expand Up @@ -92,7 +91,7 @@ public override int GetHashCode()
private static byte[] GenerateNewToken(int bitLength)
{
var data = new byte[bitLength / 8];
_randomNumberGenerator.GetBytes(data);
RandomNumberGenerator.Fill(data);
return data;
}

Expand Down
5 changes: 1 addition & 4 deletions src/Antiforgery/test/DefaultAntiforgeryTokenGeneratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,7 @@ public void GenerateRequestToken_ClaimsBasedIdentity()
httpContext.User = new ClaimsPrincipal(identity);

byte[] data = new byte[256 / 8];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(data);
}
RandomNumberGenerator.Fill(data);
var base64ClaimUId = Convert.ToBase64String(data);
var expectedClaimUid = new BinaryBlob(256, data);

Expand Down
3 changes: 1 addition & 2 deletions src/Components/Server/src/Circuits/CircuitIdFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ internal class CircuitIdFactory
private const int SecretLength = 64;
private const int IdLength = 32;

private readonly RandomNumberGenerator _generator = RandomNumberGenerator.Create();
private readonly IDataProtector _protector;

public CircuitIdFactory(IDataProtectionProvider provider)
Expand All @@ -35,7 +34,7 @@ public CircuitIdFactory(IDataProtectionProvider provider)
public CircuitId CreateCircuitId()
{
var buffer = new byte[SecretLength];
_generator.GetBytes(buffer);
RandomNumberGenerator.Fill(buffer);

var id = new byte[IdLength];
Array.Copy(
Expand Down
3 changes: 1 addition & 2 deletions src/Hosting/TestHost/src/WebSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ public async Task<WebSocket> ConnectAsync(Uri uri, CancellationToken cancellatio
private string CreateRequestKey()
{
byte[] data = new byte[16];
var rng = RandomNumberGenerator.Create();
rng.GetBytes(data);
RandomNumberGenerator.Fill(data);
return Convert.ToBase64String(data);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Middleware/Session/src/DistributedSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace Microsoft.AspNetCore.Session
{
public class DistributedSession : ISession
{
private static readonly RandomNumberGenerator CryptoRandom = RandomNumberGenerator.Create();
private const int IdByteCount = 16;

private const byte SerializationRevision = 2;
Expand Down Expand Up @@ -104,7 +103,7 @@ private byte[] IdBytes
if (IsAvailable && _sessionIdBytes == null)
{
_sessionIdBytes = new byte[IdByteCount];
CryptoRandom.GetBytes(_sessionIdBytes);
RandomNumberGenerator.Fill(_sessionIdBytes);
}
return _sessionIdBytes;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Middleware/Session/src/SessionMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace Microsoft.AspNetCore.Session
/// </summary>
public class SessionMiddleware
{
private static readonly RandomNumberGenerator CryptoRandom = RandomNumberGenerator.Create();
private const int SessionKeyLength = 36; // "382c74c3-721d-4f34-80e5-57657b6cbc27"
private static readonly Func<bool> ReturnTrue = () => true;
private readonly RequestDelegate _next;
Expand Down Expand Up @@ -91,7 +90,7 @@ public async Task Invoke(HttpContext context)
{
// No valid cookie, new session.
var guidBytes = new byte[16];
CryptoRandom.GetBytes(guidBytes);
RandomNumberGenerator.Fill(guidBytes);
sessionKey = new Guid(guidBytes).ToString();
cookieValue = CookieProtection.Protect(_dataProtector, sessionKey);
var establisher = new SessionEstablisher(context, cookieValue, _options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public abstract class RemoteAuthenticationHandler<TOptions> : AuthenticationHand
private const string CorrelationMarker = "N";
private const string AuthSchemeKey = ".AuthScheme";

private static readonly RandomNumberGenerator CryptoRandom = RandomNumberGenerator.Create();

protected string SignInScheme => Options.SignInScheme;

/// <summary>
Expand Down Expand Up @@ -194,7 +192,7 @@ protected virtual void GenerateCorrelationId(AuthenticationProperties properties
}

var bytes = new byte[32];
CryptoRandom.GetBytes(bytes);
RandomNumberGenerator.Fill(bytes);
var correlationId = Base64UrlTextEncoder.Encode(bytes);

var cookieOptions = Options.CorrelationCookie.Build(Context, Clock.UtcNow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ namespace Microsoft.AspNetCore.Authentication.MicrosoftAccount
{
public class MicrosoftAccountHandler : OAuthHandler<MicrosoftAccountOptions>
{
private static readonly RandomNumberGenerator CryptoRandom = RandomNumberGenerator.Create();

public MicrosoftAccountHandler(IOptionsMonitor<MicrosoftAccountOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
: base(options, logger, encoder, clock)
{ }
Expand Down Expand Up @@ -64,7 +62,7 @@ protected override string BuildChallengeUrl(AuthenticationProperties properties,
if (Options.UsePkce)
{
var bytes = new byte[32];
CryptoRandom.GetBytes(bytes);
RandomNumberGenerator.Fill(bytes);
var codeVerifier = Base64UrlTextEncoder.Encode(bytes);

// Store this for use during the code redemption.
Expand Down
3 changes: 1 addition & 2 deletions src/Security/Authentication/OAuth/src/OAuthHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace Microsoft.AspNetCore.Authentication.OAuth
{
public class OAuthHandler<TOptions> : RemoteAuthenticationHandler<TOptions> where TOptions : OAuthOptions, new()
{
private static readonly RandomNumberGenerator CryptoRandom = RandomNumberGenerator.Create();
protected HttpClient Backchannel => Options.Backchannel;

/// <summary>
Expand Down Expand Up @@ -274,7 +273,7 @@ protected virtual string BuildChallengeUrl(AuthenticationProperties properties,
if (Options.UsePkce)
{
var bytes = new byte[32];
CryptoRandom.GetBytes(bytes);
RandomNumberGenerator.Fill(bytes);
var codeVerifier = Base64UrlTextEncoder.Encode(bytes);

// Store this for use during the code redemption.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public class OpenIdConnectHandler : RemoteAuthenticationHandler<OpenIdConnectOpt
private const string NonceProperty = "N";
private const string HeaderValueEpocDate = "Thu, 01 Jan 1970 00:00:00 GMT";

private static readonly RandomNumberGenerator CryptoRandom = RandomNumberGenerator.Create();

private OpenIdConnectConfiguration _configuration;

protected HttpClient Backchannel => Options.Backchannel;
Expand Down Expand Up @@ -371,7 +369,7 @@ private async Task HandleChallengeAsyncInternal(AuthenticationProperties propert
if (Options.UsePkce && Options.ResponseType == OpenIdConnectResponseType.Code)
{
var bytes = new byte[32];
CryptoRandom.GetBytes(bytes);
RandomNumberGenerator.Fill(bytes);
var codeVerifier = Base64UrlTextEncoder.Encode(bytes);

// Store this for use during the code redemption. See RunAuthorizationCodeReceivedEventAsync.
Expand Down