Merge 0.10->trunk
[prosody.git] / util / random.lua
index 05f36aba326d661fd93182d3875e13520e1b01c9..574e2e1cdf384cf7d893530fdcd97c80843aaba7 100644 (file)
@@ -6,7 +6,10 @@
 -- 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 urandom, urandom_err = io.open("/dev/urandom", "r");
 
 local function seed()
 end
@@ -15,6 +18,12 @@ 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;