Random data
Last updated
Last updated
This class produces unpredictable, cryptographically secure random numbers. Using a predictable random number generator, such as , is insecure.
These functions should be used to randomly generate encryption keys, nonces, salts, seeds, integers, strings, and passphrases.
Fills a span with random bytes.
buffer
has a length of 0.
Generates a random integer between 0 (inclusive) and the upper bound (exclusive).
upperBound
is less than MinUpperBound
.
Generates a random string of a given length. A custom character set can be provided, but several character sets are available via constants.
length
is less than MinStringLength
or greater than MaxStringLength
.
characterSet
is null.
characterSet
has a length of 0.
wordCount
is less than MinWordCount
or greater than MaxWordCount
.
Fills a span with deterministic bytes indistinguishable from random without knowing the seed.
buffer
has a length of 0.
seed
has a length not equal to SeedSize
.
These are used for validation and/or save you defining your own constants.
If these functions are called inside a virtual machine (VM) which is snapshotted and restored, the same output may be produced.
Generates a random passphrase using the (minus hyphenated words).
This should be reserved for tests and custom constructions (e.g. an ).
The libsodium library uses RtlGenRandom()
on Windows and getrandom
or /dev/urandom
on Linux and macOS to generate cryptographically secure random numbers non-deterministically. Deterministic generation is done using the IETF version of .