Skip to content

Commit 65c5539

Browse files
basicernerzhul
authored andcommittedOct 30, 2017
Add sha1 to lua utils. (#6563)
1 parent a95e0d1 commit 65c5539

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed
 

Diff for: ‎doc/client_lua_api.md

+3
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,9 @@ Minetest namespace reference
630630
version entirely. To check for the presence of engine features, test
631631
whether the functions exported by the wanted features exist. For example:
632632
`if minetest.check_for_falling then ... end`.
633+
* `minetest.sha1(data, [raw])`: returns the sha1 hash of data
634+
* `data`: string of data to hash
635+
* `raw`: return raw bytes instead of hex digits, default: false
633636

634637
### Logging
635638
* `minetest.debug(...)`

Diff for: ‎doc/lua_api.txt

+3
Original file line numberDiff line numberDiff line change
@@ -2391,6 +2391,9 @@ Strings that need to be translated can contain several escapes, preceded by `@`.
23912391
version entirely. To check for the presence of engine features, test
23922392
whether the functions exported by the wanted features exist. For example:
23932393
`if minetest.check_for_falling then ... end`.
2394+
* `minetest.sha1(data, [raw])`: returns the sha1 hash of data
2395+
* `data`: string of data to hash
2396+
* `raw`: return raw bytes instead of hex digits, default: false
23942397

23952398
### Logging
23962399
* `minetest.debug(...)`

Diff for: ‎src/script/lua_api/l_util.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3737
#include "util/base64.h"
3838
#include "config.h"
3939
#include "version.h"
40+
#include "util/hex.h"
41+
#include "util/sha1.h"
4042
#include <algorithm>
4143

4244

@@ -423,6 +425,32 @@ int ModApiUtil::l_get_version(lua_State *L)
423425
return 1;
424426
}
425427

428+
int ModApiUtil::l_sha1(lua_State *L)
429+
{
430+
NO_MAP_LOCK_REQUIRED;
431+
size_t size;
432+
const char *data = luaL_checklstring(L, 1, &size);
433+
bool hex = !lua_isboolean(L, 2) || !lua_toboolean(L, 2);
434+
435+
// Compute actual checksum of data
436+
std::string data_sha1;
437+
{
438+
SHA1 ctx;
439+
ctx.addBytes(data, size);
440+
unsigned char *data_tmpdigest = ctx.getDigest();
441+
data_sha1.assign((char*) data_tmpdigest, 20);
442+
free(data_tmpdigest);
443+
}
444+
445+
if (hex) {
446+
std::string sha1_hex = hex_encode(data_sha1);
447+
lua_pushstring(L, sha1_hex.c_str());
448+
} else {
449+
lua_pushlstring(L, data_sha1.data(), data_sha1.size());
450+
}
451+
452+
return 1;
453+
}
426454

427455
void ModApiUtil::Initialize(lua_State *L, int top)
428456
{
@@ -455,6 +483,7 @@ void ModApiUtil::Initialize(lua_State *L, int top)
455483
API_FCT(decode_base64);
456484

457485
API_FCT(get_version);
486+
API_FCT(sha1);
458487

459488
LuaSettings::create(L, g_settings, g_settings_path);
460489
lua_setfield(L, top, "settings");
@@ -478,6 +507,7 @@ void ModApiUtil::InitializeClient(lua_State *L, int top)
478507
API_FCT(decode_base64);
479508

480509
API_FCT(get_version);
510+
API_FCT(sha1);
481511
}
482512

483513
void ModApiUtil::InitializeAsync(lua_State *L, int top)
@@ -503,6 +533,7 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top)
503533
API_FCT(decode_base64);
504534

505535
API_FCT(get_version);
536+
API_FCT(sha1);
506537

507538
LuaSettings::create(L, g_settings, g_settings_path);
508539
lua_setfield(L, top, "settings");

Diff for: ‎src/script/lua_api/l_util.h

+3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ class ModApiUtil : public ModApiBase
9292
// get_version()
9393
static int l_get_version(lua_State *L);
9494

95+
// sha1(string, raw)
96+
static int l_sha1(lua_State *L);
97+
9598
public:
9699
static void Initialize(lua_State *L, int top);
97100
static void InitializeAsync(lua_State *L, int top);

0 commit comments

Comments
 (0)
Please sign in to comment.