commit 60d3db8e6bafa21d807ea31690eddf44bb7ef020
parent 551445b92c1f11d4f543e96790ff29762ab1ad10
Author: wkj <devnull@localhost>
Date: Wed, 21 Apr 2004 03:06:03 +0000
Try to gather entropy from /dev/random.
Diffstat:
1 file changed, 21 insertions(+), 0 deletions(-)
diff --git a/src/lib9/truerand.c b/src/lib9/truerand.c
@@ -0,0 +1,21 @@
+#include <u.h>
+#include <libc.h>
+
+ulong
+truerand(void)
+{
+ int i, n;
+ uchar buf[sizeof(ulong)];
+ static int randfd = -1;
+
+ if(randfd < 0){
+ randfd = open("/dev/random", OREAD);
+ fcntl(randfd, F_SETFD, FD_CLOEXEC);
+ }
+ if(randfd < 0)
+ sysfatal("can't open /dev/random: %r");
+ for(i=0; i<sizeof(buf); i += n)
+ if((n = readn(randfd, buf+i, sizeof(buf)-i)) < 0)
+ sysfatal("can't read /dev/random: %r");
+ return *((ulong*)buf);
+}