28 #define DEBUG_TYPE "rng" 38 RandomNumberGenerator::RandomNumberGenerator(
StringRef Salt) {
40 <<
"Warning! Using unseeded random number generator.\n");
47 std::vector<uint32_t>
Data;
48 Data.resize(2 + Salt.
size());
54 std::seed_seq SeedSeq(Data.begin(), Data.end());
55 Generator.seed(SeedSeq);
66 if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL,
67 CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
69 if (CryptGenRandom(hProvider, Size, static_cast<BYTE *>(Buffer)))
70 return std::error_code();
72 return std::error_code(GetLastError(), std::system_category());
74 int Fd = open(
"/dev/urandom", O_RDONLY);
77 ssize_t BytesRead =
read(Fd, Buffer, Size);
79 Ret = std::error_code(errno, std::system_category());
80 else if (BytesRead != static_cast<ssize_t>(Size))
81 Ret = std::error_code(EIO, std::system_category());
83 Ret = std::error_code(errno, std::system_category());
87 return std::error_code(errno, std::system_category());
This class represents lattice values for constants.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
initializer< Ty > init(const Ty &Val)
std::error_code getRandomBytes(void *Buffer, size_t Size)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
generator_type::result_type result_type
result_type operator()()
Returns a random number in the range [0, Max).
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
static cl::opt< unsigned long long > Seed("rng-seed", cl::value_desc("seed"), cl::Hidden, cl::desc("Seed for the random number generator"), cl::init(0))
StringRef - Represent a constant reference to a string, i.e.
OutputIt copy(R &&Range, OutputIt Out)