- // Keep it open for the duration of the program
- static File randomDevice("/dev/urandom");
- PCHECK(readFull(randomDevice.fd(), data, size) == size);
+#ifdef _MSC_VER
+ static folly::once_flag flag;
+ static HCRYPTPROV cryptoProv;
+ folly::call_once(flag, [&] {
+ PCHECK(CryptAcquireContext(&cryptoProv, nullptr, nullptr,
+ PROV_RSA_FULL, 0));
+ });
+ CHECK(size <= std::numeric_limits<DWORD>::max());
+ PCHECK(CryptGenRandom(cryptoProv, (DWORD)size, (BYTE*)data));
+#else
+ // Keep the random device open for the duration of the program.
+ static int randomFd = ::open("/dev/urandom", O_RDONLY);
+ PCHECK(randomFd >= 0);
+ auto bytesRead = readFull(randomFd, data, size);
+ PCHECK(bytesRead >= 0 && size_t(bytesRead) == size);
+#endif