aboutsummaryrefslogtreecommitdiffstats
path: root/lualib/lua_selectors
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2020-04-30 12:11:57 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2020-04-30 12:11:57 +0100
commitf7b9b38901fe7623006f0c8dd525d3ec5b07f56b (patch)
treef339d785c6c9e2e273afcbad453ede9593d80e6f /lualib/lua_selectors
parentab5767eb8b54344e6ab7546a022a108024e270a2 (diff)
downloadrspamd-f7b9b38901fe7623006f0c8dd525d3ec5b07f56b.tar.gz
rspamd-f7b9b38901fe7623006f0c8dd525d3ec5b07f56b.zip
[Minor] Add common selectors functions
Diffstat (limited to 'lualib/lua_selectors')
-rw-r--r--lualib/lua_selectors/common.lua51
1 files changed, 51 insertions, 0 deletions
diff --git a/lualib/lua_selectors/common.lua b/lualib/lua_selectors/common.lua
new file mode 100644
index 000000000..e55bab84e
--- /dev/null
+++ b/lualib/lua_selectors/common.lua
@@ -0,0 +1,51 @@
+--[[
+Copyright (c) 2020, Vsevolod Stakhov <vsevolod@highsecure.ru>
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+]]--
+
+local ts = require("tableshape").types
+local exports = {}
+
+local function digest_schema()
+ return {ts.one_of{'hex', 'base32', 'bleach32', 'rbase32', 'base64'}:is_optional(),
+ ts.one_of{'blake2', 'sha256', 'sha1', 'sha512', 'md5'}:is_optional()}
+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
+
+ if encoding == 'hex' then
+ s = h:hex()
+ elseif encoding == 'base32' then
+ s = h:base32()
+ elseif encoding == 'bleach32' then
+ s = h:base32('bleach')
+ elseif encoding == 'rbase32' then
+ s = h:base32('rfc')
+ elseif encoding == 'base64' then
+ s = h:base64()
+ end
+
+ return s
+end
+
+exports.create_digest = create_digest
+
+return exports \ No newline at end of file