Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
RCryptoRandom.cxx
Go to the documentation of this file.
1#include "ROOT/RConfig.hxx"
3
4#ifdef WIN32
5#include "Windows4Root.h"
6#include <bcrypt.h>
7#else
8#if defined(R__ARC4_STDLIB)
9#include <cstdlib>
10#elif defined(R__ARC4_BSDLIB)
11#include <bsd/stdlib.h>
12#elif defined(R__GETRANDOM_CLIB)
13#include <sys/random.h>
14#elif defined(R__USE_URANDOM)
15#include <fstream>
16#endif
17#endif
18
19bool ROOT::Internal::GetCryptoRandom(void *buf, unsigned int len)
20{
21 if (len > 256)
22 return false;
23
24#ifdef WIN32
25
26 return BCryptGenRandom((BCRYPT_ALG_HANDLE)NULL, (PUCHAR)buf, (ULONG)len, BCRYPT_USE_SYSTEM_PREFERRED_RNG) == 0;
27
28#else // UNIX
29
30#if defined(R__ARC4_STDLIB) || defined(R__ARC4_BSDLIB)
31 arc4random_buf(buf, len);
32 return true;
33#elif defined(R__GETRANDOM_CLIB)
34 return getrandom(buf, len, GRND_NONBLOCK) == len;
35#elif defined(R__USE_URANDOM)
36 std::ifstream urandom{"/dev/urandom"};
37 if (!urandom)
38 return false;
39 urandom.read(reinterpret_cast<char *>(buf), len);
40 return urandom.good();
41#else
42#error "Reliable cryptographic random function not defined"
43 return false;
44#endif
45
46#endif
47}
bool GetCryptoRandom(void *buf, unsigned int len)
Get random bytes from the operating system's cryptographic random number generator The requested numb...