mirror of
https://github.com/anope/anope.git
synced 2026-07-10 19:03:13 +02:00
Use the C++11 random number generator instead of rand().
This is safer, faster, and doesn't require seeding.
This commit is contained in:
@@ -557,10 +557,6 @@ Conf::Conf() : Block("")
|
||||
}
|
||||
}
|
||||
Anope::CaseMapRebuild();
|
||||
|
||||
/* Check the user keys */
|
||||
if (!options->Get<unsigned>("seed"))
|
||||
Log() << "Configuration option options:seed should be set. It's for YOUR safety! Remember that!";
|
||||
}
|
||||
|
||||
Conf::~Conf()
|
||||
|
||||
@@ -494,10 +494,6 @@ bool Anope::Init(int ac, char **av)
|
||||
/* Initialize multi-language support */
|
||||
Language::InitLanguages();
|
||||
|
||||
/* Initialize random number generator */
|
||||
block = Config->GetBlock("options");
|
||||
srand(block->Get<unsigned>("seed") ^ time(NULL));
|
||||
|
||||
/* load modules */
|
||||
Log() << "Loading modules...";
|
||||
for (int i = 0; i < Config->CountBlock("module"); ++i)
|
||||
|
||||
+11
-1
@@ -20,7 +20,9 @@
|
||||
#include "sockets.h"
|
||||
|
||||
#include <cerrno>
|
||||
#include <climits>
|
||||
#include <numeric>
|
||||
#include <random>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#ifndef _WIN32
|
||||
@@ -745,10 +747,18 @@ Anope::string Anope::Random(size_t len)
|
||||
};
|
||||
Anope::string buf;
|
||||
for (size_t i = 0; i < len; ++i)
|
||||
buf.append(chars[rand() % sizeof(chars)]);
|
||||
buf.append(chars[Anope::RandomNumber() % sizeof(chars)]);
|
||||
return buf;
|
||||
}
|
||||
|
||||
int Anope::RandomNumber()
|
||||
{
|
||||
static std::random_device device;
|
||||
static std::mt19937 engine(device());
|
||||
static std::uniform_int_distribution<int> dist(INT_MIN, INT_MAX);
|
||||
return dist(engine);
|
||||
}
|
||||
|
||||
// Implementation of https://en.wikipedia.org/wiki/Levenshtein_distance
|
||||
size_t Anope::Distance(const Anope::string &s1, const Anope::string &s2)
|
||||
{
|
||||
|
||||
+1
-1
@@ -224,7 +224,7 @@ uint64_t NickCore::GetId()
|
||||
// Generate a random key for SipHash.
|
||||
char key[16];
|
||||
for (auto &chr : key)
|
||||
chr = rand() % CHAR_MAX;
|
||||
chr = Anope::RandomNumber() % CHAR_MAX;
|
||||
|
||||
uint64_t newid = Anope::SipHash24(secretid.c_str(), secretid.length(), key);
|
||||
nickcoreid_map::const_iterator it = NickCoreIdList.find(newid);
|
||||
|
||||
+1
-1
@@ -249,7 +249,7 @@ Anope::string XLineManager::GenerateUID()
|
||||
{
|
||||
char c;
|
||||
do
|
||||
c = (rand() % 75) + 48;
|
||||
c = (Anope::RandomNumber() % 75) + 48;
|
||||
while (!isupper(c) && !isdigit(c));
|
||||
id += c;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user