Должен ли я использовать «/ dev / random» или «/ dev / urandom» для больших случайных данных?

1002
CBHacking

Иногда я хочу много случайных данных, например, чтобы перезаписать файл или даже весь жесткий диск. Я должен использовать /dev/randomили /dev/urandom? Каковы преимущества или недостатки каждого? Есть ли лучшая альтернатива?

6
Приятно видеть, что вы опубликовали это! JakeGould 8 лет назад 0

1 ответ на вопрос

11
CBHacking

For large amounts of data, you need to use /dev/urandom. The u is for "unlimited", meaning that there will always be data available. If you try to read a lot of data from /dev/random, it will block, preventing your program from continuing for a while.

Both /dev/random and /dev/urandom provide unpredictable (random) data. The data from /dev/random is intended to be completely unpredictable (or truly random), making it suitable for things like long-term cryptographic keys (where an attacker in the future may have the advantages of extensive research and much faster computers to try and break the algorithm used to generate the data). The data from /dev/urandom is based on truly random data, but may be run through a high-quality pseudo-random function to produce additional data. It is still suitable for things like encryption keys, as long as you don't need to be sure they won't be broken for decades, but can also be used for bulk data.

The Linux kernel maintains an "entropy pool" of unpredictable data, wherein each bit has an equal chance of being true or false (one or zero). The kernel builds this entropy pool from various inputs, such as hardware sources, drivers, user actions, and anything else that cannot be reliably predicted. However, these sources take time to accumulate entropy, so the entropy pool can be depleted if it is consumed too quickly.

/dev/random draws directly from the entropy pool. When the pool is depleted, reading from /dev/random doesn't return any more data until the pool has refilled enough, which can take quite some time. /dev/urandom uses a cryptographically-secure pseudo-random number generator (CSPRNG) seeded from the entropy pool. It can generate an infinite amount of output, but the output cannot be predicted without knowing the internal state of the CSPRNG. Because the internal state is initially based on truly-random data, and the CSPRNG algorithm used is designed to not leak its internal state through its output, /dev/urandom is still a good source of highly-random data.

To recap, when you need maximally random data, use /dev/random. However, if you need a lot of data, you need to use /dev/urandom. In general, use /dev/urandom unless you need the data to be indistinguishable from truly random noise for decades to come.

Я думал, что ты расшифровывался как «разблокированный», но это деталь. Хороший ответ! Wouter Verhelst 8 лет назад 0
Википедия заявляет, что она «неограниченная», с указанием функции ядра [`random_read_unlimited`] (http://repo.or.cz/w/davej-history.git/blob/d0562c8dc:/drivers/char/random.c# 1662), но «разблокированный» также имеет смысл. Согласно man-странице, вы не можете получить более 32 МБ из `/ dev / urandom` в любом случае. CBHacking 8 лет назад 0
Я всегда предполагал, что u был для unsigned, как uint является unsigned int во многих языках. Очень информативно, спасибо Patrick L 5 лет назад 0

Похожие вопросы