Skip to content

Commit

Permalink
Add sha1 to lua utils. (#6563)
Browse files Browse the repository at this point in the history
  • Loading branch information
basicer authored and nerzhul committed Oct 30, 2017
1 parent a95e0d1 commit 65c5539
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/client_lua_api.md
Expand Up @@ -630,6 +630,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.check_for_falling 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(...)`
Expand Down
3 changes: 3 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -2391,6 +2391,9 @@ Strings that need to be translated can contain several escapes, preceded by `@`.
version entirely. To check for the presence of engine features, test
whether the functions exported by the wanted features exist. For example:
`if minetest.check_for_falling 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(...)`
Expand Down
31 changes: 31 additions & 0 deletions src/script/lua_api/l_util.cpp
Expand Up @@ -37,6 +37,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>


Expand Down Expand Up @@ -423,6 +425,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)
{
Expand Down Expand Up @@ -455,6 +483,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");
Expand All @@ -478,6 +507,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)
Expand All @@ -503,6 +533,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");
Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_util.h
Expand Up @@ -92,6 +92,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);
Expand Down

0 comments on commit 65c5539

Please sign in to comment.