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
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);This should be reserved for tests and custom constructions (e.g., an XOF).
Exceptions
buffer.Length is equal to 0.
seed.Length is not equal to SeedSize.
GetInt32
Returns a random integer between 0 (inclusive) and the upper bound (exclusive).
Exceptions
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
buffer.Length is less than MinStringSize or greater than MaxStringSize.
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
longestWord is less than MinLongestWordSize or greater than MaxLongestWordSize.
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).
buffer must be sliced using passphraseSize to remove padding.
Exceptions
buffer.Length is not equal to GetPassphraseBufferSize().
wordCount is less than MinWordCount or greater than MaxWordCount.
separatorChar is not a printable character.
GeneratePassphrase
Fills a span with a random passphrase using a custom wordlist.
buffer must be sliced using passphraseSize to remove padding.
View the source code for GetWordlist() to see how to format a custom wordlist. It is recommended to stick to English wordlists because this is what has been tested.
Exceptions
buffer.Length is not equal to GetPassphraseBufferSize().
wordlist.Length is less than MinWordlistSize.
wordlist contains empty words, spaces, or non-printable characters.
The longest word in the wordlist is less than MinLongestWordSize or greater than MaxLongestWordSize.
wordCount is less than MinWordCount or greater than MaxWordCount.
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
If the non-deterministic functions are called inside a virtual machine (VM) that has had a snapshot restored, the same output may be produced.
For random strings (e.g., passwords), the number of combinations is equal to the length of the character set raised to the power of the number of characters. For example, 52^8 for an 8-character password with uppercase/lowercase English letters.
For random passphrases, the number of combinations is equal to the length of the wordlist raised to the power of the number of words. For example, 7776^6 for a 6-word passphrase generated using a wordlist containing 7776 words.
In both cases, the entropy (in bits) is approximately log2() of the above. For instance, log2(7776^6) = 77.6 bits.
An acceptable minimum for passwords/passphrases is ~80 bits of entropy.
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