Skip to content

Commit

Permalink
uuid: make read_random work on Windows
Browse files Browse the repository at this point in the history
Windows does not have a `/dev/urandom` source.  Provide a wrapper around
the `BCryptGetRandom` interface to provide the equivalent functionality.
  • Loading branch information
compnerd committed Mar 12, 2019
1 parent a132f6e commit 43de4ac
Showing 1 changed file with 10 additions and 3 deletions.
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

0 comments on commit 43de4ac

Please sign in to comment.