Last active
July 27, 2020 20:37
-
-
Save billz0101/9e1ea2798e10870dfaa9ea521685fc8e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
notes: | |
- random function may not generate cryptographically secure values | |
- guid usually refers to microsofts implementation of uuid standard | |
- this is by no means a perfect implementation of guid/uuid (there are plugins for that which uses the uuid library) | |
- this is my own implementation of uuidv4 (which is normally generated randomly (or pseudo-randomly)) with some googling | |
[https://tools.ietf.org/html/rfc4122] | |
The formal definition of the UUID string representation is | |
provided by the following ABNF [7]: | |
UUID = time-low "-" time-mid "-" | |
time-high-and-version "-" | |
clock-seq-and-reserved | |
clock-seq-low "-" node | |
time-low = 4hexOctet | |
time-mid = 2hexOctet | |
time-high-and-version = 2hexOctet | |
clock-seq-and-reserved = hexOctet | |
clock-seq-low = hexOctet | |
node = 6hexOctet | |
hexOctet = hexDigit hexDigit | |
hexDigit = | |
"0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" / | |
"a" / "b" / "c" / "d" / "e" / "f" / | |
"A" / "B" / "C" / "D" / "E" / "F" | |
randomly or pseudo-randomly | |
generated value may be used | |
*/ | |
#define UUID_MAX_LEN 37 | |
stock UUIDv4(string[], sz = sizeof string) | |
{ | |
if(sz < UUID_MAX_LEN) return printf("error: \n sizeof string (%d) less than UUID_MAX_LEN ("#UUID_MAX_LEN") ", sz); | |
format(string, sz, "%04x%04x-%04x-%04x-%04x-%04x%04x%04x", | |
random(0) & 0xFFFF, random(0) & 0xFFFF, // "time_low": 16+16 = 32 bits | |
random(0) & 0xFFFF, // "time_mid": 16 bits | |
random(0) & 0x0FFF | 0x4000, // "time_hi_and_version": 16 bits (+ version 4) | |
random(0) & 0x3FFF | 0x8000, // "clock_seq_hi_res" & "clock_seq_low": 8+8 = 16 bits | |
random(0) & 0xFFFF, random(0) & 0xFFFF,random(0) & 0xFFFF); // "node": 16+16+16 = 48 bits | |
for(new i; i < strlen(string); i++) string[i] = tolower(string[i]); | |
return 1; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment