Browse Source

[Feature] Lua_util: table_digest can now recursively traverse tables

tags/2.0
Vsevolod Stakhov 4 years ago
parent
commit
0e83a5325c
1 changed files with 17 additions and 4 deletions
  1. 17
    4
      lualib/lua_util.lua

+ 17
- 4
lualib/lua_util.lua View File

@@ -1207,26 +1207,38 @@ end

---[[[
-- @function lua_util.table_digest(t)
-- 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

Loading…
Cancel
Save