From 762fca538c6a7a813e3f1ee10ce146bef1672dce Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sun, 25 Feb 2024 18:12:23 +0100 Subject: [PATCH] Expose SHA256 algorithm to Lua (#14403) Co-authored-by: chmodsayshello --- doc/lua_api.md | 3 +++ games/devtest/mods/unittests/misc.lua | 7 +++++++ src/script/lua_api/l_util.cpp | 25 +++++++++++++++++++++++++ src/script/lua_api/l_util.h | 3 +++ 4 files changed, 38 insertions(+) diff --git a/doc/lua_api.md b/doc/lua_api.md index 87537d1fd..2eefacc08 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -5484,6 +5484,9 @@ Utilities * `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 +* `minetest.sha256(data, [raw])`: returns the sha256 hash of data + * `data`: string of data to hash + * `raw`: return raw bytes instead of hex digits, default: false * `minetest.colorspec_to_colorstring(colorspec)`: Converts a ColorSpec to a ColorString. If the ColorSpec is invalid, returns `nil`. * `colorspec`: The ColorSpec to convert diff --git a/games/devtest/mods/unittests/misc.lua b/games/devtest/mods/unittests/misc.lua index d7620652b..034b99b52 100644 --- a/games/devtest/mods/unittests/misc.lua +++ b/games/devtest/mods/unittests/misc.lua @@ -111,6 +111,13 @@ unittests.register("test_punch_node", function(_, pos) -- currently failing: assert(on_punch_called) end, {map=true}) +local function test_hashing() + local input = "hello\000world" + assert(core.sha1(input) == "f85b420f1e43ebf88649dfcab302b898d889606c") + assert(core.sha256(input) == "b206899bc103669c8e7b36de29d73f95b46795b508aa87d612b2ce84bfb29df2") +end +unittests.register("test_hashing", test_hashing) + local function test_compress() -- This text should be compressible, to make sure the results are... normal local text = "The\000 icey canoe couldn't move very well on the\128 lake. The\000 ice was too stiff and the icey canoe's paddles simply wouldn't punch through." diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index 6a1cdc9df..f98eb0288 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -42,6 +42,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "version.h" #include "util/hex.h" #include "util/sha1.h" +#include "util/sha256.h" #include "util/png.h" #include @@ -566,6 +567,27 @@ int ModApiUtil::l_sha1(lua_State *L) return 1; } +int ModApiUtil::l_sha256(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + auto data = readParam(L, 1); + bool hex = !lua_isboolean(L, 2) || !readParam(L, 2); + + std::string data_sha256; + data_sha256.resize(SHA256_DIGEST_LENGTH); + SHA256(reinterpret_cast(data.data()), data.size(), + reinterpret_cast(data_sha256.data())); + + if (hex) { + lua_pushstring(L, hex_encode(data_sha256).c_str()); + } else { + lua_pushlstring(L, data_sha256.data(), data_sha256.size()); + } + + return 1; +} + // colorspec_to_colorstring(colorspec) int ModApiUtil::l_colorspec_to_colorstring(lua_State *L) { @@ -690,6 +712,7 @@ void ModApiUtil::Initialize(lua_State *L, int top) API_FCT(get_version); API_FCT(sha1); + API_FCT(sha256); API_FCT(colorspec_to_colorstring); API_FCT(colorspec_to_bytes); @@ -723,6 +746,7 @@ void ModApiUtil::InitializeClient(lua_State *L, int top) API_FCT(get_version); API_FCT(sha1); + API_FCT(sha256); API_FCT(colorspec_to_colorstring); API_FCT(colorspec_to_bytes); @@ -766,6 +790,7 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top) API_FCT(get_version); API_FCT(sha1); + API_FCT(sha256); API_FCT(colorspec_to_colorstring); API_FCT(colorspec_to_bytes); diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h index 07c4b86eb..056e09090 100644 --- a/src/script/lua_api/l_util.h +++ b/src/script/lua_api/l_util.h @@ -113,6 +113,9 @@ private: // sha1(string, raw) static int l_sha1(lua_State *L); + // sha256(string, raw) + static int l_sha256(lua_State *L); + // colorspec_to_colorstring(colorspec) static int l_colorspec_to_colorstring(lua_State *L);