aboutsummaryrefslogtreecommitdiff
path: root/lua/extregs/uuid.lua
blob: e23a37f65cd1e5cc5cd91b227168e158b7c87431 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
local util = require('extregs.util')
local M = {}
function M.get_provider(_)
  return util.readonly(function()
    local bytes = vim.uv.random(16)
    assert(bytes and #bytes == 16, 'unable to obtain random bytes')
    local b = { bytes:byte(1, 16) }
    b[7] = (b[7] % 16) + 64
    b[9] = (b[9] % 64) + 128
    local h = {}
    for i = 1, 16 do h[i] = string.format('%%02x', b[i]) end
    return table.concat(h, '', 1, 4) .. '-' .. table.concat(h, '', 5, 6) .. '-' ..
      table.concat(h, '', 7, 8) .. '-' .. table.concat(h, '', 9, 10) .. '-' .. table.concat(h, '', 11, 16)
  end)
end
return M