2 * Copyright (c) 2014 <copyright holder> <email>
4 * For license details, see the LICENSE file in the root of this project.
8 #include "util_random.h"
14 #include <openssl/err.h>
15 #include <openssl/rand.h>
19 bool randGenPseudoRandom(unsigned char *buffer, size_t len)
22 std::random_device rd; // slow; use to seed only!
23 std::mt19937 gen(rd()); // seed mersenne twister.
24 std::uniform_int_distribution<> dist(0,UCHAR_MAX);
25 for (size_t i=0; i<len; i++)
26 buffer[i] = dist(gen);
28 #elif defined(HAVE_OPENSSL)
30 bool existingErrors = false;
33 // See if there are any queued OpenSSL errors already.
34 existingErrors = ( 0 == ERR_peek_error() );
38 // Generate random byte string
39 osslReturn = RAND_pseudo_bytes(buffer, len);
41 // Discard the error message if there weren't any OpenSSL errors to begin with.
42 if (osslReturn == 1 && !existingErrors)
44 while (0 != ERR_get_error() );
49 #error no pseudo random generator available