Mercurial > prosody-hg
comparison util/random.lua @ 12446:e54b8a5e35ad 0.12
util.random: Test whether util.crand works before using it (fix #1734)
util.crand can be configured at compile time to use the Linux
getrandom() system call, available from Linux 3.17, but it is still
possible to load it with an older kernel lacking that system call, where
attempting to use it throws an ENOSYS error.
By testing for this on load we can fall back to /dev/urandom in this
case.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sat, 02 Apr 2022 16:33:27 +0200 |
| parents | af8c514e5cf7 |
| children | d10957394a3c |
comparison
equal
deleted
inserted
replaced
| 12444:b33558969b3e | 12446:e54b8a5e35ad |
|---|---|
| 5 -- This project is MIT/X11 licensed. Please see the | 5 -- This project is MIT/X11 licensed. Please see the |
| 6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
| 7 -- | 7 -- |
| 8 | 8 |
| 9 local ok, crand = pcall(require, "util.crand"); | 9 local ok, crand = pcall(require, "util.crand"); |
| 10 if ok then return crand; end | 10 if ok and pcall(crand.bytes, 1) then return crand; end |
| 11 | 11 |
| 12 local urandom, urandom_err = io.open("/dev/urandom", "r"); | 12 local urandom, urandom_err = io.open("/dev/urandom", "r"); |
| 13 | 13 |
| 14 local function bytes(n) | 14 local function bytes(n) |
| 15 local data, err = urandom:read(n); | 15 local data, err = urandom:read(n); |
