How to Generate True Random Numbers in Linux

While looking through the Linux kernel book, I found a way to get a true random number.

The linux kernel puts the noise generated by the physical hardware of the machine (such as keyboard strokes, interrupt times, frequency, temperature, etc.) into the entropy pools /dev/random and /dev/urandom of the system. Part of the random data in /dev/random is only put into the entropy pool to reach a certain entropy value, such as when a relatively high entropy value is reached, the data is the most chaotic at that time, so read the random data in /dev/random Data will be blocked (randomness in the entropy pool, that is, the overall entropy value reaches a certain level before there is random data in it), but its random performance is better than /dev/urandom, /dev/urandom can already satisfy Encryption requirements are very high and random numbers are required.

[root@test59 ~]# vi random.c

#include <stdio.h>

int main( int argc, char** args )
{
    unsigned long* seed = malloc( sizeof( unsigned long ) );
    FILE* file = fopen("/dev/random", "r");
    printf( "%d\n", sizeof( unsigned long ) );
    int i;  
    for( i = 0; i < 9999; ++i)
    {
        fread( seed, 1, sizeof( unsigned long ), file ); 
        printf( "%lld\n", *seed );
    }
    return 1;
}

[root@test59 ~]# gcc random.c        
[root@test59 ~]# ./a.out

4
19941251665
19270289895
17908196899
19952896273
19796292007
20353450353
21115463529
20863995845
18673226572
18264664191
20194203122
17676988606
18187335585
17527617129
18539147318
17684736786
19253561163
17469991849

linux

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325540201&siteId=291194637