Skip to content

uuid: make read_random work on Windows #1988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions uuid/uuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
#include <unistd.h>
#elif defined(_WIN32)
#include <io.h>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <bcrypt.h>
#endif
#include <stdio.h>

Expand Down Expand Up @@ -68,9 +71,6 @@ static inline void nanotime(struct timespec *tv) {
#elif TARGET_OS_WINDOWS
#include <time.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

static inline void nanotime(struct timespec *tv) {
FILETIME ftTime;

Expand All @@ -83,11 +83,18 @@ static inline void nanotime(struct timespec *tv) {
}
#endif

#if TARGET_OS_WINDOWS
static inline void read_random(void *buffer, unsigned numBytes) {
BCryptGenRandom(NULL, buffer, numBytes,
BCRYPT_RNG_USE_ENTROPY_IN_BUFFER | BCRYPT_USE_SYSTEM_PREFERRED_RNG);
}
#else
static inline void read_random(void *buffer, unsigned numBytes) {
int fd = open("/dev/urandom", O_RDONLY);
read(fd, buffer, numBytes);
close(fd);
}
#endif


UUID_DEFINE(UUID_NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Expand Down