From 8201a52cec975c6ed9c81bd6f33c41b3ef7b473a Mon Sep 17 00:00:00 2001 From: mathieui Date: Tue, 10 Mar 2015 01:47:29 +0100 Subject: [PATCH] Traversal: Use a decent PRNG instead of rand() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit we don’t need cryptosecure random, but having a uniform distribution is always better. --- Source/Core/Common/TraversalClient.cpp | 5 +++-- Source/Core/Common/TraversalClient.h | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Source/Core/Common/TraversalClient.cpp b/Source/Core/Common/TraversalClient.cpp index 02fe5ee26e..f17ce0d921 100644 --- a/Source/Core/Common/TraversalClient.cpp +++ b/Source/Core/Common/TraversalClient.cpp @@ -7,9 +7,10 @@ static void GetRandomishBytes(u8* buf, size_t size) { // We don't need high quality random numbers (which might not be available), // just non-repeating numbers! - srand(enet_time_get()); + static std::mt19937 prng(enet_time_get()); + static std::uniform_int_distribution u8_distribution(0, 255); for (size_t i = 0; i < size; i++) - buf[i] = rand() & 0xff; + buf[i] = u8_distribution(prng); } TraversalClient::TraversalClient(ENetHost* netHost, const std::string& server, const u16 port) diff --git a/Source/Core/Common/TraversalClient.h b/Source/Core/Common/TraversalClient.h index 0263d863fb..ddfb38fce1 100644 --- a/Source/Core/Common/TraversalClient.h +++ b/Source/Core/Common/TraversalClient.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "Common/Common.h" #include "Common/Thread.h"