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--
8
9local random = require "util.random";
10local random_bytes = random.bytes;
11local hex = require "util.hex".to;
12local m_ceil = math.ceil;
13
14local function get_nibbles(n)
15	return hex(random_bytes(m_ceil(n/2))):sub(1, n);
16end
17
18local function get_twobits()
19	return ("%x"):format(random_bytes(1):byte() % 4 + 8);
20end
21
22local 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);
25end
26
27return {
28	get_nibbles=get_nibbles;
29	generate = generate ;
30	-- COMPAT
31	seed = random.seed;
32};
33