diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-01-11 03:32:12 -0800 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-01-11 03:32:12 -0800 |
| commit | 9d8c7014416b6cffae66497b1c923f862fe6aa1a (patch) | |
| tree | 27e0c27cf673ebbf0d926a8f2c89de5624965369 /uuid.lua | |
| parent | bfe66a70e6fb4ac20253f6d5fb85946ef14ecbc6 (diff) | |
Added "%'s id" to use UUIDs and changed "% = %" to "set % = %" and some
other misc.
Diffstat (limited to 'uuid.lua')
| -rw-r--r-- | uuid.lua | 23 |
1 files changed, 23 insertions, 0 deletions
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 |
