diff --git a/libretroshare/src/util/rsrandom.cc b/libretroshare/src/util/rsrandom.cc index bc358064e..6bfa0eb43 100644 --- a/libretroshare/src/util/rsrandom.cc +++ b/libretroshare/src/util/rsrandom.cc @@ -19,35 +19,39 @@ * along with this program. If not, see . * * * *******************************************************************************/ + +#include "rsrandom.h" + #include #include #include -#include "rsrandom.h" - #include -uint32_t RSRandom::index = RSRandom::N ; -std::vector RSRandom::MT(RSRandom::N,0u) ; -RsMutex RSRandom::rndMtx("RSRandom") ; +uint32_t RsRandom::index = RsRandom::N; +std::vector RsRandom::MT(RsRandom::N,0u); +RsMutex RsRandom::rndMtx("RsRandom"); -// According to our tests (cyril+thunder), on both Windows and Linux does -// RAND_bytes init itself automatically at first call, from system-based -// unpredictable values, so that seeding is not even needed. -// This call still adds some randomness (not much actually, but it's always good to -// have anyway) -// +/* According to our tests (cyril+thunder), on both Windows and Linux does + * RAND_bytes init itself automatically at first call, from system-based + * unpredictable values, so that seeding is not even needed. + * This call still adds some randomness (not much actually, but it's always good + * to have anyway) */ #ifdef WINDOWS_SYS -#include "util/rstime.h" -#ifdef WIN_PTHREADS_H -static bool auto_seed = RSRandom::seed( (time(NULL) + ((uint32_t) pthread_self())*0x1293fe)^0x18e34a12 ) ; -#else -static bool auto_seed = RSRandom::seed( (time(NULL) + ((uint32_t) pthread_self().p)*0x1293fe)^0x18e34a12 ) ; -#endif +# include "util/rstime.h" +# ifdef WIN_PTHREADS_H +static bool auto_seed = RSRandom::seed( + (time(nullptr) + + static_cast(pthread_self()) *0x1293fe)^0x18e34a12 ); +# else // def WIN_PTHREADS_H +static bool auto_seed = RSRandom::seed( + (time(nullptr) + + static_cast(pthread_self().p)*0x1293fe)^0x18e34a12 ); +# endif // def WIN_PTHREADS_H #endif -bool RSRandom::seed(uint32_t s) +bool RsRandom::seed(uint32_t s) { - RsStackMutex mtx(rndMtx) ; + RS_STACK_MUTEX(rndMtx); MT.resize(N,0) ; // because MT might not be already resized @@ -66,22 +70,22 @@ bool RSRandom::seed(uint32_t s) return true ; } -void RSRandom::random_bytes(unsigned char *data,uint32_t size) +void RsRandom::random_bytes(unsigned char *data,uint32_t size) { RAND_bytes(data,size) ; } -void RSRandom::locked_next_state() +void RsRandom::locked_next_state() { RAND_bytes((unsigned char *)&MT[0],N*sizeof(uint32_t)) ; index = 0 ; } -uint32_t RSRandom::random_u32() +uint32_t RsRandom::random_u32() { uint32_t y; { - RsStackMutex mtx(rndMtx) ; + RS_STACK_MUTEX(rndMtx); index++ ; @@ -102,22 +106,22 @@ uint32_t RSRandom::random_u32() return y; } -uint64_t RSRandom::random_u64() +uint64_t RsRandom::random_u64() { return ((uint64_t)random_u32() << 32ul) + random_u32() ; } -float RSRandom::random_f32() +float RsRandom::random_f32() { return random_u32() / (float)(~(uint32_t)0) ; } -double RSRandom::random_f64() +double RsRandom::random_f64() { return random_u64() / (double)(~(uint64_t)0) ; } -std::string RSRandom::random_alphaNumericString(uint32_t len) +std::string RsRandom::random_alphaNumericString(uint32_t len) { std::string s = "" ; diff --git a/libretroshare/src/util/rsrandom.h b/libretroshare/src/util/rsrandom.h index 59c488909..aeebfa56c 100644 --- a/libretroshare/src/util/rsrandom.h +++ b/libretroshare/src/util/rsrandom.h @@ -21,34 +21,43 @@ *******************************************************************************/ #pragma once -// RSRandom contains a random number generator that is -// - thread safe -// - system independant -// - fast -// - CRYPTOGRAPHICALLY SAFE, because it is based on openssl random number generator #include -#include +#include -class RSRandom +#include "util/rsthreads.h" +#include "util/rsdeprecate.h" + +/** + * RsRandom provide a random number generator that is + * - thread safe + * - platform independent + * - fast + * - CRYPTOGRAPHICALLY SAFE, because it is based on openssl random number + * generator + */ +class RsRandom { - public: - static uint32_t random_u32() ; - static uint64_t random_u64() ; - static float random_f32() ; - static double random_f64() ; +public: + static uint32_t random_u32(); + static uint64_t random_u64(); + static float random_f32(); + static double random_f64(); - static bool seed(uint32_t s) ; + static bool seed(uint32_t s); - static std::string random_alphaNumericString(uint32_t length) ; - static void random_bytes(unsigned char *data,uint32_t length) ; + static std::string random_alphaNumericString(uint32_t length); + static void random_bytes(uint8_t* data, uint32_t length); - private: - static RsMutex rndMtx ; +private: + static RsMutex rndMtx; - static const uint32_t N = 1024; + static const uint32_t N = 1024; - static void locked_next_state() ; - static uint32_t index ; - static std::vector MT ; + static void locked_next_state(); + static uint32_t index; + static std::vector MT; }; + +/// @deprecated this alias is provided only for code retro-compatibility +using RSRandom RS_DEPRECATED_FOR(RsRandom) = RsRandom;