Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00031 #include <stdio.h>
00032 #include <unistd.h>
00033 #include <openssl/rand.h>
00034
00035 #include <eurephia_nullsafe.h>
00036 #include <eurephia_context.h>
00037 #include <eurephia_log.h>
00038
00042 static int rand_init = 0;
00043
00054 int eurephia_randstring(eurephiaCTX *ctx, void *rndstr, size_t len) {
00055 int attempts = 0;
00056 do {
00057 if( !rand_init ) {
00058 if( !RAND_load_file("/dev/urandom", 64) ) {
00059 eurephia_log(ctx, LOG_FATAL, 0, "Could not load random data from /dev/urandom");
00060 return 0;
00061 }
00062 rand_init = 1;
00063 }
00064
00065 if( RAND_pseudo_bytes((unsigned char *) rndstr, len) ) {
00066 return 1;
00067 }
00068 sleep(1);
00069 rand_init = 0;
00070 } while( attempts++ < 11 );
00071 eurephia_log(ctx, LOG_FATAL, 0, "RAND_pseudo_bytes() could not generate enough random data");
00072 return 0;
00073 }