1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Expose SHA256 algorithm to Lua (#14403)

Co-authored-by: chmodsayshello <chmodsayshello@hotmail.com>
This commit is contained in:
sfan5 2024-02-25 18:12:23 +01:00 committed by GitHub
parent fa1d80b53b
commit 762fca538c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 38 additions and 0 deletions

View file

@ -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 <cstdio>
@ -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<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)
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);

View file

@ -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);