forked from Mirrorlandia_minetest/minetest
Expose SHA256 algorithm to Lua (#14403)
Co-authored-by: chmodsayshello <chmodsayshello@hotmail.com>
This commit is contained in:
parent
fa1d80b53b
commit
762fca538c
@ -5484,6 +5484,9 @@ Utilities
|
|||||||
* `minetest.sha1(data, [raw])`: returns the sha1 hash of data
|
* `minetest.sha1(data, [raw])`: returns the sha1 hash of data
|
||||||
* `data`: string of data to hash
|
* `data`: string of data to hash
|
||||||
* `raw`: return raw bytes instead of hex digits, default: false
|
* `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
|
* `minetest.colorspec_to_colorstring(colorspec)`: Converts a ColorSpec to a
|
||||||
ColorString. If the ColorSpec is invalid, returns `nil`.
|
ColorString. If the ColorSpec is invalid, returns `nil`.
|
||||||
* `colorspec`: The ColorSpec to convert
|
* `colorspec`: The ColorSpec to convert
|
||||||
|
@ -111,6 +111,13 @@ unittests.register("test_punch_node", function(_, pos)
|
|||||||
-- currently failing: assert(on_punch_called)
|
-- currently failing: assert(on_punch_called)
|
||||||
end, {map=true})
|
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()
|
local function test_compress()
|
||||||
-- This text should be compressible, to make sure the results are... normal
|
-- 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."
|
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."
|
||||||
|
@ -42,6 +42,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
#include "version.h"
|
#include "version.h"
|
||||||
#include "util/hex.h"
|
#include "util/hex.h"
|
||||||
#include "util/sha1.h"
|
#include "util/sha1.h"
|
||||||
|
#include "util/sha256.h"
|
||||||
#include "util/png.h"
|
#include "util/png.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
@ -566,6 +567,27 @@ int ModApiUtil::l_sha1(lua_State *L)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ModApiUtil::l_sha256(lua_State *L)
|
||||||
|
{
|
||||||
|
NO_MAP_LOCK_REQUIRED;
|
||||||
|
|
||||||
|
auto data = readParam<std::string_view>(L, 1);
|
||||||
|
bool hex = !lua_isboolean(L, 2) || !readParam<bool>(L, 2);
|
||||||
|
|
||||||
|
std::string data_sha256;
|
||||||
|
data_sha256.resize(SHA256_DIGEST_LENGTH);
|
||||||
|
SHA256(reinterpret_cast<const unsigned char*>(data.data()), data.size(),
|
||||||
|
reinterpret_cast<unsigned char *>(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)
|
// colorspec_to_colorstring(colorspec)
|
||||||
int ModApiUtil::l_colorspec_to_colorstring(lua_State *L)
|
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(get_version);
|
||||||
API_FCT(sha1);
|
API_FCT(sha1);
|
||||||
|
API_FCT(sha256);
|
||||||
API_FCT(colorspec_to_colorstring);
|
API_FCT(colorspec_to_colorstring);
|
||||||
API_FCT(colorspec_to_bytes);
|
API_FCT(colorspec_to_bytes);
|
||||||
|
|
||||||
@ -723,6 +746,7 @@ void ModApiUtil::InitializeClient(lua_State *L, int top)
|
|||||||
|
|
||||||
API_FCT(get_version);
|
API_FCT(get_version);
|
||||||
API_FCT(sha1);
|
API_FCT(sha1);
|
||||||
|
API_FCT(sha256);
|
||||||
API_FCT(colorspec_to_colorstring);
|
API_FCT(colorspec_to_colorstring);
|
||||||
API_FCT(colorspec_to_bytes);
|
API_FCT(colorspec_to_bytes);
|
||||||
|
|
||||||
@ -766,6 +790,7 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top)
|
|||||||
|
|
||||||
API_FCT(get_version);
|
API_FCT(get_version);
|
||||||
API_FCT(sha1);
|
API_FCT(sha1);
|
||||||
|
API_FCT(sha256);
|
||||||
API_FCT(colorspec_to_colorstring);
|
API_FCT(colorspec_to_colorstring);
|
||||||
API_FCT(colorspec_to_bytes);
|
API_FCT(colorspec_to_bytes);
|
||||||
|
|
||||||
|
@ -113,6 +113,9 @@ private:
|
|||||||
// sha1(string, raw)
|
// sha1(string, raw)
|
||||||
static int l_sha1(lua_State *L);
|
static int l_sha1(lua_State *L);
|
||||||
|
|
||||||
|
// sha256(string, raw)
|
||||||
|
static int l_sha256(lua_State *L);
|
||||||
|
|
||||||
// colorspec_to_colorstring(colorspec)
|
// colorspec_to_colorstring(colorspec)
|
||||||
static int l_colorspec_to_colorstring(lua_State *L);
|
static int l_colorspec_to_colorstring(lua_State *L);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user