Prepare required data folder for integration tests
[prosody.git] / util / uuid.lua
blobf4fd21f64ec72b9fc3e45f2973c9a1d9d9fabeff
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
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);
16 end
18 local function get_twobits()
19 return ("%x"):format(random_bytes(1):byte() % 4 + 8);
20 end
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);
25 end
27 return {
28 get_nibbles=get_nibbles;
29 generate = generate ;
30 -- COMPAT
31 seed = random.seed;