From 9d8c7014416b6cffae66497b1c923f862fe6aa1a Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 11 Jan 2018 03:32:12 -0800 Subject: Added "%'s id" to use UUIDs and changed "% = %" to "set % = %" and some other misc. --- uuid.lua | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 uuid.lua (limited to 'uuid.lua') diff --git a/uuid.lua b/uuid.lua new file mode 100644 index 0000000..878727b --- /dev/null +++ b/uuid.lua @@ -0,0 +1,23 @@ +-- A simple UUID function based on RFC 4122: http://www.ietf.org/rfc/rfc4122.txt +local function uuid() + local r = math.random + -- Set all the other bits to randomly (or pseudo-randomly) chosen values. + local bytes = { + r(2^(4*8)), --time-low + r(2^(2*8)), --time-mid + r(2^(2*8-4)), --time-high-and-version + r(2^(1*8-2)), --clock-seq-and-reserved + r(2^(1*8)), --clock-seq-low + r(2^(3*8)), r(2^(3*8)), --node + } + -- Set the four most significant bits (bits 12 through 15) of the + -- time_hi_and_version field to the 4-bit version number from + -- Section 4.1.3. + bytes[3] = bit32.bor(bytes[3], 0x4000) + -- Set the two most significant bits (bits 6 and 7) of the + -- clock_seq_hi_and_reserved to zero and one, respectively. + bytes[4] = bit32.bor(bytes[4], 0xC0) + return ("%08x-%04x-%04x-%02x%02x-%6x%6x"):format(unpack(bytes)) +end + +return uuid -- cgit v1.2.3