Generate UUID in C

Table of Contents

Method 1: Using LibUUID

LibUUID is a library that provides functions to generate unique identifiers (UUIDs), as specified in RFC4122. The following code demonstrates how to generate a UUID using LibUUID in C.

  // Include the required header file
  #include <uuid/uuid.h>

  // Generate a UUID using LibUUID
  void generateUUID() {
    uuid_t uuid;
    uuid_generate(uuid);

    // Convert the UUID to a string
    char uuidStr[37];
    uuid_unparse_lower(uuid, uuidStr);

    printf("Generated UUID using LibUUID: %s\n", uuidStr);
  }
  

Method 2: Using OpenSSL

OpenSSL is a powerful cryptographic library that can also be used to generate UUIDs. The following code demonstrates how to generate a UUID using OpenSSL in C.

  // Include the required header files
  #include <openssl/uuid.h>
  #include <openssl/evp.h>

  // Generate a UUID using OpenSSL
  void generateUUID() {
    EVP_MD_CTX *mdctx;
    unsigned char md_value[EVP_MAX_MD_SIZE];
    unsigned int md_len;
    EVP_MD *md;

    OpenSSL_add_all_digests();
    md = EVP_get_digestbyname("MD5");
    mdctx = EVP_MD_CTX_create();
    EVP_DigestInit_ex(mdctx, md, NULL);
    EVP_DigestUpdate(mdctx, "", 0);
    EVP_DigestFinal_ex(mdctx, md_value, &md_len);
    EVP_MD_CTX_destroy(mdctx);

    // Convert the UUID to a string
    char uuidStr[37];
    memset(uuidStr, 0, sizeof(uuidStr));
    for (int i = 0; i < md_len; i++) {
      sprintf(&uuidStr[i * 2], "%02x", md_value[i]);
    }

    printf("Generated UUID using OpenSSL: %s\n", uuidStr);
  }
  

Method 3: Using rand() function

The rand() function can be used to generate a pseudo-random number, which can be used as the basis for a UUID. The following code demonstrates how to generate a UUID using the rand() function in C.

  // Include the required header file
  #include <stdlib.h>
  #include <stdio.h>
  #include <time.h>

  // Generate a UUID using rand() function
  void generateUUID() {
    srand(time(NULL));

    // Generate four 32-bit random numbers
    unsigned int num1 = rand();
    unsigned int num2 = rand();
    unsigned int num3 = rand();
    unsigned int num4 = rand();

    // Convert the random numbers to a string
    char uuidStr[37];
    sprintf(uuidStr, "%08x-%04x-%04x-%04x-%08x%04x",
            num1, num2 >> 16, num2 & 0xFFFF,
            num3 >> 16, num3 & 0xFFFF, num4);

    printf("Generated UUID using rand() function: %s\n", uuidStr);
  }
  

Method 4: Using system call

Another way to generate a UUID in C is by making a system call to the uuidgen command-line tool. The following code demonstrates how to generate a UUID using a system call in C.

  // Include the required header files
  #include <stdio.h>
  #include <stdlib.h>

  // Generate a UUID using a system call
  void generateUUID() {
    FILE *fp;
    char uuidStr[37];

    // Open a pipe to the uuidgen command
    fp = popen("uuidgen", "r");
    if (fp == NULL) {
      fprintf(stderr, "Failed to run uuidgen command\n");
      return;
    }

    // Read the UUID from the pipe
    fgets(uuidStr, sizeof(uuidStr), fp);
    pclose(fp);

    printf("Generated UUID using system call: %s\n", uuidStr);
  }
  

Summary

In this tutorial, we explored four different methods for generating UUIDs in C. We covered the usage of LibUUID, OpenSSL, the rand() function, and a system call to the uuidgen command. Depending on your requirements and environment, you can choose the most appropriate method for generating UUIDs in your C programs. Happy coding!