Repository: valkey-io/valkey
File: src/util.c (C)

Code before:
        /* Revert to a weaker seed, and in this case reseed again
         * at every call.*/
        for (unsigned int j = 0; j < sizeof(seed); j++) {
            struct timeval tv;
            gettimeofday(&tv, NULL);
            pid_t pid = getpid();
            seed[j] = tv.tv_sec ^ tv.tv_usec ^ pid ^ (long)fp;
        }
    } else {
        seed_initialized = 1;
    }
    if (fp) fclose(fp);
}

/* This function receives a string with 64 bytes encoded as 128 hexadecimal
 * digits, and sets the 64 byte random seed. */
void setRandomSeedCString(char *seed_str, size_t len) {
    assert(len == sizeof(seed) * 2);
    for (size_t i = 0; i < sizeof(seed); i++) {
        sscanf(seed_str + (i * 2), "%02hhX", &seed[i]);
    }
    seed_initialized = 1;
}

/* This function populates a char buffer with 129 bytes with the 64 bytes
 * random seed encoded as 128 hexadecimal digits. */
void getRandomSeedCString(char *buff, size_t len) {
    assert(len == (sizeof(seed) * 2 + 1));

    if (!seed_initialized) {
        initializeRandomSeed();
    }

    for (size_t i = 0; i < sizeof(seed); i++) {
        snprintf(buff + (i * 2), 3, "%02hhX", seed[i]);
    }
    buff[sizeof(seed) * 2] = 0;
}

/* Get random bytes, attempts to get an initial seed from /dev/urandom and
 * the uses a one way hash function in counter mode to generate a random
 * stream. However if /dev/urandom is not available, a weaker seed is used.
 *
 * This function is not thread safe, since the state is global. */
void getRandomBytes(unsigned char *p, size_t len) {
    static uint64_t counter = 0; /* The counter we hash with the seed. */

    if (!seed_initialized) {
        initializeRandomSeed();
    }


Review comment (suggestion):
```suggestion
/* This function populates a char buffer with 129 bytes with the 64 bytes
 * random seed encoded as 128 hexadecimal digits and a null terminator. */
```

Code after:
        /* Revert to a weaker seed, and in this case reseed again
         * at every call.*/
        for (unsigned int j = 0; j < sizeof(seed); j++) {
            struct timeval tv;
            gettimeofday(&tv, NULL);
            pid_t pid = getpid();
            seed[j] = tv.tv_sec ^ tv.tv_usec ^ pid ^ (long)fp;
        }
    } else {
        seed_initialized = 1;
    }
    if (fp) fclose(fp);
}

/* This function receives a string with 64 bytes encoded as 128 hexadecimal
 * digits, and sets the 64 byte random seed. */
void setRandomSeedCString(char *seed_str, size_t len) {
    assert(len == sizeof(seed) * 2);
    for (size_t i = 0; i < sizeof(seed); i++) {
        sscanf(seed_str + (i * 2), "%02hhX", &seed[i]);
    }
    seed_initialized = 1;
}

/* This function populates a char buffer with 129 bytes with the 64 bytes
 * random seed encoded as 128 hexadecimal digits and a null terminator. */
void getRandomSeedCString(char *buff, size_t len) {
    assert(len == (sizeof(seed) * 2 + 1));

    if (!seed_initialized) {
        initializeRandomSeed();
    }

    for (size_t i = 0; i < sizeof(seed); i++) {
        snprintf(buff + (i * 2), 3, "%02hhX", seed[i]);
    }
    buff[sizeof(seed) * 2] = '\0';
}

/* Get random bytes, attempts to get an initial seed from /dev/urandom and
 * the uses a one way hash function in counter mode to generate a random
 * stream. However if /dev/urandom is not available, a weaker seed is used.
 *
 * This function is not thread safe, since the state is global. */
void getRandomBytes(unsigned char *p, size_t len) {
    static uint64_t counter = 0; /* The counter we hash with the seed. */

    if (!seed_initialized) {
        initializeRandomSeed();
    }