Commit d4242232 authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner

lua: Wrappers for md5 and sha256

parent 8fd993bd
......@@ -22,6 +22,8 @@
#include "util.h"
#include "events.h"
#include "journal.h"
#include "md5.h"
#include "sha256.h"
#include <lua.h>
#include <lualib.h>
......@@ -478,6 +480,31 @@ static int lua_stat(lua_State *L) {
return 1;
}
static void push_hex(lua_State *L, const uint8_t *buffer, size_t size) {
char result[2 * size];
for (size_t i = 0; i < size; i ++)
sprintf(result + 2 * i, "%02hhx", buffer[i]);
lua_pushlstring(L, result, 2 * size);
}
static int lua_md5(lua_State *L) {
size_t len;
const char *buffer = luaL_checklstring(L, 1, &len);
uint8_t result[MD5_DIGEST_SIZE];
md5_buffer(buffer, len, result);
push_hex(L, result, sizeof result);
return 1;
}
static int lua_sha256(lua_State *L) {
size_t len;
const char *buffer = luaL_checklstring(L, 1, &len);
uint8_t result[SHA256_DIGEST_SIZE];
sha256_buffer(buffer, len, result);
push_hex(L, result, sizeof result);
return 1;
}
struct injected_func {
int (*func)(lua_State *);
const char *name;
......@@ -493,7 +520,9 @@ static const struct injected_func injected_funcs[] = {
{ lua_mkdir, "mkdir" },
{ lua_move, "move" },
{ lua_ls, "ls" },
{ lua_stat, "stat" }
{ lua_stat, "stat" },
{ lua_md5, "md5" },
{ lua_sha256, "sha256" }
/*
* Note: watch_cancel is not provided, because it would be hell to
* manage the dynamically allocated memory correctly and there doesn't
......
......@@ -68,6 +68,11 @@ function test_fsutils()
assert_table_equal({["x"] = "r"}, ls(ldir))
end
function test_hashes()
assert_equal("5d41402abc4b2a76b9719d911017c592", md5("hello"))
assert_equal("2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", sha256("hello"))
end
function teardown()
utils.cleanup_dirs(tmp_dirs)
tmp_dirs = {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment