Add hardened HashFunction to UnsignedInteger

This commit is contained in:
Vinnie Falco
2013-07-26 11:34:44 -07:00
parent 3248a1b57f
commit 810954014b
11 changed files with 55 additions and 16 deletions

View File

@@ -45,8 +45,6 @@ namespace beast
#include "events/beast_DeadlineTimer.cpp"
#include "events/beast_OncePerSecond.cpp"
#include "math/beast_MurmurHash.cpp"
#include "threads/beast_InterruptibleThread.cpp"
#include "threads/beast_Semaphore.cpp"
#include "memory/beast_FifoFreeStoreWithTLS.cpp"

View File

@@ -250,7 +250,6 @@ namespace beast
#include "events/beast_DeadlineTimer.h"
#include "events/beast_OncePerSecond.h"
#include "math/beast_Math.h"
#include "math/beast_MurmurHash.h"
#include "memory/beast_AllocatedBy.h"
#include "memory/beast_PagedFreeStore.h"
#include "memory/beast_GlobalPagedFreeStore.h"

View File

@@ -166,6 +166,7 @@ namespace beast
#include "maths/beast_BigInteger.cpp"
#include "maths/beast_Expression.cpp"
#include "maths/beast_MurmurHash.cpp"
#include "maths/beast_Random.cpp"
#include "memory/beast_MemoryBlock.cpp"

View File

@@ -262,6 +262,7 @@ namespace beast
#include "maths/beast_Expression.h"
#include "maths/beast_Interval.h"
#include "maths/beast_MathsFunctions.h"
#include "maths/beast_MurmurHash.h"
#include "maths/beast_Random.h"
#include "maths/beast_Range.h"
#include "memory/beast_ByteOrder.h"

View File

@@ -43,6 +43,42 @@ public:
typedef unsigned char* iterator;
typedef unsigned char const* const_iterator;
/** Hardened hash function for use with HashMap.
The seed is used to make the hash unpredictable. This prevents
attackers from exploiting crafted inputs to produce degenerate
containers.
@see HashMap
*/
class HashFunction
{
public:
/** Construct a hash function.
If a seed is specified it will be used, else a random seed
will be generated from the system.
@param seedToUse An optional seed to use.
*/
explicit HashFunction (int seedToUse = Random::getSystemRandom ().nextInt ())
: m_seed (seedToUse)
{
}
/** Generates a simple hash from an UnsignedInteger. */
int generateHash (UnsignedInteger <Bytes> const& key, const int upperLimit) const noexcept
{
uint32 hashCode;
Murmur::Hash (key.cbegin (), key.sizeInBytes, m_seed, &hashCode);
// Shouldn't produce negative numbers since upperLimit is an int?
return static_cast <int> (hashCode % upperLimit);
}
private:
int m_seed;
};
/** Construct the object.
The values are uninitialized.