Early Preview
This is currently very much a preview. Please feel free to try things out,
but don't be upset if anything is not yet working. Feedback is welcome over on our
GitHub Dicussions page.
class System.​Security.​Cryptography.​RandomNumberGenerator
Assembly: System.Security.Cryptography
Inheritance: object → RandomNumberGenerator
Implemented Interfaces
Provides functionality for generating random values.
Methods
public static
RandomNumberGenerator
Create​()
Creates an instance of the default implementation of a cryptographic random number generator that can be used to generate random data.
Returns A new instance of a cryptographic random number generator.
public static
RandomNumberGenerator
Create​(string rngName)
Creates an instance of the specified implementation of a cryptographic random number generator.
Returns A new instance of a cryptographic random number generator.
rngName
The name of the random number generator implementation to use.
public
void
Dispose​()
When overridden in a derived class, releases all resources used by the current instance of the <see cref="T:System.Security.Cryptography.RandomNumberGenerator" /> class.
protected
void
Dispose​(bool disposing)
When overridden in a derived class, releases the unmanaged resources used by the <see cref="T:System.Security.Cryptography.RandomNumberGenerator" /> and optionally releases the managed resources.
disposing
<see langword="true" /> to release both managed and unmanaged resources; <see langword="false" /> to release only unmanaged resources.
public static
void
Fill​(Span<​byte> data)
Fills a span with cryptographically strong random bytes.
data
The span to fill with cryptographically strong random bytes.
public
void
GetBytes​(byte[] data)
When overridden in a derived class, fills an array of bytes with a cryptographically strong random sequence of values.
data
The array to fill with cryptographically strong random bytes.
public
void
GetBytes​(byte[] data,
int offset,
int count)
Fills the specified byte array with a cryptographically strong random sequence of values.
data
The array to fill with cryptographically strong random bytes.
offset
The index of the array to start the fill operation.
count
The number of bytes to fill.
public static
byte[]
GetBytes​(int count)
Creates an array of bytes with a cryptographically strong random sequence of values.
Returns An array populated with cryptographically strong random values.
count
The number of bytes of random values to create.
public
void
GetBytes​(Span<​byte> data)
Fills a span with cryptographically strong random bytes.
data
The span to fill with cryptographically strong random bytes.
public static
string
GetHexString​(int stringLength,
bool lowercase = false)
Creates a string filled with cryptographically random hexadecimal characters.
Returns A string populated with random hexadecimal characters.
stringLength
The length of string to create.
lowercase
<see langword="true" /> if the hexadecimal characters should be lowercase; <see langword="false" /> if they should be uppercase.
The default is <see langword="false" /> .
public static
void
GetHexString​(Span<​char> destination,
bool lowercase = false)
Fills a buffer with cryptographically random hexadecimal characters.
destination
The buffer to receive the characters.
lowercase
<see langword="true" /> if the hexadecimal characters should be lowercase; <see langword="false" /> if they should be uppercase.
The default is <see langword="false" /> .
public static
int
GetInt32​(int toExclusive)
Generates a random integer between 0 (inclusive) and a specified exclusive upper bound using a cryptographically strong random number generator.
Returns A random integer between 0 (inclusive) and <paramref name="toExclusive" /> (exclusive).
toExclusive
The exclusive upper bound of the random range.
public static
int
GetInt32​(int fromInclusive,
int toExclusive)
Generates a random integer between a specified inclusive lower bound and a specified exclusive upper bound using a cryptographically strong random number generator.
Returns A random integer between <paramref name="fromInclusive" /> (inclusive) and <paramref name="toExclusive" /> (exclusive).
fromInclusive
The inclusive lower bound of the random range.
toExclusive
The exclusive upper bound of the random range.
public static
T[]
GetItems​(ReadOnlySpan<​T> choices,
int length)
Creates an array populated with items chosen at random from choices.
Returns An array populated with random choices.
choices
The items to use to populate the array.
length
The length of array to return populated with items.
public static
void
GetItems​(ReadOnlySpan<​T> choices,
Span<​T> destination)
Fills the elements of a specified span with items chosen at random from the provided set of choices.
choices
The items to use to fill the buffer.
destination
The buffer to receive the items.
public
void
GetNonZeroBytes​(byte[] data)
When overridden in a derived class, fills an array of bytes with a cryptographically strong random sequence of nonzero values.
data
The array to fill with cryptographically strong random nonzero bytes.
public
void
GetNonZeroBytes​(Span<​byte> data)
Fills a byte span with a cryptographically strong random sequence of nonzero values.
data
The span to fill with cryptographically strong random nonzero bytes.
public static
string
GetString​(ReadOnlySpan<​char> choices,
int length)
Creates a string populated with characters chosen at random from choices.
Returns A string populated with random choices.
choices
The characters to use to populate the string.
length
The length of string to return.
public static
void
Shuffle​(Span<​T> values)
Performs an in-place shuffle of a span using cryptographically random number generation.
values
The span to shuffle.
public
bool
Equals​(object obj)
Inherited from object
protected
void
Finalize​()
Inherited from object
public
int
GetHashCode​()
Inherited from object
protected
object
MemberwiseClone​()
Inherited from object
public
string
ToString​()
Inherited from object