Commit f622a97e authored by Pekka Pessi's avatar Pekka Pessi

su_uniqueid.c: using /dev/urandom when possible.

darcs-hash:20060925212918-65a35-1951e7bab6d060b47d28e1cee6080e59a5451a0e.gz
parent 44cfcff8
......@@ -286,10 +286,14 @@ isize_t su_guid_sprintf(char* buf, size_t len, su_guid_t const *v)
int su_randint(int lb, int ub)
{
unsigned rnd = 0;
if (!initialized) init();
rnd = random();
if (urandom) {
fread(&rnd, 1, sizeof rnd, urandom);
}
else
rnd = random();
if (ub - lb + 1 != 0)
rnd %= (ub - lb + 1);
......@@ -303,7 +307,10 @@ void *su_randmem(void *mem, size_t siz)
if (!initialized) init();
for (i = 0; i < siz; i++) {
if (urandom) {
fread(mem, 1, siz, urandom);
}
else for (i = 0; i < siz; i++) {
unsigned r = random();
((char *)mem)[i] = (r >> 24) ^ (r >> 16) ^ (r >> 8) ^ r;
}
......@@ -318,7 +325,14 @@ void *su_randmem(void *mem, size_t siz)
*/
uint32_t su_random(void)
{
uint32_t rnd;
if (!initialized) init();
return (uint32_t)random();
if (urandom)
fread(&rnd, 1, sizeof rnd, urandom);
else
rnd = (uint32_t)random();
return rnd;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment