sysrandom.nim

A Nim library to generate random bytes, strings and integers using platform provided sources of randomness.

The following sources of randomness are used depending on platform:

Installation

nimble install sysrandom

Usage

import sysrandom

## Make sure to close the `/dev/urandom` file on posix or close the DLL handle on Windows after you're finished generating random data
defer: closeRandom()

## Create an array of 10 random bytes (`array[10, byte]`)
let randomBytes = getRandomBytes(10)
echo "Generating 10 random bytes: ", repr(randomBytes)

## Get a random unsigned 32 bit integer (`uint32`) in the range 0..0xffffffff
let randomUint32 = getRandom()
echo "Random integer: ", randomUint32

## Generate a random string based upon a 32 byte array of random values, base 64 encoded
let randomString = getRandomString(32)
echo "Random string: ", randomString