Skip to content

Instantly share code, notes, and snippets.

@yorickdewid
Created January 30, 2016 17:12
Show Gist options
  • Save yorickdewid/dc600cb52a4ab90747d2 to your computer and use it in GitHub Desktop.
Save yorickdewid/dc600cb52a4ab90747d2 to your computer and use it in GitHub Desktop.
djb2a hash algorithm
#include <stdio.h>
unsigned long hash(unsigned char *str) {
unsigned long hash = 5381;
int c;
while ((c = *str++))
hash = hash * 33 ^ c;
return hash;
}
int main(int argc, char *argv[]) {
if (argc < 2)
return 1;
printf("hash %ld\n", hash(argv[1]));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment