Skip to content

Commit

Permalink
Update uuid.lua to generate RFC4122 valid version 4 UUIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
jobe1986 authored and payonel committed Jan 11, 2018
1 parent 1a8cd8d commit c78cf08
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ function uuid.next()
-- 8-4-4-4-12 (halved sizes because bytes make hex pairs)
local sets = {4, 2, 2, 2, 6}
local result = ""
local pos = 0

for _,set in ipairs(sets) do
if result:len() > 0 then
result = result .. "-"
end
for i = 1,set do
result = result .. string.format("%02x", math.random(0, 255))
local byte = math.random(0, 255)
if pos == 6 then
byte = bit32.bor(bit32.band(byte, 0x0F), 0x40)
elseif pos == 8 then
byte = bit32.bor(bit32.band(byte, 0x3F), 0x80)
end
result = result .. string.format("%02x", byte)
pos = pos + 1
end
end

Expand Down

0 comments on commit c78cf08

Please sign in to comment.