shithub: mc

ref: e09c6b53f3b1928d3752c53c813025dbcbb976e0
dir: /lib/crypto/entropy.myr/

View raw version
use std

pkg crypto =
	const getentropy : (buf : byte[:] -> void)
;;

var randfd

const __init__ = {
	match std.open("/dev/urandom", std.Oread)
	| `std.Ok fd:	randfd = fd
	| `std.Err e:	randfd = std.try(std.open("/dev/random", std.Oread))
	;;
}

const getentropy = {buf
	var nread

	nread = 0
	while nread < buf.len
		match std.read(randfd, buf)
		| `std.Err e:	std.die(std.fmt("unable to read from randfd: {}\n", e))
		| `std.Ok 0:	std.die("could not get entropy from randfd: EOF\n")
		| `std.Ok n:	nread += n
		;;
	;;
}