jweinst1
7/18/2017 - 5:52 PM

djb2 hash testing in C, makes console to test hashs

djb2 hash testing in C, makes console to test hashs

#include <stdio.h>

//c program hash tester

unsigned long
hash(unsigned char *str)
{
    unsigned long hash = 5381;
    int c;

    while (c = *str++)
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

    return hash;
}

#define HASHTABLE 5000

void hashconsole()
{
  int running = 1;
  unsigned char key[100];
  printf("Hash Tester-----\n");
  while(running)
  {
    printf("Enter a key: ");
    scanf("%s", key);
    if(key[0] == ']') { printf("Shutting down\n"); break; }
    printf("The hash for %s is %d\n", key, hash(key) % HASHTABLE);
  }
}


int main()
{
    hashconsole();
    return 0;
}