diff options
author | Sadie Powell <sadie@witchery.services> | 2024-03-07 21:56:58 +0000 |
---|---|---|
committer | Sadie Powell <sadie@witchery.services> | 2024-03-07 22:52:41 +0000 |
commit | 464e6b8010c11708bcbed7cf6b2d972977f0e286 (patch) | |
tree | 0adee0c876aee23acb1e16e2195df90500ff6e65 | |
parent | 3272c1bbc65eaa29ba63e176ec76702c5b21d367 (diff) |
Use the C++11 random number generator instead of rand().
This is safer, faster, and doesn't require seeding.
-rw-r--r-- | data/anope.example.conf | 13 | ||||
-rw-r--r-- | include/anope.h | 3 | ||||
-rw-r--r-- | modules/dns.cpp | 2 | ||||
-rw-r--r-- | modules/encryption/enc_bcrypt.cpp | 2 | ||||
-rw-r--r-- | modules/encryption/enc_sha256.cpp | 2 | ||||
-rw-r--r-- | modules/nickserv/nickserv.cpp | 2 | ||||
-rw-r--r-- | modules/webcpanel/pages/index.cpp | 2 | ||||
-rw-r--r-- | src/config.cpp | 4 | ||||
-rw-r--r-- | src/init.cpp | 4 | ||||
-rw-r--r-- | src/misc.cpp | 12 | ||||
-rw-r--r-- | src/nickcore.cpp | 2 | ||||
-rw-r--r-- | src/xline.cpp | 2 |
12 files changed, 21 insertions, 29 deletions
diff --git a/data/anope.example.conf b/data/anope.example.conf index f7a55121f..227cdd4bf 100644 --- a/data/anope.example.conf +++ b/data/anope.example.conf @@ -396,19 +396,6 @@ options casemap = "ascii" /* - * This key is used to initiate the random number generator. This number - * MUST be random as you want your passcodes to be random. Don't give this - * key to anyone! Keep it private! - * - * NOTE: If you don't uncomment this or keep the default values, any talented - * programmer would be able to easily "guess" random strings used to mask - * information. Be safe, and come up with a 7-digit number. - * - * This directive is optional, but highly recommended. - */ - #seed = 9866235 - - /* * Sets the number of invalid password tries before services removes a user * from the network. If a user enters a number of invalid passwords equal to * the given amount for any services function or combination of functions diff --git a/include/anope.h b/include/anope.h index bafd862e7..000e74d1a 100644 --- a/include/anope.h +++ b/include/anope.h @@ -558,6 +558,9 @@ namespace Anope */ extern CoreExport Anope::string Random(size_t len); + /** Generate a random number. */ + extern CoreExport int RandomNumber(); + /** Calculates the levenshtein distance between two strings. * @param s1 The first string. * @param s2 The second string. diff --git a/modules/dns.cpp b/modules/dns.cpp index c16a390a1..d7eda6f9d 100644 --- a/modules/dns.cpp +++ b/modules/dns.cpp @@ -679,7 +679,7 @@ public: : Manager(creator) , Timer(300, true) , serial(Anope::CurTime) - , cur_id(rand()) + , cur_id(Anope::RandomNumber()) { } diff --git a/modules/encryption/enc_bcrypt.cpp b/modules/encryption/enc_bcrypt.cpp index 76809ed4e..cc255813d 100644 --- a/modules/encryption/enc_bcrypt.cpp +++ b/modules/encryption/enc_bcrypt.cpp @@ -22,7 +22,7 @@ class EBCRYPT final { char entropy[16]; for (auto &chr : entropy) - chr = static_cast<char>(rand() % 0xFF); + chr = static_cast<char>(Anope::RandomNumber() % 0xFF); char salt[32]; if (!_crypt_gensalt_blowfish_rn("$2a$", rounds, entropy, sizeof(entropy), salt, sizeof(salt))) diff --git a/modules/encryption/enc_sha256.cpp b/modules/encryption/enc_sha256.cpp index b9580cd7f..97bd5dd9d 100644 --- a/modules/encryption/enc_sha256.cpp +++ b/modules/encryption/enc_sha256.cpp @@ -249,7 +249,7 @@ class ESHA256 final void NewRandomIV() { for (auto &ivsegment : iv) - ivsegment = static_cast<uint32_t>(rand()); + ivsegment = static_cast<uint32_t>(Anope::RandomNumber()); } /* returns the IV as base64-encrypted string */ diff --git a/modules/nickserv/nickserv.cpp b/modules/nickserv/nickserv.cpp index 499b84d32..058dd29be 100644 --- a/modules/nickserv/nickserv.cpp +++ b/modules/nickserv/nickserv.cpp @@ -246,7 +246,7 @@ public: int i = 0; do { - guestnick = guestprefix + stringify(static_cast<uint16_t>(rand())); + guestnick = guestprefix + stringify(static_cast<uint16_t>(Anope::RandomNumber())); if (guestnick.length() > nicklen) guestnick = guestnick.substr(0, nicklen); } diff --git a/modules/webcpanel/pages/index.cpp b/modules/webcpanel/pages/index.cpp index 216c7cda4..204e00db1 100644 --- a/modules/webcpanel/pages/index.cpp +++ b/modules/webcpanel/pages/index.cpp @@ -50,7 +50,7 @@ public: { char c; do - c = 48 + (rand() % 75); + c = 48 + (Anope::RandomNumber() % 75); while (!isalnum(c)); id += c; } diff --git a/src/config.cpp b/src/config.cpp index eb037c76a..b6c7ff4cf 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -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() diff --git a/src/init.cpp b/src/init.cpp index 4d8594a9d..969263191 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -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) diff --git a/src/misc.cpp b/src/misc.cpp index b40c69844..f7ada8f60 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -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) { diff --git a/src/nickcore.cpp b/src/nickcore.cpp index 939f224db..77f4aaddf 100644 --- a/src/nickcore.cpp +++ b/src/nickcore.cpp @@ -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); diff --git a/src/xline.cpp b/src/xline.cpp index cbc26a36c..41f130b47 100644 --- a/src/xline.cpp +++ b/src/xline.cpp @@ -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; } |