Skip to content

More RNG.Fill() and UnixEpoch usage #18132

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 1 commit into from
Jan 6, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ namespace Microsoft.AspNetCore.DataProtection.Managed
{
internal unsafe sealed class ManagedGenRandomImpl : IManagedGenRandom
{
#if NETSTANDARD2_0
private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create();
#endif
public static readonly ManagedGenRandomImpl Instance = new ManagedGenRandomImpl();

private ManagedGenRandomImpl()
Expand All @@ -18,7 +20,11 @@ private ManagedGenRandomImpl()
public byte[] GenRandom(int numBytes)
{
var bytes = new byte[numBytes];
#if NETSTANDARD2_0
_rng.GetBytes(bytes);
#else
RandomNumberGenerator.Fill(bytes);
#endif
return bytes;
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/Identity/Extensions.Core/src/Rfc6238AuthenticationService.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Diagnostics;
using System.Net;
using System.Security.Cryptography;
using System.Text;

namespace Microsoft.AspNetCore.Identity
{
using System;
using System.Text;

internal static class Rfc6238AuthenticationService
{
private static readonly DateTime _unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private static readonly TimeSpan _timestep = TimeSpan.FromMinutes(3);
private static readonly Encoding _encoding = new UTF8Encoding(false, true);
#if NETSTANDARD2_0
private static readonly DateTime _unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create();
#endif

// Generates a new 80-bit security token
public static byte[] GenerateRandomKey()
{
byte[] bytes = new byte[20];
#if NETSTANDARD2_0
_rng.GetBytes(bytes);
#else
RandomNumberGenerator.Fill(bytes);
#endif
return bytes;
}

Expand Down Expand Up @@ -63,7 +68,11 @@ private static byte[] ApplyModifier(byte[] input, string modifier)
// More info: https://tools.ietf.org/html/rfc6238#section-4
private static ulong GetCurrentTimeStepNumber()
{
#if NETSTANDARD2_0
var delta = DateTime.UtcNow - _unixEpoch;
#else
var delta = DateTimeOffset.UtcNow - DateTimeOffset.UnixEpoch;
#endif
return (ulong)(delta.Ticks / _timestep.Ticks);
}

Expand Down
6 changes: 6 additions & 0 deletions src/Identity/Extensions.Core/src/UserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public class UserManager<TUser> : IDisposable where TUser : class

private TimeSpan _defaultLockout = TimeSpan.Zero;
private bool _disposed;
#if NETSTANDARD2_0
private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create();
#endif
private IServiceProvider _services;

/// <summary>
Expand Down Expand Up @@ -2428,7 +2430,11 @@ private IUserRoleStore<TUser> GetUserRoleStore()
private static string NewSecurityStamp()
{
byte[] bytes = new byte[20];
#if NETSTANDARD2_0
_rng.GetBytes(bytes);
#else
RandomNumberGenerator.Fill(bytes);
#endif
return Base32.ToBase32(bytes);
}

Expand Down