Merge 0.10->trunk
[prosody.git] / util / random.lua
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 seed()
15 end
16
17 local function bytes(n)
18         return urandom:read(n);
19 end
20
21 if not urandom then
22         function bytes()
23                 error("Unable to obtain a secure random number generator, please see https://prosody.im/doc/random ("..urandom_err..")");
24         end
25 end
26
27 return {
28         seed = seed;
29         bytes = bytes;
30 };