aboutsummaryrefslogtreecommitdiff
path: root/src/script/lua_api
diff options
context:
space:
mode:
authorRob Blanckaert <basicer@basicer.com>2017-10-30 00:18:18 -0700
committerSmallJoker <mk939@ymail.com>2018-06-03 17:32:00 +0200
commit4d9bf75d3ab6a181b3113c3231d2e41245795a60 (patch)
tree2905b213ab235a646080fd18c0a457827ec4263f /src/script/lua_api
parent416c4535c7e302191903c2a5c174de4f41a62e1c (diff)
downloadminetest-4d9bf75d3ab6a181b3113c3231d2e41245795a60.tar.gz
minetest-4d9bf75d3ab6a181b3113c3231d2e41245795a60.tar.bz2
minetest-4d9bf75d3ab6a181b3113c3231d2e41245795a60.zip
Add sha1 to lua utils. (#6563)
Diffstat (limited to 'src/script/lua_api')
-rw-r--r--src/script/lua_api/l_util.cpp31
-rw-r--r--src/script/lua_api/l_util.h3
2 files changed, 34 insertions, 0 deletions
diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp
index c4a988e07..901105fe0 100644
--- a/src/script/lua_api/l_util.cpp
+++ b/src/script/lua_api/l_util.cpp
@@ -36,6 +36,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/base64.h"
#include "config.h"
#include "version.h"
+#include "util/hex.h"
+#include "util/sha1.h"
#include <algorithm>
@@ -422,6 +424,32 @@ int ModApiUtil::l_get_version(lua_State *L)
return 1;
}
+int ModApiUtil::l_sha1(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ size_t size;
+ const char *data = luaL_checklstring(L, 1, &size);
+ bool hex = !lua_isboolean(L, 2) || !lua_toboolean(L, 2);
+
+ // Compute actual checksum of data
+ std::string data_sha1;
+ {
+ SHA1 ctx;
+ ctx.addBytes(data, size);
+ unsigned char *data_tmpdigest = ctx.getDigest();
+ data_sha1.assign((char*) data_tmpdigest, 20);
+ free(data_tmpdigest);
+ }
+
+ if (hex) {
+ std::string sha1_hex = hex_encode(data_sha1);
+ lua_pushstring(L, sha1_hex.c_str());
+ } else {
+ lua_pushlstring(L, data_sha1.data(), data_sha1.size());
+ }
+
+ return 1;
+}
void ModApiUtil::Initialize(lua_State *L, int top)
{
@@ -454,6 +482,7 @@ void ModApiUtil::Initialize(lua_State *L, int top)
API_FCT(decode_base64);
API_FCT(get_version);
+ API_FCT(sha1);
LuaSettings::create(L, g_settings, g_settings_path);
lua_setfield(L, top, "settings");
@@ -479,6 +508,7 @@ void ModApiUtil::InitializeClient(lua_State *L, int top)
API_FCT(decode_base64);
API_FCT(get_version);
+ API_FCT(sha1);
}
void ModApiUtil::InitializeAsync(lua_State *L, int top)
@@ -504,6 +534,7 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top)
API_FCT(decode_base64);
API_FCT(get_version);
+ API_FCT(sha1);
LuaSettings::create(L, g_settings, g_settings_path);
lua_setfield(L, top, "settings");
diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h
index b75d9db29..16f4edea8 100644
--- a/src/script/lua_api/l_util.h
+++ b/src/script/lua_api/l_util.h
@@ -93,6 +93,9 @@ private:
// get_version()
static int l_get_version(lua_State *L);
+ // sha1(string, raw)
+ static int l_sha1(lua_State *L);
+
public:
static void Initialize(lua_State *L, int top);
static void InitializeAsync(lua_State *L, int top);