1./dev/random
And/dev/urandom
Special files
Some systems provide two types of Random Pseudo devices.
:/Dev/random
And
/Dev/urandom
.
The difference between the two devices lies in
/Dev/random
It will be blocked until the system generates enough random numbers, so it can ensure high quality random numbers. Relatively,
/Dev/urandom
No deadlock, and the random degree of data is not high.
Read
1kb
Random key tuples
[Root @ local ~] #
TIME dd COUNT = 1 bs = 1024 if =/dev/random>/dev/null
0 + 1 records in
0 + 1 records out
O. ooou 0.020 s. 62 0.4% 0 + 0 K 0 + 0io 86pf + 0 w
Read
1 MB
Random key tuples
[Root @ local ~] #
TIME dd COUNT = 1024 BS = 1024 if =/dev/urandom>/dev/null
1024 + 0 records in
2048 + 0 records out
0.000u 0 0.660 S 0 0 0.66 0 + 0 K 0 + 0io
86pf + ow
/Dev/random
The more data is read, the slower the response. We used these two devices to experiment on several systems and found that
/Dev/random
Extract
L0mb
The data actually consumes one day or more. While
/Dev/urandom
Execute the command on our fastest system and generate the same data in three seconds.
Both pseudo devices can replace
Mktemp
To generate temporary file names that are difficult to push.
:
[Root @ local ~] #
Tmpfile =/tmp/secret. $ (CAT/dev/urandom | OD-x | tr-D ''| head-n
1)
[Root @ local ~] #
Echo
$ Tmpfile
#
Show random file names
/Tmp/secret. 00000003024d462705664c043c04410e570492e
Here, we start from
/Dev/urandom
Read Binary byte data streams,
Od
Convert it to hexadecimal format and use
Tr
Remove spaces and then stop when a row is full. Because
Od
Convert each output line
16'
Byte, thus providing
16x8 = 128
Random bits.
2.
Use random files or functions to generate random numbers
Typedef struct {<br/> uint32_t UUID [4]; // 128-bit random number <br/>} uuid_t; <br/> uint32_t get_random_bytes (uint32_t * P) <br/> {<br/> int32_t RfD = 0; <br/> uint32_t ret = 0; <br/> ssize_t Len = 0; <br/> RfD = open ("/dev/urandom", o_rdonly); <br/> If (RfD <0) <br/>{< br/> ret =-1; <br/> goto out; <br/>}< br/> Len = read (RfD, P, 16ul); // read 16 bytes <br/> If (16! = Len) <br/>{< br/> ret =-1; </P> <p >}< br/> close (RFD); <br/> out: <br/> return ret; <br/>}< br/> uint32_t uid_generate_random (uuid_t * new_uuid) <br/>{< br/> uint32_t * P = NULL; <br/> uint32_t ret = 0; <br/> memset (new_uuid, 0, sizeof (uuid_t); <br/> P = new_uuid-> UUID; <br/> ret = get_random_bytes (p); <br/> If (0! = RET) <br/>{< br/> // Random Seed <br/> srandom (uint32_t) time (0) ^ getpid ()); <br/> * P = (uint32_t) random (); <br/> * (p + 1) = (uint32_t) random (); <br/> * (p + 2) = (uint32_t) random (); <br/> * (p + 3) = (uint32_t) random (); <br/>}</P> <p> return ret; <br/>}