/dev/random and /dev/urandom
by: G.E. Ozz Nixon Jr.
Published: May 2009
©opyright 2009 by Friends of FPC
While researching a random character generator for a routine to generate a new password when someone forgets theirs... I came across
the following code (I ported to FPC) which reads random numbers from the linux device random (/dev/random) and device urandom (/dev/urandom).
//////////////////////////////////////////////////////////////
// Application: read_random
// Author: G.E. Ozz Nixon Jr.
// ===========================================================
// Code ported from: http://bytes.com/groups/c/219952-dev-urandom-vs-dev-random
// Original C code by Ron Peterson
// Network & Systems Manager
// Mount Holyoke College
//////////////////////////////////////////////////////////////
uses
baseunix;
procedure read_random(dev:string);
const
RAND_LEN = 1024;
var
i,fd:longint;
dat:ansistring;
begin
setlength(dat,RAND_LEN);
fd := fpopen(dev, O_RDONLY);
if (fd<>-1) then begin
fpread(fd, @dat[1], RAND_LEN);
for i:=1 to RAND_LEN do
dat[i]:= chr((ord(dat[i]) mod 10)+48); // deviation
system.writeln(dev,': ',dat);
fpclose(fd);
end;
setlength(dat,0);
end;
begin
read_random('/dev/random');
read_random('/dev/urandom');
end.
In the above code the application is opening a
Linux device as a file and reading 1024 bytes. One of the
flaws to this concept is the use of
mod affects the
quality of
the random. The modulo operation finds the remainder of the division. Whereas,
random is working bit by bit to truly generate a unique bit pattern (byte).
Output he is complaining about...
/dev/random: 7457786333072066861528061125746776121859000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000
/dev/urandom: 722261992511128536016644550004790009331061936439795150430507646
91848999304092758691247284809888249307540976619098237104511649611832502535592
78725151321883123968697163126680205928332606696437848178303712508787572862053
12978920949269267453862790748691651258315541251287681022340425811433726846482
05969398892090720443092914994681184480708732719222774435500326395657249288025
22052854755841270576759434853896349159663200693482868818577199822369280701022
70638989988409432169990017920070155719498546334193137289703558086492459128814
46872782919849943124366624284355209916256852365979137665062524821873054023039
92534220114087638599753208507259991202771218051539793872576551390615235217457
73885300209512910155757768146152201158440754413447025767839352179590797714327
49467930566375555003871512812332900001589694233864701976747616546509726921218
17443379448876869756081042137807568541714869414921374934063310994922104767967
61683958432011926330500369142088849240510609831305515873929821684608859614672
8528711605027520263374754703955741874
As you can see, his "mod" and the use of "mod" in general is dropping the MSB
(Most Significant Bits) from the output of "random", however, it gave him the
sense of 'randomness' for the output of "urandom". The flaw to manipulating
either devices output, is it is truly bitwise random 0..255, if you are needing
a smaller "window" you must make sure your 'math' is bitwise to the number of
bits you need. Also read me research notes on primes and large primes used for
cryptography.
G.E. Ozz Nixon Jr.