2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local random = require "util.random";
10 local random_bytes = random.bytes;
11 local hex = require "util.hex".to;
12 local m_ceil = math.ceil;
14 local function get_nibbles(n)
15 return hex(random_bytes(m_ceil(n/2))):sub(1, n);
18 local function get_twobits()
19 return ("%x"):format(random_bytes(1):byte() % 4 + 8);
22 local function generate()
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);
28 get_nibbles=get_nibbles;