util/uuid.lua
changeset 1302 4561c6d95339
parent 896 2c0b9e3c11c3
child 1303 2170e2c0d57a
equal deleted inserted replaced
1301:d10d84f755b5 1302:4561c6d95339
     5 -- This project is MIT/X11 licensed. Please see the
     5 -- This project is MIT/X11 licensed. Please see the
     6 -- COPYING file in the source package for more information.
     6 -- COPYING file in the source package for more information.
     7 --
     7 --
     8 
     8 
     9 
     9 
    10 
       
    11 local m_random = math.random;
    10 local m_random = math.random;
    12 local tostring = tostring;
    11 local tostring = tostring;
       
    12 local os_time = os.time;
       
    13 local os_clock = os.clock;
       
    14 local sha1 = require "util.hashes".sha1;
       
    15 
    13 module "uuid"
    16 module "uuid"
    14 
    17 
       
    18 local last_uniq_time = 0;
       
    19 local function uniq_time()
       
    20 	local new_uniq_time = os_time();
       
    21 	if last_uniq_time >= new_uniq_time then new_uniq_time = last_uniq_time + 1; end
       
    22 	last_uniq_time = new_uniq_time;
       
    23 	return new_uniq_time;
       
    24 end
       
    25 
       
    26 local function new_random(x)
       
    27 	return sha1(x..os_clock()..tostring({}), true);
       
    28 end
       
    29 
    15 function generate()
    30 function generate()
    16 	return tostring(m_random(0, 99999999));
    31 	return new_random(uniq_time());
    17 end
    32 end
    18 
    33 
    19 return _M;
    34 return _M;