aboutsummaryrefslogtreecommitdiff
path: root/src/helperfuncs.cpp
diff options
context:
space:
mode:
authorGravatar Sadie Powell2024-08-27 16:20:49 +0100
committerGravatar Sadie Powell2024-08-27 16:31:38 +0100
commit3f7a7df7409e177f158bdd677321e4a61eb6a440 (patch)
treec0aa2641655d40fb497007f5c705e40a072f87a4 /src/helperfuncs.cpp
parentUse GenRandom instead of GenRandomInt to populate Bcrypt entropy. (diff)
Deprecate the raw overload of GenRandomStr in favour of GenRandom.
The raw overload was almost always misused where GenRandom would be better. While we're making changes to this code switch the printable mode to use a static array like Anope does.
Diffstat (limited to 'src/helperfuncs.cpp')
-rw-r--r--src/helperfuncs.cpp24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp
index 5ebb6e44d..5ccb8ed6c 100644
--- a/src/helperfuncs.cpp
+++ b/src/helperfuncs.cpp
@@ -443,13 +443,31 @@ std::string Time::ToString(time_t curtime, const char* format, bool utc)
return buffer;
}
+std::string InspIRCd::GenRandomStr(size_t length) const
+{
+ static const char chars[] = {
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
+ 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
+ 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ };
+
+ std::string buf;
+ buf.reserve(length);
+ for (size_t idx = 0; idx < length; ++idx)
+ buf.push_back(chars[GenRandomInt(std::size(chars))]);
+ return buf;
+}
+
std::string InspIRCd::GenRandomStr(size_t length, bool printable) const
{
+ if (printable)
+ return GenRandomStr(length);
+
+ // DEPRECATED
std::vector<char> str(length);
GenRandom(str.data(), length);
- if (printable)
- for (size_t i = 0; i < length; i++)
- str[i] = 0x3F + (str[i] & 0x3F);
return std::string(str.data(), str.size());
}