How to generate truly random numbers (not pseudo) in Linux

What is the (best) way to create safe random numbers on Linux (C / C ++ code), more random than the generic rand () results, and not pseudo like OpenSSL BN_rand?

On Windows, I found CryptGenRandom() as a good option. Is there any equivalent on Linux?

Thanks in advance.

+4
source share
7 answers

you can read from /dev/random , which is populated by the entropy pool. The wikipedia website has good information: http://en.wikipedia.org/wiki//dev/random

+16
source

The "random" numbers generated by a computer without any external data are pseudo-random. This means that they are generated using a mathematical formula. These algorithms are reliable and should be in order for almost all purposes.

To have a “true” random number, you need outside intervention. There are some solutions implemented in various programs (I remember several of them that used mouse movements or atmospheric noise).

As Martigine pointed out, there is also / dev / random on Linux and OSX. It uses the noise collected by the device drivers.

There is also a web service that I just found: http://www.random.org/clients/http/

+5
source

1st CryptGenRandom not a “truly” random device, as they are random enough to be cryptographically secure.

Similarly, Linux (and most unixes) reads from /dev/urandom .

If you want real random numbers, you can read /dev/random , but you can lock while waiting for the system to collect them if the entropy pool is too small.

+2
source

Take a look at boost :: random_device .

Edit: It is in the boost::random namespace, starting with Boost 1.47: boost :: random :: random_device

+1
source

I wrote this earlier today. Compiles in C and C ++ using the GNU compiler on Linux.

 #include "rands.h" #include <sys/types.h> /* for open(2) */ #include <sys/stat.h> /* for open(2) */ #include <fcntl.h> /* for open(2) */ #include <unistd.h> /* for read(2), close(2) */ #define DEVURANDOM "/dev/urandom" typedef uint8_t TYPE; TYPE getRandU8() { TYPE rnum = 0; int fd = open(DEVURANDOM, O_RDONLY); if (fd != -1) { (void) read(fd, (void *)&rnum, sizeof(TYPE)); (void) close(fd); } return rnum; } 

You can change the TYPE to int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t and int64_t if necessary (and change the function name accordingly). You can also use (signed / unsigned) char, short, int, long, long long, etc. The rands.h file (in the same directory) just has function prototypes for binding.

+1
source

/ dev / urandom generates some random numbers based on the actions you perform (move the mouse, enter text, etc.!)

0
source

You can use quantum random number generators such as Quantis: http://www.idquantique.com/true-random-number-generator/products-overview.html

It uses the quantum-mechanical probability of a single photon passing or reflected from a translucent mirror, and generates random bits with true random bits of up to 4 Mbps.

0
source

All Articles