/* Sample application to demonstrate the use of the PRNGD daemon */ /* for reading random numbers. */ /***************************************************************** Compiling and running the example application. ****************************************************************** [jones1@master mpihello]$ cc test_rand.c -lssl [jones1@master mpihello]$ ./a.out Random number is 92 Random number is 213 Random number is 189 Random number is 222 Random number is 141 Random number is 105 Random number is 244 Random number is 175 Random number is 52 Random number is 166 ******************************************************************/ #include #include int main(int argc, char *argv[]) { int i, j, status; int prngd_bytes, random_num; unsigned char random_bytes[4]; static char egd_path[]="/var/run/egd-pool"; /* First action is to seed the PRNG via the PRNGD daemon. */ prngd_bytes = RAND_egd(egd_path); if (prngd_bytes == -1) { printf("Problem seeding the PRNG!\n"); } /* Ok, let's read something from the randomness pool. */ for (i=0; i < 10; i++) { /* Zero out the integer so there's nothing left behind. */ memset((void *) &random_num, 0x0, 4); /* Now go and get some number of bytes of randomness from the pool. */ /* The second parameter is the number of bytes to read. For an */ /* integer, the obvious values are 1, 2, and 4 depending on the */ /* desired range of the random number. */ status = RAND_bytes((char *) &random_num, 1); if (status != 1) { printf("Problem: Not enough randomness available.\n"); } /* Print the random number as an unsigned integer. */ printf("Random number is %u\n", random_num); } /* Innocuous return value. */ return(0); }