# DJB2 Hash Function
The code that I will be referencing is [here](http://www.cse.yorku.ca/~oz/hash.html).
## C
```
/*
Define a function called "hash" which returns an
unsigned (positive) long number.
This function takes in a pointer to a char * data type.
Note that C doesn't have str data types unlike Python.
Instead, it lays out chars contiguously in memory and
ends it with a null '\0' terminator character
to denote the ending of a string. This is why strings are a pain
to deal with in C.
*/
unsigned long hash(unsigned char *str) {
/*
Initialize a variable named hash to some prime number.
Prime numbers are very useful in generating pseudo-
random numbers. You don't need to know why it's 5381 as
it gets very theoretical as to why this is the number
being used (other than it's a prime number).
*/
unsigned long hash = 5381;
int c; // Initialize a variable c
/*
This is by far the most confusing, but this
loop simply goes through each char and assigns
it to c until you encounter a null-terminator
*/
while (c = *str++)
/*
This is the secret-sauce of the hash function
which simply does some bit manipulation and
arithmetic to get the hash
*/
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
```
## Python
After realizing the annotation above, converting it to Python is pretty straightforward:
```
def hash_djb2(s):
hash = 5381
for x in s:
# ord(x) simply returns the unicode rep of the
# character x
hash = (( hash << 5) + hash) + ord(x)
# Note to clamp the value so that the hash is
# related to the power of 2
return hash & 0xFFFFFFFF
```