This module generates a UUID when called. The full() version conforms to rfc:4122, the short() version is a denser version.



local UUID = {}

local random = math.random
function UUID:full()
    -- Request for Comment 4122
    local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
    return string.gsub(template, '[xy]', function (c)
        local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
        return string.format('%x', v)
    end)
end

function UUID:short()
    -- unformatted 128 bit hex
    local template ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    return string.gsub(template, '[xy]', function (c)
        local v = (c == 'x') and random(0, 0xf)
        return string.format('%x', v)
    end)
end

return UUID