commit 0e83a53: [Feature] Lua_util: table_digest can now recursively traverse tables

Vsevolod Stakhov vsevolod at highsecure.ru
Wed Aug 28 07:35:04 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-08-27 19:20:30 +0100
URL: https://github.com/rspamd/rspamd/commit/0e83a5325c17e77542b209ea7773e1129e87864b

[Feature] Lua_util: table_digest can now recursively traverse tables

---
 lualib/lua_util.lua | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua
index 8f24b2871..79c031b3c 100644
--- a/lualib/lua_util.lua
+++ b/lualib/lua_util.lua
@@ -1207,26 +1207,38 @@ end
 
 ---[[[
 -- @function lua_util.table_digest(t)
--- Returns hash of all values if t[1] is string or all keys otherwise
+-- Returns hash of all values if t[1] is string or all keys/values otherwise
 -- @param {table} t input array or map
 -- @return {string} base32 representation of blake2b hash of all strings
 --]]]
-exports.table_digest = function(t)
+local function table_digest(t)
   local cr = require "rspamd_cryptobox_hash"
   local h = cr.create()
 
   if t[1] then
     for _,e in ipairs(t) do
-      h:update(tostring(e))
+      if type(e) == 'table' then
+        h:update(table_digest(e))
+      else
+        h:update(tostring(e))
+      end
     end
   else
-    for k,_ in pairs(t) do
-      h:update(k)
+    for k,v in pairs(t) do
+      h:update(tostring(k))
+
+      if type(v) == 'string' then
+        h:update(v)
+      elseif type(v) == 'table' then
+        h:update(table_digest(v))
+      end
     end
   end
  return h:base32()
 end
 
+exports.table_digest = table_digest
+
 ---[[[
 -- @function lua_util.toboolean(v)
 -- Converts a string or a number to boolean


More information about the Commits mailing list