util/random.lua

changeset 0
550f506de75a
equal deleted inserted replaced
-1:000000000000 0:550f506de75a
1 -- Prosody IM
2 -- Copyright (C) 2008-2014 Matthew Wild
3 -- Copyright (C) 2008-2014 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local ok, crand = pcall(require, "util.crand");
10 if ok then return crand; end
11
12 local urandom, urandom_err = io.open("/dev/urandom", "r");
13
14 local function bytes(n)
15 local data, err = urandom:read(n);
16 if not data then
17 if err then
18 error("Unable to retrieve data from secure random number generator (/dev/urandom): "..tostring(err));
19 else
20 error("Secure random number generator (/dev/urandom) returned an end-of-file condition");
21 end
22 end
23 return data;
24 end
25
26 if not urandom then
27 function bytes()
28 error("Unable to obtain a secure random number generator, please see https://prosody.im/doc/random ("..urandom_err..")");
29 end
30 end
31
32 return {
33 bytes = bytes;
34 _source = "/dev/urandom";
35 };

mercurial