Software /
code /
prosody
Comparison
util/uuid.lua @ 7076:ad9e683b8f0b
util.uuid: Open /dev/urandom read-only, make seed() a noop
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 10 Jan 2016 23:21:34 +0100 |
parent | 7057:c633e1338554 |
child | 7078:ec17115e3721 |
comparison
equal
deleted
inserted
replaced
7060:eed0632cd636 | 7076:ad9e683b8f0b |
---|---|
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 error = error; | 9 local error = error; |
10 local round_up = math.ceil; | 10 local round_up = math.ceil; |
11 local urandom, urandom_err = io.open("/dev/urandom", "r+"); | 11 local urandom, urandom_err = io.open("/dev/urandom", "r"); |
12 | 12 |
13 module "uuid" | 13 module "uuid" |
14 | 14 |
15 local function get_nibbles(n) | 15 local function get_nibbles(n) |
16 local binary_random = urandom:read(round_up(n/2)); | 16 local binary_random = urandom:read(round_up(n/2)); |
28 end | 28 end |
29 -- generate RFC 4122 complaint UUIDs (version 4 - random) | 29 -- generate RFC 4122 complaint UUIDs (version 4 - random) |
30 return get_nibbles(8).."-"..get_nibbles(4).."-4"..get_nibbles(3).."-"..(get_twobits())..get_nibbles(3).."-"..get_nibbles(12); | 30 return get_nibbles(8).."-"..get_nibbles(4).."-4"..get_nibbles(3).."-"..(get_twobits())..get_nibbles(3).."-"..get_nibbles(12); |
31 end | 31 end |
32 | 32 |
33 function seed(x) | 33 function seed() |
34 urandom:write(x); | |
35 urandom:flush(); | |
36 end | 34 end |
37 | 35 |
38 return _M; | 36 return _M; |