Table of Contents

Class RandomNumberGenerator

Namespace
CryptoHives.Foundation.Security.Cryptography.Rng
Assembly
CryptoHives.Foundation.Security.Cryptography.dll

Cryptographically strong random bytes from the operating system generator, with the span-based Fill(Span<byte>) available on every target framework, including those where RandomNumberGenerator has no span API.

public class RandomNumberGenerator : IDisposable
Inheritance
RandomNumberGenerator
Implements
Inherited Members

Remarks

It wraps the in-box generator rather than deriving from it, so it cannot be passed where an RandomNumberGenerator is expected. Importing both namespaces is CS0104; alias one side, e.g. using Bcl = System.Security.Cryptography;.

Methods

Create()

When overridden in a derived class, creates an instance of the default implementation of a cryptographic random number generator that can be used to generate random data.

public static RandomNumberGenerator Create()

Returns

RandomNumberGenerator

A new instance of a cryptographic random number generator.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Dispose(bool)

When overridden in a derived class, releases the unmanaged resources used by the RandomNumberGenerator and optionally releases the managed resources.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Fill(byte[])

Fills the specified array with cryptographically strong random bytes.

public static void Fill(byte[] data)

Parameters

data byte[]

The array to fill.

Remarks

Uses the same shared generator as Fill(Span<byte>).

Exceptions

ArgumentNullException

data is null.

Fill(Span<byte>)

Fills the specified span with cryptographically strong random bytes.

public static void Fill(Span<byte> data)

Parameters

data Span<byte>

The span to fill.

Remarks

Uses one generator shared by the process, so it needs no instance and is safe to call from several threads at once.

GetBytes(byte[])

When overridden in a derived class, fills an array of bytes with a cryptographically strong random sequence of values.

public void GetBytes(byte[] data)

Parameters

data byte[]

The array to fill with cryptographically strong random bytes.

Exceptions

ObjectDisposedException

The instance has been disposed.

GetBytes(byte[], int, int)

Fills the specified byte array with a cryptographically strong random sequence of values.

public void GetBytes(byte[] data, int offset, int count)

Parameters

data byte[]

The array to fill with cryptographically strong random bytes.

offset int

The index of the array to start the fill operation.

count int

The number of bytes to fill.

Exceptions

ArgumentNullException

data is null.

ArgumentOutOfRangeException

offset or count is less than 0

ArgumentException

offset plus count exceeds the length of data.

ObjectDisposedException

The instance has been disposed.

GetBytes(Span<byte>)

Fills a span with cryptographically strong random bytes.

public void GetBytes(Span<byte> data)

Parameters

data Span<byte>

The span to fill.

Remarks

Before .NET 6 the bytes are generated into a rented ArrayPool<T> array, which is cleared before it goes back to the pool.

Exceptions

ObjectDisposedException

The instance has been disposed.