summaryrefslogtreecommitdiff
path: root/src/helperfuncs.cpp
diff options
context:
space:
mode:
authordanieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7>2010-02-09 02:22:27 +0000
committerdanieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7>2010-02-09 02:22:27 +0000
commitaab7998583ca16590a32c7bdb80955a18b090700 (patch)
treea2b7f6d82a523e683347b7489ab77f0e940bdede /src/helperfuncs.cpp
parentdb790d9d1516c9c7cd48738340e5df1c8a2bebe3 (diff)
Add random number generation functions to InspIRCd class.
Default implementation uses libc random(), which can be better than rand(). If gnutls is loaded, gcrypt will be used to provide random numbers. git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@12404 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'src/helperfuncs.cpp')
-rw-r--r--src/helperfuncs.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp
index bba8dc8dc..66a84bbce 100644
--- a/src/helperfuncs.cpp
+++ b/src/helperfuncs.cpp
@@ -411,3 +411,31 @@ void InspIRCd::AddExtBanChar(char c)
tok.insert(ebpos, 1, c);
}
}
+
+std::string InspIRCd::GenRandomStr(int length, bool printable)
+{
+ char* buf = new char[length];
+ GenRandom(buf, length);
+ std::string rv;
+ rv.resize(length);
+ for(int i=0; i < length; i++)
+ rv[i] = printable ? 0x3F + (buf[i] & 0x3F) : buf[i];
+ delete[] buf;
+ return rv;
+}
+
+// NOTE: this has a slight bias for lower values if max is not a power of 2.
+// Don't use it if that matters.
+unsigned long InspIRCd::GenRandomInt(unsigned long max)
+{
+ unsigned long rv;
+ GenRandom((char*)&rv, sizeof(rv));
+ return rv % max;
+}
+
+// This is overridden by a higher-quality algorithm when SSL support is loaded
+void GenRandomHandler::Call(char *output, size_t max)
+{
+ for(unsigned int i=0; i < max; i++)
+ output[i] = random();
+}