From 5d3677f48bf7b0493bab5cae6f7928f9a878f320 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 4 Jul 2018 14:56:47 +0100 Subject: [PATCH] [Minor] Use proper hashing when getting texts fuzzy hash --- lualib/rspamadm/mime.lua | 22 ++++++++++++++++------ src/lua/lua_mimepart.c | 4 +++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lualib/rspamadm/mime.lua b/lualib/rspamadm/mime.lua index f31fe29f1..f41529646 100644 --- a/lualib/rspamadm/mime.lua +++ b/lualib/rspamadm/mime.lua @@ -325,12 +325,22 @@ local function stat_handler(opts) end for _,part in ipairs(parts) do if not part:is_multipart() then - table.insert(out_elts[fname], { - digest = part:get_digest(), - file = part:get_filename(), - type = string.format('%s/%s', part:get_type()) - }) - + local text = part:get_text() + + if text then + local digest,shingles = text:get_fuzzy_hashes(task:get_mempool()) + table.insert(out_elts[fname], { + digest = digest, + shingles = rspamd_logger.slog('%s', shingles), + type = string.format('%s/%s', part:get_type()) + }) + else + table.insert(out_elts[fname], { + digest = part:get_digest(), + file = part:get_filename(), + type = string.format('%s/%s', part:get_type()) + }) + end end end end diff --git a/src/lua/lua_mimepart.c b/src/lua/lua_mimepart.c index 822df2f36..dd4e998a1 100644 --- a/src/lua/lua_mimepart.c +++ b/src/lua/lua_mimepart.c @@ -906,6 +906,8 @@ lua_textpart_get_fuzzy_hashes (lua_State * L) /* TODO: add short text support */ /* Calculate direct hash */ + rspamd_cryptobox_hash_init (&st, key, rspamd_cryptobox_HASHKEYBYTES); + for (i = 0; i < part->normalized_words->len; i ++) { word = &g_array_index (part->normalized_words, rspamd_stat_token_t, i); rspamd_cryptobox_hash_update (&st, word->begin, word->len); @@ -915,7 +917,7 @@ lua_textpart_get_fuzzy_hashes (lua_State * L) rspamd_encode_hex_buf (digest, sizeof (digest), hexdigest, sizeof (hexdigest)); - lua_pushstring (L, hexdigest); + lua_pushlstring (L, hexdigest, sizeof (hexdigest) - 1); sgl = rspamd_shingles_from_text (part->normalized_words, key, pool, lua_shingles_filter, part, RSPAMD_SHINGLES_MUMHASH); -- 2.39.5