urand

Simple method of obtaining secure random numbers from the OS


Keywords
random, urandom, crypto
License
MIT
Install
nimble install urand

Documentation

Urand

Cryptographic-quality random bytes from your operating system Extracted and adapted from urandom.nim from nim-random by Oleh Prypin

WARNING: untested on Windows, but it should work there

API:

type Urand* = object
  when defined(windows):
    cryptProv: HCRYPTPROV = 0
  else:
    ufile: File
# Contains the ``/dev/urandom`` file handle or, if on windows, some kind of marker, it seems

proc open*(r: var Urand)
# Opens /dev/urandom, or, on Windows, does some kind of initalization.
# Raises OSError on failure.

proc close*(r: var Urand)
# Closes /dev/urandom when not on Windows

proc urand*(r: var Urand, size: Natural): seq[uint8]
proc urand*(r: var Urand, size: static[Natural]): array[size, uint8]
# Returns a random array[size, uint8] or seq of random uint8 generated using the operating system's cryptographic source.
# Raises OSError on failure.

Example:

#test1.nim
import urand

var ur: Urand


ur.open()

echo ur.urand(16)

ur.close()

License: MIT (see LICENSE.txt)