Software /
code /
prosody
Comparison
util/uuid.lua @ 1303:2170e2c0d57a
util.uuid: Now generates RFC 4122 complaint UUIDs (version 4 - random)
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Thu, 04 Jun 2009 18:04:26 +0500 |
parent | 1302:4561c6d95339 |
child | 1304:0a49759464e0 |
comparison
equal
deleted
inserted
replaced
1302:4561c6d95339 | 1303:2170e2c0d57a |
---|---|
25 | 25 |
26 local function new_random(x) | 26 local function new_random(x) |
27 return sha1(x..os_clock()..tostring({}), true); | 27 return sha1(x..os_clock()..tostring({}), true); |
28 end | 28 end |
29 | 29 |
30 local buffer = new_random(uniq_time()); | |
31 local function get_nibbles(n) | |
32 if #buffer < n then | |
33 buffer = new_random(buffer..uniq_time()); | |
34 end | |
35 local r = buffer:sub(0, n); | |
36 buffer = buffer:sub(n+1); | |
37 return r; | |
38 end | |
39 local function get_twobits() | |
40 return ("%x"):format(get_nibbles(1):byte() % 4 + 8); | |
41 end | |
42 | |
30 function generate() | 43 function generate() |
31 return new_random(uniq_time()); | 44 -- generate RFC 4122 complaint UUIDs (version 4 - random) |
45 return get_nibbles(8).."-"..get_nibbles(4).."-4"..get_nibbles(3).."-"..(get_twobits())..get_nibbles(3).."-"..get_nibbles(12); | |
32 end | 46 end |
33 | 47 |
34 return _M; | 48 return _M; |