aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua/lua_cryptobox.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-05-03 11:11:15 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-05-03 11:11:15 +0100
commitdfb304e588d9a34da9cad2cb5855efca40283b93 (patch)
tree179793e440ec0658a524cb81e8425e9e450296c0 /src/lua/lua_cryptobox.c
parent773ef1f7414e60dfdd3a3ea73b89d9deda5e74d4 (diff)
downloadrspamd-dfb304e588d9a34da9cad2cb5855efca40283b93.tar.gz
rspamd-dfb304e588d9a34da9cad2cb5855efca40283b93.zip
[Feature] Add more encodingsto cryptobox hash API
Diffstat (limited to 'src/lua/lua_cryptobox.c')
-rw-r--r--src/lua/lua_cryptobox.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/lua/lua_cryptobox.c b/src/lua/lua_cryptobox.c
index 7f267fe71..9e699c2df 100644
--- a/src/lua/lua_cryptobox.c
+++ b/src/lua/lua_cryptobox.c
@@ -45,6 +45,8 @@ LUA_FUNCTION_DEF (cryptobox_hash, create);
LUA_FUNCTION_DEF (cryptobox_hash, create_keyed);
LUA_FUNCTION_DEF (cryptobox_hash, update);
LUA_FUNCTION_DEF (cryptobox_hash, hex);
+LUA_FUNCTION_DEF (cryptobox_hash, base32);
+LUA_FUNCTION_DEF (cryptobox_hash, base64);
LUA_FUNCTION_DEF (cryptobox_hash, bin);
LUA_FUNCTION_DEF (cryptobox_hash, gc);
LUA_FUNCTION_DEF (cryptobox, verify_memory);
@@ -106,6 +108,8 @@ static const struct luaL_reg cryptoboxhashlib_f[] = {
static const struct luaL_reg cryptoboxhashlib_m[] = {
LUA_INTERFACE_DEF (cryptobox_hash, update),
LUA_INTERFACE_DEF (cryptobox_hash, hex),
+ LUA_INTERFACE_DEF (cryptobox_hash, base32),
+ LUA_INTERFACE_DEF (cryptobox_hash, base64),
LUA_INTERFACE_DEF (cryptobox_hash, bin),
{"__tostring", rspamd_lua_class_tostring},
{"__gc", lua_cryptobox_hash_gc},
@@ -710,6 +714,57 @@ lua_cryptobox_hash_hex (lua_State *L)
}
/***
+ * @method cryptobox_hash:base32()
+ * Finalizes hash and return it as zbase32 string
+ * @return {string} base32 value of hash
+ */
+static gint
+lua_cryptobox_hash_base32 (lua_State *L)
+{
+ rspamd_cryptobox_hash_state_t *h = lua_check_cryptobox_hash (L, 1);
+ guchar out[rspamd_cryptobox_HASHBYTES],
+ out_b32[rspamd_cryptobox_HASHBYTES * 2];
+
+ if (h) {
+ memset (out_b32, 0, sizeof (out_b32));
+ rspamd_cryptobox_hash_final (h, out);
+ rspamd_encode_base32_buf (out, sizeof (out), out_b32, sizeof (out_b32));
+
+ lua_pushstring (L, out_b32);
+ }
+ else {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ return 1;
+}
+
+/***
+ * @method cryptobox_hash:base64()
+ * Finalizes hash and return it as base64 string
+ * @return {string} base64 value of hash
+ */
+static gint
+lua_cryptobox_hash_base64 (lua_State *L)
+{
+ rspamd_cryptobox_hash_state_t *h = lua_check_cryptobox_hash (L, 1);
+ guchar out[rspamd_cryptobox_HASHBYTES], *b64;
+ gsize len;
+
+ if (h) {
+ rspamd_cryptobox_hash_final (h, out);
+ b64 = rspamd_encode_base64 (out, sizeof (out), 0, &len);
+ lua_pushlstring (L, b64, len);
+ g_free (b64);
+ }
+ else {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ return 1;
+}
+
+/***
* @method cryptobox_hash:bin()
* Finalizes hash and return it as raw string
* @return {string} raw value of hash