mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Add hardened HashFunction to UnsignedInteger
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user