]> git.lizzy.rs Git - minetest.git/commitdiff
Add sha1 to lua utils. (#6563)
authorRob Blanckaert <basicer@basicer.com>
Mon, 30 Oct 2017 07:18:18 +0000 (00:18 -0700)
committerSmallJoker <mk939@ymail.com>
Sun, 3 Jun 2018 15:32:00 +0000 (17:32 +0200)
doc/client_lua_api.md
doc/lua_api.txt
src/script/lua_api/l_util.cpp
src/script/lua_api/l_util.h

index ab72bbccf94254cecfadf035c5e678e181e73476..8ef3bfb2effb943d2498187b5b48532754d44253 100644 (file)
@@ -628,6 +628,9 @@ Minetest namespace reference
   version entirely. To check for the presence of engine features, test
   whether the functions exported by the wanted features exist. For example:
   `if minetest.nodeupdate then ... end`.
+* `minetest.sha1(data, [raw])`: returns the sha1 hash of data
+    * `data`: string of data to hash
+    * `raw`: return raw bytes instead of hex digits, default: false
 
 ### Logging
 * `minetest.debug(...)`
index d6b8fc5bba986fe6b917d8f32e77231956b369fc..13881ef700d7f37f83068e1d7d4bb950f8619683 100644 (file)
@@ -2234,6 +2234,9 @@ Helper functions
   version entirely. To check for the presence of engine features, test
   whether the functions exported by the wanted features exist. For example:
   `if minetest.nodeupdate then ... end`.
+* `minetest.sha1(data, [raw])`: returns the sha1 hash of data
+    * `data`: string of data to hash
+    * `raw`: return raw bytes instead of hex digits, default: false
 
 ### Logging
 * `minetest.debug(...)`
index c4a988e0758fc3c07cc438c13683d208d0a5e2a7..901105fe07c8c6e0c77a175098fc736be99d6948 100644 (file)
@@ -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");
index b75d9db29ae9dd5c83c1d152112b310a16971989..16f4edea89d5854197d0b0b658e225622d49397d 100644 (file)
@@ -93,6 +93,9 @@ class ModApiUtil : public ModApiBase
        // 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);