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

@@ -7,6 +7,8 @@
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<PreprocessorDefinitions>_CRTDBG_MAP_ALLOC;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<MinimalRebuild>false</MinimalRebuild>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup />

View File

@@ -93,7 +93,6 @@
<ClInclude Include="..\..\modules\beast_basics\events\beast_OncePerSecond.h" />
<ClInclude Include="..\..\modules\beast_basics\functor\beast_Function.h" />
<ClInclude Include="..\..\modules\beast_basics\math\beast_Math.h" />
<ClInclude Include="..\..\modules\beast_basics\math\beast_MurmurHash.h" />
<ClInclude Include="..\..\modules\beast_basics\memory\beast_AllocatedBy.h" />
<ClInclude Include="..\..\modules\beast_basics\memory\beast_FifoFreeStore.h" />
<ClInclude Include="..\..\modules\beast_basics\memory\beast_FifoFreeStoreWithoutTLS.h" />
@@ -156,6 +155,7 @@
<ClInclude Include="..\..\modules\beast_core\maths\beast_Expression.h" />
<ClInclude Include="..\..\modules\beast_core\maths\beast_Interval.h" />
<ClInclude Include="..\..\modules\beast_core\maths\beast_MathsFunctions.h" />
<ClInclude Include="..\..\modules\beast_core\maths\beast_MurmurHash.h" />
<ClInclude Include="..\..\modules\beast_core\maths\beast_Random.h" />
<ClInclude Include="..\..\modules\beast_core\maths\beast_Range.h" />
<ClInclude Include="..\..\modules\beast_core\memory\beast_Atomic.h" />
@@ -302,12 +302,6 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_basics\math\beast_MurmurHash.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_basics\memory\beast_FifoFreeStoreWithoutTLS.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
@@ -519,6 +513,12 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\maths\beast_MurmurHash.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\maths\beast_Random.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>

View File

@@ -503,9 +503,6 @@
<ClInclude Include="..\..\modules\beast_basics\math\beast_Math.h">
<Filter>beast_basics\math</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_basics\math\beast_MurmurHash.h">
<Filter>beast_basics\math</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_basics\memory\beast_AllocatedBy.h">
<Filter>beast_basics\memory</Filter>
</ClInclude>
@@ -734,6 +731,9 @@
<ClInclude Include="..\..\modules\beast_sqdb\detail\use_type.h">
<Filter>beast_sqdb\detail</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_core\maths\beast_MurmurHash.h">
<Filter>beast_core\maths</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\modules\beast_core\beast_core.cpp">
@@ -991,9 +991,6 @@
<ClCompile Include="..\..\modules\beast_basics\events\beast_OncePerSecond.cpp">
<Filter>beast_basics\events</Filter>
</ClCompile>
<ClCompile Include="..\..\modules\beast_basics\math\beast_MurmurHash.cpp">
<Filter>beast_basics\math</Filter>
</ClCompile>
<ClCompile Include="..\..\modules\beast_basics\memory\beast_FifoFreeStoreWithoutTLS.cpp">
<Filter>beast_basics\memory</Filter>
</ClCompile>
@@ -1135,6 +1132,9 @@
<ClCompile Include="..\..\modules\beast_sqdb\source\use_type.cpp">
<Filter>beast_sqdb\source</Filter>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\maths\beast_MurmurHash.cpp">
<Filter>beast_core\maths</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\TODO.txt" />

View File

@@ -2,6 +2,8 @@
BEAST TODO
--------------------------------------------------------------------------------
- Implement HardenedHashFunctions
- Set sqlite thread safety model to '2' in beast_sqlite
- Document and rename all the sqdb files and classes

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.