Module sysrandom

A simple library to generate random data, using the system's PRNG.

The following sources of randomness are used depending on platform:

Procs

proc getRandomBytes(buf: pointer; len: int) {.
raises: [OSError], tags: []
.}
Fill a buffer buff with len random bytes.   Source Edit
proc getRandom(): uint32 {.
raises: [OSError], tags: []
.}
Generate a random value in the range 0 to 0xffffffff.   Source Edit
proc closeRandom() {.
raises: [], tags: []
.}

Close the source of randomness.

On systems such as OpenBSD, Windows and Linux (using getrandom()), this does nothing. On other Posix systems, it releases any resources associated with the generation of random numbers.

  Source Edit
proc getRandomBytes(len: static[int]): array[len, byte]
Generate an array of random bytes in the range 0 to 0xff of length len.   Source Edit
proc getRandomBytes(len: int): seq[byte] {.
raises: [OSError], tags: []
.}
Generate a seq of random bytes in the range 0 to 0xff of length len.   Source Edit
proc getRandomString(len: static[int]): string

Create a random string with the given number of bytes.

This will create an array of characters of the given len length, fill that using getRandomBytes then Base 64 encode the result.

  Source Edit
proc getRandomString(len: int): string {.
raises: [OSError], tags: []
.}

Create a random string with the given number of bytes.

This will allocate a sequence of characters of the given len length, fill that using getRandomBytes then Base 64 encode the result.

  Source Edit
proc getRandom(upperBound: uint32): uint32 {.
raises: [OSError], tags: []
.}

Generate a random value in the range 0 to upperBound.

The implementation on most platforms is derived from here: http://www.azillionmonkeys.com/qed/random.html

On OpenBSD this simply calls arc4random_uniform.

  Source Edit