如何在 Linux 中生成真正的随机数(非伪)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4062396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate truly random numbers (NOT pseudo) in Linux
提问by rkellerm
What is the (best) way to create a secure random numbers in Linux (C/ C++ code), more random than the general rand() results, and not pseudo as OpenSSL BN_rand?
在 Linux(C/C++ 代码)中创建安全随机数的(最佳)方法是什么,比一般的 rand() 结果更随机,而不是像 OpenSSL BN_rand 那样伪?
In Windows I found CryptGenRandom()
as a good option. Is there any equivalent in Linux?
在 Windows 中,我发现这CryptGenRandom()
是一个不错的选择。Linux 中是否有任何等价物?
Thank you in advance.
先感谢您。
采纳答案by Martijn
you can read from /dev/random
which is populated with an entropy pool. There is some good info on the wikipedia site on it: http://en.wikipedia.org/wiki//dev/random
您可以从中读取/dev/random
填充有熵池的信息。维基百科网站上有一些很好的信息:http: //en.wikipedia.org/wiki//dev/random
回答by Marc Demierre
"Random" numbers generated by a computer without any external data are pseudo-random. It means that they are generated with a mathematical formula. These algorithms are reliable and should be okay for almost all purposes.
由没有任何外部数据的计算机生成的“随机”数字是伪随机数。这意味着它们是用数学公式生成的。这些算法是可靠的,几乎适用于所有目的。
To have a "true" random number, you need an intervention from outside. There are some solutions implemented in various programs (I remember of several ones that used mouse movements or atmospheric noise).
要获得“真正的”随机数,您需要外部干预。在各种程序中实现了一些解决方案(我记得有几个使用鼠标移动或大气噪音的解决方案)。
As Martijin just pointed, there is also /dev/random on Linux and OSX. It uses the noise collected by the device drivers.
正如 Martijin 刚刚指出的,Linux 和 OSX 上也有 /dev/random。它使用设备驱动程序收集的噪声。
There is also a web service that I just found : http://www.random.org/clients/http/
还有一个我刚刚找到的网络服务:http: //www.random.org/clients/http/
回答by usta
Take a look at boost::random_device.
Edit:It resides in namespace boost::random
starting from Boost 1.47 : boost::random::random_device
编辑:它位于boost::random
从 Boost 1.47 开始的命名空间中:boost::random::random_device
回答by Artyom
1st CryptGenRandom
is not "truly" random device by they are enough random to be cryptographically safe.
1stCryptGenRandom
不是“真正的”随机设备,因为它们足够随机以保证密码安全。
Similar under Linux (and most unixes) is reading from /dev/urandom
.
在 Linux(和大多数 unixes)下类似的是从/dev/urandom
.
If you want to get real random numbers you may read /dev/random
but you may get blocked waiting for system to collect them if entropy pool is too small.
如果您想获得真正的随机数,您可以阅读,/dev/random
但如果熵池太小,您可能会被阻止等待系统收集它们。
回答by Stefan George
/dev/urandom generates some random numbers based on the actions you perform(moving of the mouse,typing,etc!)
/dev/urandom 根据您执行的操作(移动鼠标、打字等!)生成一些随机数
回答by pr1268
I wrote this earlier today. Compiles in both C and C++ using GNU compiler on Linux.
我今天早些时候写了这个。在 Linux 上使用 GNU 编译器在 C 和 C++ 中编译。
#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 as needed (and change the name of the function appropriately). You could also use (signed/unsigned) char, short, int, long, long long, etc. The rands.h file (in the same directory) just has function prototypes for linkage.
您可以根据需要将 TYPE 更改为 int8_t、uint16_t、int16_t、uint32_t、int32_t、uint64_t 和 int64_t(并适当更改函数名称)。您还可以使用(有符号/无符号)char、short、int、long、long long 等。rands.h 文件(在同一目录中)只有用于链接的函数原型。
回答by Rubi Sharmax
You can use quantum random number generators such as Quantis: http://www.idquantique.com/true-random-number-generator/products-overview.html
可以使用Quantis等量子随机数生成器:http: //www.idquantique.com/true-random-number-generator/products-overview.html
It utilizes the quantum mechanical probability of a single photon passing or being reflected from a semi transparent mirror and generated random bits up to 4Mbit/s true random bits.
它利用单个光子通过或从半透明镜反射的量子力学概率,并生成高达 4Mbit/s 的真随机位的随机位。