* mu-util: add mu_util_get_hash (refactored)

This commit is contained in:
djcb 2012-12-25 16:45:05 +02:00
parent 19a0c1c478
commit 1a14d19cad
2 changed files with 38 additions and 0 deletions

View File

@ -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;
}

View File

@ -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"