SuperFastHash
#include <Windows.h>
PVOID CopyMemoryEx(_Inout_ PVOID Destination, _In_ CONST PVOID Source, _In_ SIZE_T Length)
{
PBYTE D = (PBYTE)Destination;
PBYTE S = (PBYTE)Source;
while (Length--)
*D++ = *S++;
return Destination;
}
SIZE_T StringLengthW(_In_ LPCWSTR String)
{
LPCWSTR String2;
for (String2 = String; *String2; ++String2);
return (String2 - String);
}
UINT16 Read16BitsAlignedSafe(PVOID Pointer)
{
UINT16 Value;
CopyMemoryEx(&Value, Pointer, sizeof(Value));
return Value;
}
UINT HashStringSuperFastHashW(PWCHAR String, INT Length)
{
UINT32 Hash = (UINT32)Length;
UINT32 TempObject = 0;
INT Remainder = 0;
Remainder = Length & 3;
Length >>= 2;
for (; Length > 0; Length--)
{
Hash = Read16BitsAlignedSafe(String);
TempObject = (Read16BitsAlignedSafe(String + 2) << 11) ^ Hash;
Hash = (Hash << 16) ^ TempObject;
String += 4;
Hash += Hash >> 11;
}
switch (Remainder)
{
case 3:
{
Hash += Read16BitsAlignedSafe(String);
Hash ^= Hash << 16;
Hash ^= (UINT32)String[2] << 18;
Hash += Hash >> 11;
break;
}
case 2:
{
Hash += Read16BitsAlignedSafe(String);
Hash ^= Hash << 11;
Hash += Hash >> 17;
break;
}
case 1:
{
Hash += (UCHAR)String[0];
Hash ^= Hash << 10;
Hash += Hash >> 1;
break;
}
default:
break;
}
Hash ^= Hash << 3;
Hash += Hash >> 5;
Hash ^= Hash << 4;
Hash += Hash >> 17;
Hash ^= Hash << 25;
Hash += Hash >> 6;
return Hash;
}
INT main(VOID)
{
WCHAR StringHashExample[] = L"Hash This String";
UINT Hash = 0;
Hash = HashStringSuperFastHashW(StringHashExample, (INT)(StringLengthW(StringHashExample) * sizeof(WCHAR)));
return ERROR_SUCCESS;
}Last updated