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