package bytesrw

  1. Overview
  2. Docs

Module Bytesrw_sysrandomSource

Cryptographically secure pseudorandom byte streams and entropy.

This module provides cryptographically secure pseudorandom bytes and an entropy primitive using operating system sources. See the documentation of the primitives for details on the sources.

Note. If you are already using Bytesrw_crypto rather use the randomness provided there.

Cryptographically secure pseurorandom bytes

Sourceval reads : ?pos:Bytesrw.Bytes.Stream.pos -> ?slice_length:Bytesrw.Bytes.Slice.length -> ?length:int -> unit -> Bytesrw.Bytes.Reader.t

reads () is a stream of length, or unbounded if unspecified, cryptographically secure pseudorandom bytes in slice_length chunks (defaults to Bytesrw.Bytes.Slice.default_length).

Reads may raise Panic which is not turned into a stream error as it likely indicates a serious condition in the system, see the underlying call set_random.

Sourceval string : int -> string

string n are n cryptographically secure pseudorandom bytes. Raises Panic in case of problems, see the underlying call set_random.

Sourceval bytes : int -> bytes

bytes n are n cryptographically secure pseudorandom bytes. Raises Panic in case of problems, see the underlying call set_random.

Primitives

Sourceexception Panic of string

Exception raised by primitives in case of problem. If this happens do not try to handle the exception, log it at the toplevel of your program and abort the program. It likely indicates a serious condition in the system.

Cryptographically secure pseudorandom bytes

Sourceval set_random : Bytesrw.Bytes.Slice.t -> unit

set_random s writes the bytes in the slice range with cryptographically secure pseudorandom bytes. Theoretically this function should not block or error, except perhaps if you try to use it in early OS boot phase.

This uses:

Raises Panic in case of problem, not meant to be handled.

Entropy

Sourceval set_entropy : Bytesrw.Bytes.Slice.t -> unit

set_entropy s write the bytes in the slice range with entropy from your operating system. The function blocks until enough entropy is gathered. The Bytesrw.Bytes.Slice.length of s must be smaller or equal to 256 or Invalid_argument is raised.

This uses:

Raises Panic in case of problem, not meant to be handled.