util/random.lua

Thu, 03 Dec 2020 17:05:27 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 03 Dec 2020 17:05:27 +0000
changeset 0
550f506de75a
permissions
-rw-r--r--

Initial commit

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

mercurial