namespace {
void readRandomDevice(void* data, size_t size) {
- // Keep it open for the duration of the program
- static File randomDevice("/dev/urandom");
- PCHECK(readFull(randomDevice.fd(), data, size) == size);
+ // 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);
}
class BufferedRandomDevice {
ptr_ += size;
}
-ThreadLocal<BufferedRandomDevice> bufferedRandomDevice;
} // namespace
void Random::secureRandom(void* data, size_t size) {
+ static ThreadLocal<BufferedRandomDevice> bufferedRandomDevice;
bufferedRandomDevice->get(data, size);
}