From 1a14d19cadb7a40592ce3f8b315bf653da727bbe Mon Sep 17 00:00:00 2001 From: djcb Date: Tue, 25 Dec 2012 16:45:05 +0200 Subject: [PATCH] * mu-util: add mu_util_get_hash (refactored) --- lib/mu-util.c | 24 ++++++++++++++++++++++++ lib/mu-util.h | 14 ++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/lib/mu-util.c b/lib/mu-util.c index 7da50099..838d450c 100644 --- a/lib/mu-util.c +++ b/lib/mu-util.c @@ -563,3 +563,27 @@ mu_util_read_password (const char *prompt) return g_strdup (pass); } + + +const char* +mu_util_get_hash (const char* str) +{ + unsigned djbhash, bkdrhash, bkdrseed; + unsigned u; + static char hex[18]; + + g_return_val_if_fail (str, NULL); + + djbhash = 5381; + bkdrhash = 0; + bkdrseed = 1313; + + for(u = 0; str[u]; ++u) { + djbhash = ((djbhash << 5) + djbhash) + str[u]; + bkdrhash = bkdrhash * bkdrseed + str[u]; + } + + snprintf (hex, sizeof(hex), "%08x%08x", djbhash, bkdrhash); + + return hex; +} diff --git a/lib/mu-util.h b/lib/mu-util.h index 1b2d8d22..8991b22c 100644 --- a/lib/mu-util.h +++ b/lib/mu-util.h @@ -502,6 +502,20 @@ gboolean mu_util_g_set_error (GError **err, MuError errcode, const char *frm, .. G_GNUC_PRINTF(3,4); +/** + * calculate a 64-bit hash for the given string, based on a + * combination of the DJB and BKDR hash functions + * + * @param a string + * + * @return the hash as a static string, which stays valid until this + * function is called again. + */ +const char* mu_util_get_hash (const char* str); + + + + #define MU_COLOR_RED "\x1b[31m" #define MU_COLOR_GREEN "\x1b[32m" #define MU_COLOR_YELLOW "\x1b[33m"