Entropy in the Linux Kernel: Difference between revisions
Created page with "Entropy is required to generate random numbers. {{code|/dev/random}} and {{code|/dev/urandom}} on Linux provide a way to access random numbers. Both devices are supplied by th..." |
|||
| (9 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
Entropy is required to generate random numbers. | Entropy is required to generate good random numbers. Modern operating systems will use physical hardware as sources for entropy and the 'randomness' goes into an entropy pool for future use. | ||
== Entropy | On Linux, {{code|/dev/random}} and {{code|/dev/urandom}} are pseudo devices that provide random numbers. Both devices are supplied by the kernel's PRNG, though {{code|/dev/random}} will block when the entropy pool is depleted while {{code|/dev/urandom}} will continue to generate random numbers. | ||
For a cheap entropy source, see [[Entropy Source using RTL-SDR]]. | |||
== Linux Entropy Pool == | |||
You can see the size of the entropy pool size in linux by running: | You can see the size of the entropy pool size in linux by running: | ||
{{ | {{highlight|lang=terminal|code= | ||
cat /proc/sys/kernel/random/poolsize | # cat /proc/sys/kernel/random/poolsize | ||
}} | }} | ||
The number of 'bits' of entropy can be seen at: | The number of 'bits' of entropy can be seen at: | ||
{{ | {{highlight|lang=terminal|code= | ||
cat /proc/sys/kernel/random/entropy_avail | # cat /proc/sys/kernel/random/entropy_avail | ||
}} | |||
== Visualizing Randomness == | |||
The following PHP script will let you generate bitmap from data piped to the script. You can also use the commented out line to visualize PHP's {{code|rand}} function. This script can be found at http://api.leo.leung.xyz/random | |||
{{highlight|lang=php|code= | |||
<?php | |||
header("Content-type: image/png"); | |||
// Requires the GD Library | |||
$img = imagecreatetruecolor(512, 512) or die("Cannot Initialize new GD image stream"); | |||
$white = imagecolorallocate($img, 255, 255, 255); | |||
$fp = fopen('php://stdin', 'r'); | |||
for ($x = 0; $x < 512; $x++) { | |||
for ($y = 0; $y < 512; $y++) { | |||
// $data = rand(0, 1) | |||
$data = ord(fread($fp, 1)); | |||
if ($data % 2 == 0) { | |||
imagesetpixel($img, $x, $y, $white); | |||
} | |||
} | |||
} | |||
fclose($fp); | |||
imagepng($img); | |||
imagedestroy($img); | |||
}} | }} | ||
| Line 17: | Line 52: | ||
* http://www.2uo.de/myths-about-urandom/ | * http://www.2uo.de/myths-about-urandom/ | ||
{{ | {{Navbox Linux}}[[Category:Linux]] | ||
[[Category:Linux]] | |||
Latest revision as of 02:05, 15 June 2020
Entropy is required to generate good random numbers. Modern operating systems will use physical hardware as sources for entropy and the 'randomness' goes into an entropy pool for future use.
On Linux, /dev/random and /dev/urandom are pseudo devices that provide random numbers. Both devices are supplied by the kernel's PRNG, though /dev/random will block when the entropy pool is depleted while /dev/urandom will continue to generate random numbers.
For a cheap entropy source, see Entropy Source using RTL-SDR.
Linux Entropy Pool
You can see the size of the entropy pool size in linux by running:
# cat /proc/sys/kernel/random/poolsize
The number of 'bits' of entropy can be seen at:
# cat /proc/sys/kernel/random/entropy_avail
Visualizing Randomness
The following PHP script will let you generate bitmap from data piped to the script. You can also use the commented out line to visualize PHP's rand function. This script can be found at http://api.leo.leung.xyz/random
<?php
header("Content-type: image/png");
// Requires the GD Library
$img = imagecreatetruecolor(512, 512) or die("Cannot Initialize new GD image stream");
$white = imagecolorallocate($img, 255, 255, 255);
$fp = fopen('php://stdin', 'r');
for ($x = 0; $x < 512; $x++) {
for ($y = 0; $y < 512; $y++) {
// $data = rand(0, 1)
$data = ord(fread($fp, 1));
if ($data % 2 == 0) {
imagesetpixel($img, $x, $y, $white);
}
}
}
fclose($fp);
imagepng($img);
imagedestroy($img);