#include <stdlib.h> #include <string.h> #include <stdio.h> #include <time.h> int get_nonce(void) { /* add your random nonce int value for more secure protection */ return 289182; } int random_int() { unsigned int nonce = (unsigned int)time(NULL) & get_nonce(); srand(nonce); return rand(); } uint64_t random_uint64(void) { uint64_t num = random_int() & 0x1FFFFF; num = (num << 32) | random_int(); return num; } char *random_uuid(void) { const char *template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"; const char *samples = "0123456789abcdef"; union { unsigned char b[16]; uint64_t word[2]; } rnd; rnd.word[0] = random_uint64(); rnd.word[1] = random_uint64(); /* Generate the string */ char uuid[37], *dst = uuid; const char *p = template; int i = 0, n = 0; while(*p) { n = rnd.b[i >> 1]; n = (i & 1) ? (n >> 4) : (n & 0xf); switch (*p) { case 'x': *dst = samples[n]; i++; break; case 'y': *dst = samples[(n & 0x3) + 8]; i++; break; default: *dst = *p; } p++; dst++; } uuid[36] = '\0'; return strdup(uuid); } int main(int argc, char *argv[]) { char *tmp_uid = random_uuid(); printf("make uuid : %s\n", tmp_uid); free(tmp_uid); return 0; }