Comparison

util/uuid.lua @ 1304:0a49759464e0

util.uuid: Added function seed(string) to allow further seeding of the PRNG
author Waqas Hussain <waqas20@gmail.com>
date Thu, 04 Jun 2009 19:55:56 +0500
parent 1303:2170e2c0d57a
child 1523:841d61be198f
comparison
equal deleted inserted replaced
1303:2170e2c0d57a 1304:0a49759464e0
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()); 30 local buffer = new_random(uniq_time());
31 local function _seed(x)
32 buffer = new_random(buffer..x);
33 end
31 local function get_nibbles(n) 34 local function get_nibbles(n)
32 if #buffer < n then 35 if #buffer < n then seed(uniq_time()); end
33 buffer = new_random(buffer..uniq_time());
34 end
35 local r = buffer:sub(0, n); 36 local r = buffer:sub(0, n);
36 buffer = buffer:sub(n+1); 37 buffer = buffer:sub(n+1);
37 return r; 38 return r;
38 end 39 end
39 local function get_twobits() 40 local function get_twobits()
42 43
43 function generate() 44 function generate()
44 -- generate RFC 4122 complaint UUIDs (version 4 - random) 45 -- 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); 46 return get_nibbles(8).."-"..get_nibbles(4).."-4"..get_nibbles(3).."-"..(get_twobits())..get_nibbles(3).."-"..get_nibbles(12);
46 end 47 end
48 seed = _seed;
47 49
48 return _M; 50 return _M;