简体   繁体   中英

Using Intel RDRAND in Python 2.7

I want to make use of Intel's RDRAND feature on Windows and generate true random numbers (since Python's random module isn't so random). Is there any API in Python which can access this feature?

I've tried installing the rdrand module mentioned in the comment below, but I keep getting an error. Log: http://pastebin.com/A2Vqsqec

The error seems to be thrown by these lines in rdrand.c:

#ifdef __GNUC__
#define USING_GCC 1
#elif __clang__
#define USING_CLANG 1
#else
#error Only support for gcc or clang currently
#error if you port to another compiler, please
#error send back the patch to https://github.com/stillson/rdrand
#endif

Why is this happening?

UPDATE: I've checked and made sure that __GNUC__ is defined

You will probably want to use Python to wrap a C/C++ routine, instead of using the Python implementation of RdRand(). A research paper here ( http://iopscience.iop.org/article/10.3847/1538-4357/aa7ede/meta;jsessionid=A9DA9DDB925E6522D058F3CEEC7D0B21.ip-10-40-2-120 ), or non-paywalled version here ( https://arxiv.org/abs/1707.02212 ) recently showed how poor the performance of RdRand() in Python is. Even so, as the paper mentions, the RdRand and RdSeed instructions are not quite "truly" random...

Hope that helps.

You don't necessarily need RDRAND for quality randomness. The documentation for the random module states:

Use os.urandom() or SystemRandom if you require a cryptographically secure pseudo-random number generator.

The documentation for os.urandom(n) says:

Return a string of n random bytes suitable for cryptographic use.

This function returns random bytes from an OS-specific randomness source. The returned data should be unpredictable enough for cryptographic applications, though its exact quality depends on the OS implementation. On a UNIX-like system this will query /dev/urandom , and on Windows it will use CryptGenRandom() .

SystemRandom is based on urandom .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM