Merge 0.10->trunk
[prosody.git] / util / random.lua
index 4963e98c5645268f196c3d98858e1ccc5a7cc8bd..574e2e1cdf384cf7d893530fdcd97c80843aaba7 100644 (file)
@@ -6,17 +6,24 @@
 -- COPYING file in the source package for more information.
 --
 
-local urandom = assert(io.open("/dev/urandom", "r+"));
+local ok, crand = pcall(require, "util.crand");
+if ok then return crand; end
 
-local function seed(x)
-       urandom:write(x);
-       urandom:flush();
+local urandom, urandom_err = io.open("/dev/urandom", "r");
+
+local function seed()
 end
 
 local function bytes(n)
        return urandom:read(n);
 end
 
+if not urandom then
+       function bytes()
+               error("Unable to obtain a secure random number generator, please see https://prosody.im/doc/random ("..urandom_err..")");
+       end
+end
+
 return {
        seed = seed;
        bytes = bytes;