diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-08-27 19:20:30 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-08-27 19:20:30 +0100 |
commit | 0e83a5325c17e77542b209ea7773e1129e87864b (patch) | |
tree | b886d1f97624cf7cdb9014231ecda9ea0eb946cb /lualib/lua_util.lua | |
parent | 259c79b98d420286271b3f02901771feb3a4a081 (diff) | |
download | rspamd-0e83a5325c17e77542b209ea7773e1129e87864b.tar.gz rspamd-0e83a5325c17e77542b209ea7773e1129e87864b.zip |
[Feature] Lua_util: table_digest can now recursively traverse tables
Diffstat (limited to 'lualib/lua_util.lua')
-rw-r--r-- | lualib/lua_util.lua | 22 |
1 files 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 |