commit f2a5e35: [Minor] Selectors: Fix digests and add compatibility with blake2b used in C

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Apr 30 12:28:06 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-04-30 12:37:37 +0100
URL: https://github.com/rspamd/rspamd/commit/f2a5e351ef0401b37010c844dff3e4374928aaa0

[Minor] Selectors: Fix digests and add compatibility with blake2b used in C

---
 lualib/lua_selectors/common.lua     | 16 +++++++++++++---
 lualib/lua_selectors/extractors.lua |  9 +++------
 2 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/lualib/lua_selectors/common.lua b/lualib/lua_selectors/common.lua
index e55bab84e..cb2ffc37f 100644
--- a/lualib/lua_selectors/common.lua
+++ b/lualib/lua_selectors/common.lua
@@ -16,6 +16,9 @@ limitations under the License.
 
 local ts = require("tableshape").types
 local exports = {}
+local cr_hash = require 'rspamd_cryptobox_hash'
+
+local blake2b_key = cr_hash.create_specific('blake2'):update('rspamd'):bin()
 
 local function digest_schema()
   return {ts.one_of{'hex', 'base32', 'bleach32', 'rbase32', 'base64'}:is_optional(),
@@ -25,11 +28,18 @@ end
 exports.digest_schema = digest_schema
 
 local function create_digest(data, args)
-  local hash = require 'rspamd_cryptobox_hash'
+
   local encoding = args[1] or 'hex'
   local ht = args[2] or 'blake2'
-  local h = hash:create_specific(ht):update(data)
-  local s
+
+  local h, s
+
+  if ht == 'blake2' then
+    -- Hack to be compatible with various 'get_digest' methods
+    h = cr_hash.create_keyed(blake2b_key):update(data)
+  else
+    h = cr_hash.create_specific(ht):update(data)
+  end
 
   if encoding == 'hex' then
     s = h:hex()
diff --git a/lualib/lua_selectors/extractors.lua b/lualib/lua_selectors/extractors.lua
index 11728c268..e0d692135 100644
--- a/lualib/lua_selectors/extractors.lua
+++ b/lualib/lua_selectors/extractors.lua
@@ -134,12 +134,9 @@ uses any type by default)]],
     ['get_value'] = function(task, args)
       local parts = task:get_parts() or E
       local digests = {}
-
-      if #args > 0 then
-        for _,p in ipairs(parts) do
-          if p:get_filename() then
-            table.insert(digests, common.create_digest(p:get_content('raw_parsed'), args))
-          end
+      for _,p in ipairs(parts) do
+        if p:get_filename() then
+          table.insert(digests, common.create_digest(p:get_content('raw_parsed'), args))
         end
       end
 


More information about the Commits mailing list