The documentation is currently being reviewed, so you may see some inconsistencies between sections.
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Random data

Purpose

This class produces unpredictable, cryptographically secure random numbers. Using a predictable random number generator, such as System.Random, is insecure.

These functions should be used to randomly generate encryption keys, nonces, salts, seeds, integers, strings, and passphrases.

Usage

Fill

Fills a span with random bytes.

SecureRandom.Fill(Span<byte> buffer);

Exceptions

ArgumentOutOfRangeException

buffer.Length is equal to 0.

FillDeterministic

Fills a span with deterministic bytes indistinguishable from random without knowing the seed.

SecureRandom.FillDeterministic(Span<byte> buffer, ReadOnlySpan<byte> seed);

Exceptions

ArgumentOutOfRangeException

buffer.Length is equal to 0.

ArgumentOutOfRangeException

seed.Length is not equal to SeedSize.

GetInt32

Returns a random integer between 0 (inclusive) and the upper bound (exclusive).

Exceptions

ArgumentOutOfRangeException

upperBound is less than MinUpperBound.

GenerateString

Fills a span with a random string based on the provided character set.

See the constants for some included character sets.

Exceptions

ArgumentOutOfRangeException

buffer.Length is less than MinStringSize or greater than MaxStringSize.

ArgumentOutOfRangeException

characterSet.Length is less than MinCharacterSetSize.

GetPassphraseBufferSize

Returns the required buffer size for GeneratePassphrase(). The length of the longest word in the wordlist being used must be known (see LongestWordSize for examples).

Exceptions

ArgumentOutOfRangeException

longestWord is less than MinLongestWordSize or greater than MaxLongestWordSize.

ArgumentOutOfRangeException

wordCount is less than MinWordCount or greater than MaxWordCount.

GeneratePassphrase

Fills a span with a random passphrase using the EFF's long wordlist (minus hyphenated words).

Exceptions

ArgumentOutOfRangeException

buffer.Length is not equal to GetPassphraseBufferSize().

ArgumentOutOfRangeException

wordCount is less than MinWordCount or greater than MaxWordCount.

ArgumentException

separatorChar is not a printable character.

GeneratePassphrase

Fills a span with a random passphrase using a custom wordlist.

Exceptions

ArgumentOutOfRangeException

buffer.Length is not equal to GetPassphraseBufferSize().

ArgumentOutOfRangeException

wordlist.Length is less than MinWordlistSize.

FormatException

wordlist contains empty words, spaces, or non-printable characters.

ArgumentOutOfRangeException

The longest word in the wordlist is less than MinLongestWordSize or greater than MaxLongestWordSize.

ArgumentOutOfRangeException

wordCount is less than MinWordCount or greater than MaxWordCount.

ArgumentException

separatorChar is not a printable character.

GetWordlist

Returns the built-in EFF long wordlist (minus hyphenated words) formatted correctly for GeneratePassphrase().

Exceptions

N/A

Constants

These are used for validation and/or save you from defining your own constants.

Notes

The libsodium library uses RtlGenRandom() on Windows and getrandom or /dev/urandom on Linux and macOS to generate cryptographically secure, non-deterministic random numbers. Deterministic generation is done using the IETF version of ChaCha20 with a hardcoded nonce.

Last updated