Skip to content

Commit e12c2d8

Browse files
authored
Merge pull request #1988 from compnerd/i-am-totally-random
uuid: make `read_random` work on Windows
2 parents f555464 + 43de4ac commit e12c2d8

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

uuid/uuid.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
#include <unistd.h>
4040
#elif defined(_WIN32)
4141
#include <io.h>
42+
#define WIN32_LEAN_AND_MEAN
43+
#include <Windows.h>
44+
#include <bcrypt.h>
4245
#endif
4346
#include <stdio.h>
4447

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

71-
#define WIN32_LEAN_AND_MEAN
72-
#include <windows.h>
73-
7474
static inline void nanotime(struct timespec *tv) {
7575
FILETIME ftTime;
7676

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

86+
#if TARGET_OS_WINDOWS
87+
static inline void read_random(void *buffer, unsigned numBytes) {
88+
BCryptGenRandom(NULL, buffer, numBytes,
89+
BCRYPT_RNG_USE_ENTROPY_IN_BUFFER | BCRYPT_USE_SYSTEM_PREFERRED_RNG);
90+
}
91+
#else
8692
static inline void read_random(void *buffer, unsigned numBytes) {
8793
int fd = open("/dev/urandom", O_RDONLY);
8894
read(fd, buffer, numBytes);
8995
close(fd);
9096
}
97+
#endif
9198

9299

93100
UUID_DEFINE(UUID_NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

0 commit comments

Comments
 (0)