]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
var_expand(): Added %N, which is the same as %H except based on MD5.
authorTimo Sirainen <tss@iki.fi>
Thu, 30 May 2013 14:45:27 +0000 (17:45 +0300)
committerTimo Sirainen <tss@iki.fi>
Thu, 30 May 2013 14:45:27 +0000 (17:45 +0300)
This gives a better distribution of values than %H.

src/lib/var-expand.c

index baec1e250c19f4a7d64a2e1ff988e2185a5148f3..0e8ec6aa049363a158cfa5bee314a5497d93ff8d 100644 (file)
@@ -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 },