Giovanni's Diary > Subjects > Programming > Gists >

C / hash_djb2.c

Simple hash function for strings.

// SPDX-License-Identifier: MIT

// hash [bytes] of size [len]
// Credits to http://www.cse.yorku.ca/~oz/hash.html
unsigned int djb2(const char *bytes, unsigned int len)
{
  unsigned int hash = 5381;
  for (unsigned int i = 0; i < len; ++i)
    hash = hash * 33 + bytes[i];
  return hash;
}

Travel: Gists, Index