Try to gather entropy from /dev/random.
This commit is contained in:
parent
551445b92c
commit
60d3db8e6b
1 changed files with 21 additions and 0 deletions
21
src/lib9/truerand.c
Normal file
21
src/lib9/truerand.c
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue