Skip to content
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 @@ -7,6 +7,15 @@ namespace System.Security.Cryptography
{
internal sealed partial class RandomNumberGeneratorImplementation : RandomNumberGenerator
{
// a singleton which always calls into a thread-safe implementation
// and whose Dispose method no-ops
internal static readonly RandomNumberGeneratorImplementation s_singleton = new RandomNumberGeneratorImplementation();

// private ctor used only by singleton
private RandomNumberGeneratorImplementation()
{
}

// As long as each implementation can provide a static GetBytes(ref byte buf, int length)
// they can share this one implementation of FillSpan.
internal static unsafe void FillSpan(Span<byte> data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ public abstract class RandomNumberGenerator : IDisposable
{
protected RandomNumberGenerator() { }

public static RandomNumberGenerator Create()
{
return new RandomNumberGeneratorImplementation();
}
public static RandomNumberGenerator Create() => RandomNumberGeneratorImplementation.s_singleton;

[UnsupportedOSPlatform("browser")]
[RequiresUnreferencedCode(CryptoConfig.CreateFromNameUnreferencedCodeMessage)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ namespace System.Security.Cryptography.RNG.Tests
{
public class RandomNumberGeneratorTests
{
[Fact]
public static void Create_ReturnsSingleton()
{
RandomNumberGenerator rng1 = RandomNumberGenerator.Create();
RandomNumberGenerator rng2 = RandomNumberGenerator.Create();

Assert.Same(rng1, rng2);
}

[Fact]
public static void Singleton_NoopsDispose()
{
byte[] random = new byte[1024];
RandomNumberGenerator rng = RandomNumberGenerator.Create();
rng.GetBytes(random);
RandomDataGenerator.VerifyRandomDistribution(random);
rng.Dispose(); // should no-op if called once

random = new byte[1024];
rng.GetBytes(random); // should still work even after earlier Dispose call
RandomDataGenerator.VerifyRandomDistribution(random);
rng.Dispose(); // should no-op if called twice
}

[Theory]
[InlineData(2048)]
[InlineData(65536)]
Expand Down