aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua/lua_util.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-06-10 09:27:25 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-06-10 09:27:25 +0100
commit42c732f32a45f4f3dbea262eff6b251a839eab15 (patch)
treed14f27bd725f7ee3beedebbabd4079cd64e97424 /src/lua/lua_util.c
parentfb326efc2b3fa1c25705d218987199a608b87b87 (diff)
downloadrspamd-42c732f32a45f4f3dbea262eff6b251a839eab15.tar.gz
rspamd-42c732f32a45f4f3dbea262eff6b251a839eab15.zip
[Feature] Add function to generate random hex string
Diffstat (limited to 'src/lua/lua_util.c')
-rw-r--r--src/lua/lua_util.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c
index a5b5eac84..272e39463 100644
--- a/src/lua/lua_util.c
+++ b/src/lua/lua_util.c
@@ -324,6 +324,15 @@ LUA_FUNCTION_DEF (util, create_file);
*/
LUA_FUNCTION_DEF (util, close_file);
+/**
+ * @function util.random_hex(size)
+ * Returns random hex string of the specified size
+ *
+ * @param {number} len length of desired string in bytes
+ * @return {string} string with random hex digests
+ */
+LUA_FUNCTION_DEF (util, random_hex);
+
static const struct luaL_reg utillib_f[] = {
LUA_INTERFACE_DEF (util, create_event_base),
LUA_INTERFACE_DEF (util, load_rspamd_config),
@@ -358,6 +367,7 @@ static const struct luaL_reg utillib_f[] = {
LUA_INTERFACE_DEF (util, unlock_file),
LUA_INTERFACE_DEF (util, create_file),
LUA_INTERFACE_DEF (util, close_file),
+ LUA_INTERFACE_DEF (util, random_hex),
{NULL, NULL}
};
@@ -1471,6 +1481,26 @@ lua_util_close_file (lua_State *L)
}
static gint
+lua_util_random_hex (lua_State *L)
+{
+ gchar *buf;
+ gint buflen;
+
+ buflen = lua_tonumber (L, 1);
+
+ if (buflen <= 0) {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ buf = g_malloc (buflen);
+ rspamd_random_hex (buf, buflen);
+ lua_pushlstring (L, buf, buflen);
+ g_free (buf);
+
+ return 1;
+}
+
+static gint
lua_load_util (lua_State * L)
{
lua_newtable (L);