本文介绍了如何使用C /开发/随机或urandom的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用的/ dev /随机的/ dev / urandom的在C.我怎样才能做到这一点?我不知道我该怎么处理它们在C,如果有人知道请告诉我如何。谢谢。

I want to use /dev/random or /dev/urandom in C. How can I do it? I don't know how can I handle them in C, if someone knows please tell me how. Thank you.

推荐答案

在一般情况下,这是一个更好的主意,以避免打开文件获得随机的数据,因为失败的多少分有在程序中。

In general, it's a better idea to avoid opening files to get random data, because of how many points of failure there are in the procedure.

在最近的Linux发行版中,系统调用可用来获取加密安全随机数字,就不能不如果 GRND_RANDOM 是的不是的指定为标志和读取量为至多256个字节。

On recent Linux distributions, the getrandom system call can be used to get crypto-secure random numbers, and it cannot fail if GRND_RANDOM is not specified as a flag and the read amount is at most 256 bytes.

在OpenBSD系统中,<一个href=\"http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/arc4random.3?query=arc4random&sec=3\"><$c$c>arc4random家人返回加密安全随机数字,不能失败。不幸的是,很多有一个 arc4random 实现不循经而仍然使用RC4等系统;不要指望加密安全性是跨平台的,请检查您的联机手册。

On OpenBSD, the arc4random family returns crypto-secure random numbers and can't fail. Unfortunately, a lot of other systems that have an arc4random implementation didn't follow through and still use RC4; don't expect the crypto-secure properties to be cross-platform, check your manpages.

否则,您可以使用随机的设备,如果他们的文件。您可以从他们读,你会得到随机数据。我使用打开 / 在这里,但的fopen / FREAD 将工作一样好。

Otherwise, you can use the random devices as if they were files. You read from them and you get random data. I'm using open/read here, but fopen/fread would work just as well.

int randomData = open("/dev/random", O_RDONLY);
char myRandomData[50];
size_t randomDataLen = 0;
while (randomDataLen < sizeof myRandomData)
{
    ssize_t result = read(randomData, myRandomData + randomDataLen, (sizeof myRandomData) - randomDataLen);
    if (result < 0)
    {
        // error, unable to read /dev/random 
    }
    randomDataLen += result;
}
close(randomData);

您可以关闭文件描述符前阅读更多的随机字节,但的/ dev /随机将最终耗尽。一旦这样做,将中止读操作和将返回多少字节被成功读取。 的/ dev / urandom的不会阻塞或耗尽。 (的/ dev / urandom的也将永远给你你要的字节数,这样你就不会需要大约读循环。)

You may read many more random bytes before closing the file descriptor, although /dev/random will eventually deplete. Once it does, it will abort the reading operation and read will return how many bytes were successfully read. /dev/urandom will never block or deplete. (/dev/urandom will also always give you the number of bytes you asked for, so you wouldn't need the loop around the read.)

的/ dev /随机的/ dev / urandom的 /的区别开发/随机使用从设备驱动程序收集创建随机数的环境噪音。 的/ dev / urandom的返回随​​机数的根据的关于的/ dev /随机,但它也背后都有一个RNG算法来掩饰在没有熵位都可以从的/ dev /随机。它仍然被认为密码安全,所以一般是用比的/ dev /随机

The difference between /dev/random and /dev/urandom is that /dev/random uses environmental noise gathered from device drivers to create random numbers. /dev/urandom returns random numbers based on /dev/random, but it also has a RNG algorithm behind it to cover up for when no entropy bits are available from /dev/random. It is still considered cryptographically-secure, and so in general it's a better idea to use it than /dev/random.

这篇关于如何使用C /开发/随机或urandom的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 18:04