Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Sha256 algorithm to lua api #14259

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/lua_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5426,6 +5426,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
Expand Down
25 changes: 24 additions & 1 deletion src/script/lua_api/l_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -551,7 +552,7 @@ 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) || !readParam<bool>(L, 2);
const bool hex = !lua_isboolean(L, 2) || !readParam<bool>(L, 2);

// Compute actual checksum of data
std::string data_sha1;
Expand All @@ -573,6 +574,25 @@ int ModApiUtil::l_sha1(lua_State *L)
return 1;
}

int ModApiUtil::l_sha256(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
size_t size;
const char *data = luaL_checklstring(L, 1, &size);
const bool hex = !lua_isboolean(L, 2) || !readParam<bool>(L, 2);

std::string data_sha256;
data_sha256.assign((char *) SHA256((const unsigned char*) data, size, NULL), SHA256_DIGEST_LENGTH);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If md is NULL, the digest is placed
in a static array. Note: setting md to NULL is not thread safe.


if (hex) {
std::string sha256_hex = hex_encode(data_sha256);
lua_pushstring(L, sha256_hex.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)
{
Expand Down Expand Up @@ -699,6 +719,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);

Expand Down Expand Up @@ -732,6 +753,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);

Expand Down Expand Up @@ -772,6 +794,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);

Expand Down
4 changes: 3 additions & 1 deletion src/script/lua_api/l_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ class ModApiUtil : public ModApiBase
// 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);

Expand All @@ -130,7 +133,6 @@ class ModApiUtil : public ModApiBase

// urlencode(value)
static int l_urlencode(lua_State *L);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please keep this space

public:
static void Initialize(lua_State *L, int top);
static void InitializeAsync(lua_State *L, int top);
Expand Down