manaphoenix

Ficsit Networks [UUID Module]

Sep 27th, 2020
892
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local uuid = {}
  2.  
  3. local function fold(init, op, ...)
  4.   local result = init
  5.   local args = table.pack(...)
  6.   for i = 1, args.n do
  7.     result = op(result, args[i])
  8.   end
  9.   return result
  10. end
  11.  
  12. local function band(...)
  13.   return fold(0xFFFFFFFF, function(a, b) return a & b end, ...)
  14. end
  15.  
  16. local function bor(...)
  17.   return fold(0, function(a, b) return a | b end, ...)
  18. end
  19.  
  20. function uuid.next()
  21.   local sets = {4, 2, 2, 2, 6}
  22.   local result = ""
  23.   local pos = 0
  24.  
  25.   for _, set in ipairs(sets) do
  26.     if result:len() > 0 then
  27.       result = result .. "-"
  28.     end
  29.     for _ = 1, set do
  30.       local byte = math.random(0, 255)
  31.       if pos == 6 then
  32.         byte = bor(band(byte, 0x0F), 0x40)
  33.       elseif pos == 8 then
  34.         byte = bor(band(byte, 0x3F), 0x80)
  35.       end
  36.       result = result .. string.format("%02x", byte)
  37.       pos = pos + 1
  38.     end
  39.   end
  40.  
  41.   return result
  42. end
  43.  
  44. return uuid
RAW Paste Data