diff options
author | wkj <devnull@localhost> | 2004-04-21 03:06:03 +0000 |
---|---|---|
committer | wkj <devnull@localhost> | 2004-04-21 03:06:03 +0000 |
commit | 60d3db8e6bafa21d807ea31690eddf44bb7ef020 (patch) | |
tree | 81ac6c00d5a73cce1255e2559fb23db99e5d33bf /src/lib9 | |
parent | 551445b92c1f11d4f543e96790ff29762ab1ad10 (diff) | |
download | plan9port-60d3db8e6bafa21d807ea31690eddf44bb7ef020.tar.gz plan9port-60d3db8e6bafa21d807ea31690eddf44bb7ef020.tar.bz2 plan9port-60d3db8e6bafa21d807ea31690eddf44bb7ef020.zip |
Try to gather entropy from /dev/random.
Diffstat (limited to 'src/lib9')
-rw-r--r-- | src/lib9/truerand.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/lib9/truerand.c b/src/lib9/truerand.c new file mode 100644 index 00000000..aaf10f0a --- /dev/null +++ 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); +} |