Commit 2f30b475 authored by Kenton Varda's avatar Kenton Varda

Use CryptGenRandom() rather than non-existent /dev/urandom on Windows. Fixes #167.

parent fbfcb24f
......@@ -28,18 +28,33 @@
#include <sys/stat.h>
#include <fcntl.h>
#if _WIN32
#include <windows.h>
#undef VOID
#endif
namespace capnp {
namespace compiler {
uint64_t generateRandomId() {
uint64_t result;
#if _WIN32
HCRYPTPROV handle;
KJ_ASSERT(CryptAcquireContextW(&handle, nullptr, nullptr,
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT));
KJ_DEFER(KJ_ASSERT(CryptReleaseContext(handle, 0)) {break;});
KJ_ASSERT(CryptGenRandom(handle, sizeof(result), reinterpret_cast<BYTE*>(&result)));
#else
int fd;
KJ_SYSCALL(fd = open("/dev/urandom", O_RDONLY));
ssize_t n;
KJ_SYSCALL(n = read(fd, &result, sizeof(result)), "/dev/urandom");
KJ_ASSERT(n == sizeof(result), "Incomplete read from /dev/urandom.", n);
#endif
return result | (1ull << 63);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment