X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=util%2Frandom.lua;h=e4b4a700a89c15b2bb72397094ad1b7f0b16fd51;hb=2b120230b5ec5bcd129344ae908e73b89867d600;hp=5938a94f820ac4e3379c1f56594d3abe07810a15;hpb=d7500b793dfcb85934bc5c42213dc6b393b1a2f6;p=prosody.git diff --git a/util/random.lua b/util/random.lua index 5938a94f..e4b4a700 100644 --- a/util/random.lua +++ b/util/random.lua @@ -6,35 +6,19 @@ -- COPYING file in the source package for more information. -- -local tostring = tostring; -local os_time = os.time; -local os_clock = os.clock; -local ceil = math.ceil; -local H = require "util.hashes".sha512; +local urandom, urandom_err = io.open("/dev/urandom", "r"); -local last_uniq_time = 0; -local function uniq_time() - local new_uniq_time = os_time(); - if last_uniq_time >= new_uniq_time then new_uniq_time = last_uniq_time + 1; end - last_uniq_time = new_uniq_time; - return new_uniq_time; +local function seed() end -local function new_random(x) - return H(x..os_clock()..tostring({})); -end - -local buffer = new_random(uniq_time()); - -local function seed(x) - buffer = new_random(buffer..x); +local function bytes(n) + return urandom:read(n); end -local function bytes(n) - if #buffer < n+4 then seed(uniq_time()); end - local r = buffer:sub(1, n); - buffer = buffer:sub(n+1); - return r; +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 {