aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua/lua_cryptobox.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2020-04-10 11:38:39 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2020-04-10 11:44:46 +0100
commitf8072344f9fb3fbc1ac93122f6cad11204c8cbf2 (patch)
tree29a3c5b33cdaf77138d7f753a97b04d6102c84c0 /src/lua/lua_cryptobox.c
parentbaecba4ce93e91b553e2d5d0d8e779dcd05e1d43 (diff)
downloadrspamd-f8072344f9fb3fbc1ac93122f6cad11204c8cbf2.tar.gz
rspamd-f8072344f9fb3fbc1ac93122f6cad11204c8cbf2.zip
[Feature] Allow multiple base32 encodings in Lua API
Diffstat (limited to 'src/lua/lua_cryptobox.c')
-rw-r--r--src/lua/lua_cryptobox.c31
1 files changed, 26 insertions, 5 deletions
diff --git a/src/lua/lua_cryptobox.c b/src/lua/lua_cryptobox.c
index 71c4655f4..07b4888b9 100644
--- a/src/lua/lua_cryptobox.c
+++ b/src/lua/lua_cryptobox.c
@@ -827,8 +827,9 @@ lua_cryptobox_signature_hex (lua_State *L)
}
/***
- * @method cryptobox_signature:base32()
+ * @method cryptobox_signature:base32([b32type='default'])
* Return base32 encoded signature string
+ * @param {string} b32type base32 type (default, bleach, rfc)
* @return {string} raw value of signature
*/
static gint
@@ -837,9 +838,18 @@ lua_cryptobox_signature_base32 (lua_State *L)
LUA_TRACE_POINT;
rspamd_fstring_t *sig = lua_check_cryptobox_sign (L, 1);
gchar *encoded;
+ enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT;
+
+ if (lua_type (L, 2) == LUA_TSTRING) {
+ btype = rspamd_base32_decode_type_from_str (lua_tostring (L, 2));
+
+ if (btype == RSPAMD_BASE32_INVALID) {
+ return luaL_error (L, "invalid b32 type: %s", lua_tostring (L, 2));
+ }
+ }
if (sig) {
- encoded = rspamd_encode_base32 (sig->str, sig->len, RSPAMD_BASE32_DEFAULT);
+ encoded = rspamd_encode_base32 (sig->str, sig->len, btype);
lua_pushstring (L, encoded);
g_free (encoded);
}
@@ -1365,8 +1375,9 @@ lua_cryptobox_hash_hex (lua_State *L)
}
/***
- * @method cryptobox_hash:base32()
- * Finalizes hash and return it as zbase32 string
+ * @method cryptobox_hash:base32([b32type])
+ * Finalizes hash and return it as zbase32 (by default) string
+ * @param {string} b32type base32 type (default, bleach, rfc)
* @return {string} base32 value of hash
*/
static gint
@@ -1379,6 +1390,16 @@ lua_cryptobox_hash_base32 (lua_State *L)
guint dlen;
if (h && !h->is_finished) {
+ enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT;
+
+ if (lua_type (L, 2) == LUA_TSTRING) {
+ btype = rspamd_base32_decode_type_from_str (lua_tostring (L, 2));
+
+ if (btype == RSPAMD_BASE32_INVALID) {
+ return luaL_error (L, "invalid b32 type: %s", lua_tostring (L, 2));
+ }
+ }
+
memset (out_b32, 0, sizeof (out_b32));
lua_cryptobox_hash_finish (h, out, &dlen);
r = out;
@@ -1392,7 +1413,7 @@ lua_cryptobox_hash_base32 (lua_State *L)
}
}
- rspamd_encode_base32_buf (r, dlen, out_b32, sizeof (out_b32), RSPAMD_BASE32_DEFAULT);
+ rspamd_encode_base32_buf (r, dlen, out_b32, sizeof (out_b32), btype);
lua_pushstring (L, out_b32);
h->is_finished = TRUE;
}