Comparison

util/uuid.lua @ 7049:0eee56075901

util.uuid: Take random byte directly instead of the low bits from the ascii value of a hex nibble
author Kim Alvefur <zash@zash.se>
date Wed, 06 Jan 2016 03:28:31 +0100
parent 7012:990b4ddaf582
child 7061:eda0feeaf759
comparison
equal deleted inserted replaced
7047:9ca2b720ad43 7049:0eee56075901
14 local function get_nibbles(n) 14 local function get_nibbles(n)
15 return hex(random_bytes(m_ceil(n/2))):sub(1, n); 15 return hex(random_bytes(m_ceil(n/2))):sub(1, n);
16 end 16 end
17 17
18 local function get_twobits() 18 local function get_twobits()
19 return ("%x"):format(get_nibbles(1):byte() % 4 + 8); 19 return ("%x"):format(random_bytes(1):byte() % 4 + 8);
20 end 20 end
21 21
22 local function generate() 22 local function generate()
23 -- generate RFC 4122 complaint UUIDs (version 4 - random) 23 -- generate RFC 4122 complaint UUIDs (version 4 - random)
24 return get_nibbles(8).."-"..get_nibbles(4).."-4"..get_nibbles(3).."-"..(get_twobits())..get_nibbles(3).."-"..get_nibbles(12); 24 return get_nibbles(8).."-"..get_nibbles(4).."-4"..get_nibbles(3).."-"..(get_twobits())..get_nibbles(3).."-"..get_nibbles(12);