Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
Hash algorithms

Files

file  hash_adler32.c
 32-bit Adler hash algorithm
 
file  hash_crc32.c
 32-bit CRC hash algorithm
 
file  hash_djb2.c
 DJB2 hash algorithm
 
file  hash_sdbm.c
 SDBM hash algorithm
 
file  hash_xor8.c
 8-bit XOR hash algorithm for ASCII characters
 

Functions

uint32_t adler32 (const char *s)
 32-bit Adler algorithm implementation More...
 
void test_adler32 ()
 Test function for adler32. More...
 
uint32_t crc32 (const char *s)
 32-bit CRC algorithm implementation More...
 
void test_crc32 ()
 Test function for crc32. More...
 
uint64_t djb2 (const char *s)
 DJB2 algorithm implementation. More...
 
void test_djb2 (void)
 Test function for djb2. More...
 
uint64_t sdbm (const char *s)
 SDBM algorithm implementation. More...
 
void test_sdbm ()
 Test function for sdbm. More...
 
uint8_t xor8 (const char *s)
 8-bit XOR algorithm implementation More...
 
void test_xor8 ()
 Test function for xor8. More...
 

Detailed Description

Function Documentation

◆ adler32()

uint32_t adler32 ( const char *  s)

32-bit Adler algorithm implementation

Parameters
sNULL terminated ASCII string to hash
Returns
32-bit hash result
19{
20 uint32_t a = 1;
21 uint32_t b = 0;
22 const uint32_t MODADLER = 65521;
23
24 size_t i = 0;
25 while (s[i] != '\0')
26 {
27 a = (a + s[i]) % MODADLER;
28 b = (b + a) % MODADLER;
29 i++;
30 }
31 return (b << 16) | a;
32}

◆ crc32()

uint32_t crc32 ( const char *  s)

32-bit CRC algorithm implementation

Parameters
sNULL terminated ASCII string to hash
Returns
32-bit hash result
21{
22 uint32_t crc = 0xffffffff;
23 size_t i = 0;
24 while (s[i] != '\0')
25 {
26 uint8_t byte = s[i];
27 crc = crc ^ byte;
28 for (uint8_t j = 8; j > 0; --j)
29 {
30 crc = (crc >> 1) ^ (0xEDB88320 & (-(crc & 1)));
31 }
32
33 i++;
34 }
35 return crc ^ 0xffffffff;
36}

◆ djb2()

uint64_t djb2 ( const char *  s)

DJB2 algorithm implementation.

Parameters
sNULL terminated string to hash
Returns
64-bit hash result
19{
20 uint64_t hash = 5381; /* init value */
21 size_t i = 0;
22 while (s[i] != '\0')
23 {
24 hash = ((hash << 5) + hash) + s[i];
25 i++;
26 }
27 return hash;
28}

◆ sdbm()

uint64_t sdbm ( const char *  s)

SDBM algorithm implementation.

Parameters
sNULL terminated string to hash
Returns
64-bit hash result
19{
20 uint64_t hash = 0;
21 size_t i = 0;
22 while (s[i] != '\0')
23 {
24 hash = s[i] + (hash << 6) + (hash << 16) - hash;
25 i++;
26 }
27 return hash;
28}

◆ test_adler32()

void test_adler32 ( )

Test function for adler32.

Returns
None
39{
40 assert(adler32("Hello World") == 403375133);
41 assert(adler32("Hello World!") == 474547262);
42 assert(adler32("Hello world") == 413860925);
43 assert(adler32("Hello world!") == 487130206);
44 printf("Tests passed\n");
45}
uint32_t adler32(const char *s)
32-bit Adler algorithm implementation
Definition: hash_adler32.c:18
Here is the call graph for this function:

◆ test_crc32()

void test_crc32 ( )

Test function for crc32.

Returns
None
43{
44 assert(crc32("Hello World") == 1243066710);
45 assert(crc32("Hello World!") == 472456355);
46 assert(crc32("Hello world") == 2346098258);
47 assert(crc32("Hello world!") == 461707669);
48 printf("Tests passed\n");
49}
uint32_t crc32(const char *s)
32-bit CRC algorithm implementation
Definition: hash_crc32.c:20
Here is the call graph for this function:

◆ test_djb2()

void test_djb2 ( void  )

Test function for djb2.

Returns
none
35{
36 assert(djb2("Hello World") == 13827776004929097857);
37 assert(djb2("Hello World!") == 13594750393630990530);
38 assert(djb2("Hello world") == 13827776004967047329);
39 assert(djb2("Hello world!") == 13594750394883323106);
40 printf("Tests passed\n");
41}
uint64_t djb2(const char *s)
DJB2 algorithm implementation.
Definition: hash_djb2.c:18
Here is the call graph for this function:

◆ test_sdbm()

void test_sdbm ( )

Test function for sdbm.

Returns
None
35{
36 assert(sdbm("Hello World") == 12881824461405877380U);
37 assert(sdbm("Hello World!") == 7903571203300273309);
38 assert(sdbm("Hello world") == 15154913742888948900U);
39 assert(sdbm("Hello world!") == 15254999417003201661U);
40 printf("Tests passed\n");
41}
uint64_t sdbm(const char *s)
SDBM algorithm implementation.
Definition: hash_sdbm.c:18
Here is the call graph for this function:

◆ test_xor8()

void test_xor8 ( )

Test function for xor8.

Returns
None
36{
37 assert(xor8("Hello World") == 228);
38 assert(xor8("Hello World!") == 195);
39 assert(xor8("Hello world") == 196);
40 assert(xor8("Hello world!") == 163);
41 printf("Tests passed\n");
42}
uint8_t xor8(const char *s)
8-bit XOR algorithm implementation
Definition: hash_xor8.c:19
Here is the call graph for this function:

◆ xor8()

uint8_t xor8 ( const char *  s)

8-bit XOR algorithm implementation

Parameters
sNULL terminated ASCII string to hash
Returns
8-bit hash result
20{
21 uint8_t hash = 0;
22 size_t i = 0;
23 while (s[i] != '\0')
24 {
25 hash = (hash + s[i]) & 0xff;
26 i++;
27 }
28 return (((hash ^ 0xff) + 1) & 0xff);
29}