libsodium_randombytes (libsodium v3.0.1)

View Source

Unpredictable 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

Types

A randombytes_SEEDBYTES (32-byte) binary.

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

seed()

-type seed() :: <<_:256>>.

A randombytes_SEEDBYTES (32-byte) binary.

Functions

buf(Size)

-spec buf(Size) -> Result when Size :: non_neg_integer(), Result :: binary().

Return Size random bytes.

buf_deterministic(Size, Seed)

-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.

close()

-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.

implementation_name()

-spec implementation_name() -> Result when Result :: atom().

Return the name of the active random number generator implementation, such as sysrandom.

random()

-spec random() -> Result when Result :: 0..4294967295.

Return a random integer between 0 and 16#ffffffff (inclusive).

randombytes(Size)

-spec randombytes(Size) -> Result when Size :: non_neg_integer(), Result :: binary().

Return Size random bytes; equivalent to buf/1.

seedbytes()

-spec seedbytes() -> Result when Result :: 32.

Return randombytes_seedbytes(), in bytes.

set_implementation(Impl)

-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.

stir()

-spec stir() -> Result when Result :: ok.

Reseed the generator. Rarely needed; libsodium reseeds automatically after a fork.

uniform(UpperBound)

-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.