libsodium_randombytes (libsodium v3.0.1)
View SourceUnpredictable random data from libsodium's cryptographically secure generator, which uses the operating system's random source.
buf/1 and randombytes/1 return binaries of the requested length;
random/0 and uniform/1 return 32-bit integers. Requests for more than
64 KiB run on a dirty CPU scheduler.
set_implementation/1 changes the generator for the whole VM and should
only be called during startup, before any other libsodium function.
Bytes = libsodium_randombytes:buf(32),
32 = byte_size(Bytes),
Roll = libsodium_randombytes:uniform(6),
true = Roll >= 0 andalso Roll < 6.
Summary
Functions
Return Size random bytes.
Return Size bytes deterministically derived from the seedbytes/0 byte
Seed. The same seed always produces the same output.
Release resources held by the generator (such as an open
/dev/urandom descriptor). Returns 0 on success or -1 on failure.
Return the name of the active random number generator implementation, such as sysrandom.
Return a random integer between 0 and 16#ffffffff (inclusive).
Return Size random bytes; equivalent to buf/1.
Return randombytes_seedbytes(), in bytes.
Select the generator implementation for the whole VM: sysrandom uses the
operating system directly, while internal (also called salsa20) uses
a ChaCha20-based generator seeded from it. Returns 0 on success.
Reseed the generator. Rarely needed; libsodium reseeds automatically after a fork.
Return a uniformly distributed random integer between 0 and
UpperBound - 1 (inclusive), avoiding modulo bias. Returns 0 when
UpperBound is less than 2.
Types
Functions
-spec buf(Size) -> Result when Size :: non_neg_integer(), Result :: binary().
Return Size random bytes.
-spec buf_deterministic(Size, Seed) -> Result when Size :: non_neg_integer(), Seed :: seed(), Result :: binary().
Return Size bytes deterministically derived from the seedbytes/0 byte
Seed. The same seed always produces the same output.
-spec close() -> Result when Result :: 0 | -1.
Release resources held by the generator (such as an open
/dev/urandom descriptor). Returns 0 on success or -1 on failure.
-spec implementation_name() -> Result when Result :: atom().
Return the name of the active random number generator implementation, such as sysrandom.
-spec random() -> Result when Result :: 0..4294967295.
Return a random integer between 0 and 16#ffffffff (inclusive).
-spec randombytes(Size) -> Result when Size :: non_neg_integer(), Result :: binary().
Return Size random bytes; equivalent to buf/1.
-spec seedbytes() -> Result when Result :: 32.
Return randombytes_seedbytes(), in bytes.
-spec set_implementation(Impl) -> Result when Impl :: internal | salsa20 | sysrandom, Result :: 0 | -1.
Select the generator implementation for the whole VM: sysrandom uses the
operating system directly, while internal (also called salsa20) uses
a ChaCha20-based generator seeded from it. Returns 0 on success.
-spec stir() -> Result when Result :: ok.
Reseed the generator. Rarely needed; libsodium reseeds automatically after a fork.
-spec uniform(UpperBound) -> Result when UpperBound :: 0..4294967295, Result :: 0..4294967294.
Return a uniformly distributed random integer between 0 and
UpperBound - 1 (inclusive), avoiding modulo bias. Returns 0 when
UpperBound is less than 2.