/*** * @method cryptobox_hash:bin() * Finalizes hash and return it as raw string * @return {string} raw value of hash */ static gint lua_cryptobox_hash_bin (lua_State *L) { struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash (L, 1); guchar out[rspamd_cryptobox_HASHBYTES]; guint dlen; if (h && !h->is_finished) { if (h->is_ssl) { dlen = sizeof (out); EVP_DigestFinal_ex (h->c, out, &dlen); } else { dlen = sizeof (out); rspamd_cryptobox_hash_final (h->h, out); } lua_pushlstring (L, out, sizeof (out)); h->is_finished = TRUE; } 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) { struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash (L, 1); guchar out[rspamd_cryptobox_HASHBYTES], *b64; gsize len; guint dlen; if (h && !h->is_finished) { if (h->is_ssl) { dlen = sizeof (out); EVP_DigestFinal_ex (h->c, out, &dlen); } else { dlen = sizeof (out); rspamd_cryptobox_hash_final (h->h, out); } b64 = rspamd_encode_base64 (out, dlen, 0, &len); lua_pushlstring (L, b64, len); g_free (b64); h->is_finished = TRUE; } else { return luaL_error (L, "invalid arguments"); } return 1; }
/*** * @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) { struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash (L, 1); guchar out[rspamd_cryptobox_HASHBYTES], out_b32[rspamd_cryptobox_HASHBYTES * 2]; guint dlen; if (h && !h->is_finished) { memset (out_b32, 0, sizeof (out_b32)); if (h->is_ssl) { dlen = sizeof (out); EVP_DigestFinal_ex (h->c, out, &dlen); } else { dlen = sizeof (out); rspamd_cryptobox_hash_final (h->h, out); } rspamd_encode_base32_buf (out, dlen, out_b32, sizeof (out_b32)); lua_pushstring (L, out_b32); h->is_finished = TRUE; } else { return luaL_error (L, "invalid arguments"); } return 1; }
static gint lua_cryptobox_hash_gc (lua_State *L) { rspamd_cryptobox_hash_state_t *h = lua_check_cryptobox_hash (L, 1); rspamd_explicit_memzero (h, sizeof (*h)); g_slice_free1 (sizeof (*h), h); return 0; }
/*** * @method cryptobox_hash:bin() * Finalizes hash and return it as raw string * @return {string} raw value of hash */ static gint lua_cryptobox_hash_bin (lua_State *L) { rspamd_cryptobox_hash_state_t *h = lua_check_cryptobox_hash (L, 1); guchar out[rspamd_cryptobox_HASHBYTES]; if (h) { rspamd_cryptobox_hash_final (h, out); lua_pushlstring (L, out, sizeof (out)); } 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:update(data) * Updates hash with the specified data (hash should not be finalized using `hex` or `bin` methods) * @param {string} data data to hash */ static gint lua_cryptobox_hash_update (lua_State *L) { rspamd_cryptobox_hash_state_t *h = lua_check_cryptobox_hash (L, 1); const gchar *data; struct rspamd_lua_text *t; gsize len; if (lua_isuserdata (L, 2)) { t = lua_check_text (L, 2); if (!t) { return luaL_error (L, "invalid arguments"); } data = t->start; len = t->len; } else { data = luaL_checklstring (L, 2, &len); } if (lua_isnumber (L, 3)) { gsize nlen = lua_tonumber (L, 3); if (nlen > len) { return luaL_error (L, "invalid length: %d while %d is available", (int)nlen, (int)len); } len = nlen; } if (h && data) { rspamd_cryptobox_hash_update (h, data, len); } else { return luaL_error (L, "invalid arguments"); } return 0; }
/*** * @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:reset() * Resets hash to the initial state */ static gint lua_cryptobox_hash_reset (lua_State *L) { struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash (L, 1); if (h) { if (h->is_ssl) { EVP_DigestInit (h->c, EVP_MD_CTX_md (h->c)); } else { memset (h->h, 0, sizeof (*h->h)); rspamd_cryptobox_hash_init (h->h, NULL, 0); } h->is_finished = FALSE; } else { return luaL_error (L, "invalid arguments"); } return 0; }
static gint lua_cryptobox_hash_gc (lua_State *L) { struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash (L, 1); if (h->is_ssl) { #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) EVP_MD_CTX_cleanup (h->c); #else EVP_MD_CTX_reset (h->c); #endif EVP_MD_CTX_destroy (h->c); } else { rspamd_explicit_memzero (h->h, sizeof (*h->h)); g_slice_free1 (sizeof (*h->h), h->h); } g_slice_free1 (sizeof (*h), h); return 0; }