This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathHash.c
69 lines (61 loc) · 1.36 KB
/
Hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*!
*
* GRIMREAPER
*
* Austin Hudson
*
* suspicious.actor
*
!*/
#include "Common.h"
/*!
*
* Purpose:
*
* Returns a DJB2 hash representation of the input
* string up to the specified length. If no length
* is specified it is presumed it is a ANSI string.
* and will calculcate the buffer up until the \0
* terminator.
*
!*/
D_SEC( B ) UINT32 HashString( _In_ PUINT8 Buffer, _In_ UINT32 Length )
{
UINT8 Val = 0;
UINT32 Hsh = 5381;
PUINT8 Ptr = C_PTR( Buffer );
/* Loop through until we reach a NULL terminator OR the length specified if not 0 */
while ( TRUE ) {
/* Extract the current ANSI character */
Val = *Ptr;
/* Was no length specified? */
if ( ! Length ) {
/* Have we reached a NULL terminator? */
if ( ! Val ) {
/* Abort the loop */
break;
};
} else
{
/* Have we exceeded the length of the buffer if a length was specified? */
if ( ( UINT32 )( Ptr - ( PUINT8 ) Buffer ) >= Length ) {
/* Abort the loop */
break;
};
/* Has a NULL character? */
if ( ! Val ) {
/* Move onto the next character since we skip it */
++Ptr ; continue;
};
};
/* Is an lowercase character? */
if ( Val >= 'a' ) {
/* Force UPPERCASE */
Val -= 0x20;
};
/* Hash the current character and move onto the next char */
Hsh = ( ( Hsh << 5 ) + Hsh ) + Val; ++Ptr ;
};
/* Return the hash */
return Hsh;
};