From 93cf81cfe87aa4974468e79b87caae3107b5a913 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 21 Jan 2023 17:58:44 -0800 Subject: [PATCH] Generate GUID according to RFC 4122 --- lua/guid.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lua/guid.lua b/lua/guid.lua index 3ebd4e6..810cd82 100644 --- a/lua/guid.lua +++ b/lua/guid.lua @@ -58,10 +58,23 @@ local function find_pattern_at_pos(pattern, pos, check_col) end local function guid_generate() + -- Generate a pseudo-random GUID according to RFC 4122: + -- https://www.rfc-editor.org/rfc/rfc4122 + + -- Set all bits to randomly (or pseudo-randomly) chosen values. local bytes = {} for i = 1, 16 do bytes[i] = math.random(0, 255) end + + -- Set the two most significant bits (bits 6 and 7) of the + -- clock_seq_hi_and_reserved to zero and one, respectively. + bytes[9] = bit.band(bit.bor(bytes[9], 0x80), 0x8f) + + -- Set the four most significant bits (bits 12 through 15) of the + -- time_hi_and_version field to the 4-bit version number. + bytes[7] = bit.band(bit.bor(bytes[7], 0x40), bit.lshift(4, 4)) + return bytes end