From aab7998583ca16590a32c7bdb80955a18b090700 Mon Sep 17 00:00:00 2001 From: danieldg Date: Tue, 9 Feb 2010 02:22:27 +0000 Subject: 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 --- src/helperfuncs.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/helperfuncs.cpp') 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(); +} -- cgit v1.2.3