Returns n random bytes that are suitable for keys, salts and
nonces. Use it whenever a value must be unguessable; use
set.seed() and runif() for simulations, where
reproducibility is the point.
Details
Reads the operating system's own random source rather than R's
Mersenne Twister: /dev/urandom on Unix and macOS, RtlGenRandom on
Windows.
This distinction matters for anything that becomes a key.
set.seed() makes R's generator reproducible BY
DESIGN, and its state can be recovered from its output; a key drawn from
it is guessable. Reading the OS source also leaves R's own random stream
untouched, so generating a key does not perturb a reproducible analysis.
If no OS source can be read the function FAILS rather than falling back to a weaker generator, because a silent downgrade in a key is worse than an error.
Errors: n not a single whole number from 1 to 1,048,576 (1 MiB);
no readable operating-system source.
Examples
rndbytes(8)
#> [1] 49 12 44 ce 9d dd b4 fb
# Independent between calls, unlike a seeded generator.
identical(rndbytes(16), rndbytes(16))
#> [1] FALSE
# R's own stream is not consumed, so a seeded analysis is unaffected.
set.seed(1)
a <- stats::runif(1)
set.seed(1)
invisible(rndbytes(32))
identical(stats::runif(1), a)
#> [1] TRUE
# As hex, for a seed argument.
paste(format(rndbytes(4)), collapse = "")
#> [1] "2dabc30f"
try(rndbytes(0))
#> Error : `n` must be a single integer between 1 and 1048576
