From: Timo Sirainen Date: Thu, 30 May 2013 14:45:27 +0000 (+0300) Subject: var_expand(): Added %N, which is the same as %H except based on MD5. X-Git-Tag: 2.2.3~105 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e95bc848767afa2e52cb988a6d3f5e5cc5933885;p=thirdparty%2Fdovecot%2Fcore.git var_expand(): Added %N, which is the same as %H except based on MD5. This gives a better distribution of values than %H. --- diff --git a/src/lib/var-expand.c b/src/lib/var-expand.c index baec1e250c..0e8ec6aa04 100644 --- a/src/lib/var-expand.c +++ b/src/lib/var-expand.c @@ -87,6 +87,29 @@ static const char *m_str_hash(const char *str, struct var_expand_context *ctx) return str_c(hash); } +static const char * +m_str_newhash(const char *str, struct var_expand_context *ctx) +{ + string_t *hash = t_str_new(20); + unsigned char result[MD5_RESULTLEN]; + unsigned int value; + + md5_get_digest(str, strlen(str), result); + memcpy(&value, result, sizeof(value)); + + if (ctx->width != 0) { + value %= ctx->width; + ctx->width = 0; + } + + str_printfa(hash, "%x", value); + while ((int)str_len(hash) < ctx->offset) + str_insert(hash, 0, "0"); + ctx->offset = 0; + + return str_c(hash); +} + static const char * m_str_md5(const char *str, struct var_expand_context *ctx ATTR_UNUSED) { @@ -132,6 +155,7 @@ static const struct var_expand_modifier modifiers[] = { { 'X', m_str_hex }, { 'R', m_str_reverse }, { 'H', m_str_hash }, + { 'N', m_str_newhash }, { 'M', m_str_md5 }, { 'D', m_str_ldap_dn }, { 'T', m_str_trim },