在 C 中生成随机 UUID

Generating a random UUID in C

我将如何在 C 中生成基于熵的 UUID 并将其存储为字符串(字符指针)?

我希望有一种简单的方法可以在内部执行此操作,但如果没有,system("uuidgen -r") 也可以。

此功能由 libuuid 提供。 (Debian 上的软件包 libuuid1uuid-dev。)

这是一个简单的程序,它生成一个基于熵的(随机)UUID 并将其写入 stdout,然后以状态 0.

退出
/* For malloc() */
#include <stdlib.h>
/* For puts()/printf() */
#include <stdio.h>
/* For uuid_generate() and uuid_unparse() */
#include <uuid/uuid.h>


/* Uncomment to always generate capital UUIDs. */
//#define capitaluuid true

/* Uncomment to always generate lower-case UUIDs. */
//#define lowercaseuuid true

/*
 * Don't uncomment either if you don't care (the case of the letters
 * in the 'unparsed' UUID will depend on your system's locale).
 */


int main(void) {
    uuid_t binuuid;
    /*
     * Generate a UUID. We're not done yet, though,
     * for the UUID generated is in binary format 
     * (hence the variable name). We must 'unparse' 
     * binuuid to get a usable 36-character string.
     */
    uuid_generate_random(binuuid);

    /*
     * uuid_unparse() doesn't allocate memory for itself, so do that with
     * malloc(). 37 is the length of a UUID (36 characters), plus '[=10=]'.
     */
    char *uuid = malloc(37);

#ifdef capitaluuid
    /* Produces a UUID string at uuid consisting of capital letters. */
    uuid_unparse_upper(binuuid, uuid);
#elif lowercaseuuid
    /* Produces a UUID string at uuid consisting of lower-case letters. */
    uuid_unparse_lower(binuuid, uuid);
#else
    /*
     * Produces a UUID string at uuid consisting of letters
     * whose case depends on the system's locale.
     */
    uuid_unparse(binuuid, uuid);
#endif

    // Equivalent of printf("%s\n", uuid); - just my personal preference
    puts(uuid);

    return 0;
}

uuid_unparse() 不分配它自己的内存;为避免在执行时出现分段错误,您必须使用 uuid = malloc(37); 手动执行此操作(您也可以将 UUID 存储在该长度的字符数组中:char uuid[37];)。确保使用 -luuid 进行编译,以便链接器知道 uuid_generate_random()uuid_unparse() 是在 libuuid.

中定义的

在 Linux 上,您可以使用 ,对我来说 Ubuntu 20.x 位于 /usr/include/uuid/.

/* uuid.c
 * 
 * Defines function uuid
 *
 * Print a universally unique identifer, created using Linux uuid_generate.
 *
 * 
 * Compile
 *
 * gcc uuid.c -o uuid -luuid -Wall -g
 *
 *
 * Run
 * 
 * ./uuid
 * 
 *
 * Debug
 *
 * gdb uuid
 * b main
 * r
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <uuid/uuid.h>

char* uuid(char out[UUID_STR_LEN]){
  uuid_t b;
  uuid_generate(b);
  uuid_unparse_lower(b, out);
  return out;
}

int main(){
  char out[UUID_STR_LEN]={0};
  puts(uuid(out));
  return EXIT_SUCCESS;
}

因为每个人都在说要使用库,所以我想我会使用 C 的 rand() 编写一个 fast-and-dirty-C-only 版本。确保在某处调用 srand 这样随机 UUID 实际上是随机的。

不能保证它不会生成两个相同的 UUID,也不能保证它符合 UUID 的任何标准。据我所知,它们只是随机的十六进制字符串,块之间有破折号。此外,这不是多线程安全的。

char* gen_uuid() {
    char v[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    //3fb17ebc-bc38-4939-bc8b-74f2443281d4
    //8 dash 4 dash 4 dash 4 dash 12
    static char buf[37] = {0};

    //gen random for all spaces because lazy
    for(int i = 0; i < 36; ++i) {
        buf[i] = v[rand()%16];
    }

    //put dashes in place
    buf[8] = '-';
    buf[13] = '-';
    buf[18] = '-';
    buf[23] = '-';

    //needs end byte
    buf[36] = '[=10=]';

    return buf;
}