From e07b9a5eff9d8207849e47f561b4981f4f90ba9d Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Tue, 17 Jun 2025 21:48:07 +0200 Subject: [PATCH] djb2 hash algo --- djb2.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 djb2.c diff --git a/djb2.c b/djb2.c new file mode 100644 index 0000000..a468f97 --- /dev/null +++ b/djb2.c @@ -0,0 +1,22 @@ +#include + +unsigned long djb2(const char *str) { + unsigned long h = 5381; + int c; + + while ((c = *str++)) { + h = ((h << 5) + h) + c; + + // Essentially equal to: + // h = h * 33 + c; + } + + return h; +} + +int main(void) { + char *a = "ABC"; + printf("Hash: %ld\n", djb2(a)); + + return 0; +}