From 5a5b911f0b6eb47ac54836bc3c70ed1d61544f18 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Mon, 30 May 2022 23:06:29 +0200 Subject: [PATCH] Added LuaSha1 for SHA-1 calculation. --- .gitmodules | 3 +++ CMakeLists.txt | 1 + lib/CMakeLists.txt | 1 + lib/LuaSha1 | 1 + src/CMakeLists.txt | 1 + src/LunaPaak.cpp | 3 +++ tests/LuaSha1.luna | 36 ++++++++++++++++++++++++++++++++++++ 7 files changed, 46 insertions(+) create mode 160000 lib/LuaSha1 create mode 100644 tests/LuaSha1.luna diff --git a/.gitmodules b/.gitmodules index 6108218..1a1c5c9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -25,3 +25,6 @@ [submodule "lib/fmt"] path = lib/fmt url = https://github.com/madmaxoft/fmt +[submodule "lib/LuaSha1"] + path = lib/LuaSha1 + url = https://github.com/madmaxoft/LuaSha1 diff --git a/CMakeLists.txt b/CMakeLists.txt index a83a28e..859483c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,3 +43,4 @@ add_subdirectory (src) # Want to build some modules as dynamic, not for this project, but for others to use: set_target_properties(LuaSimpleWinHttp PROPERTIES EXCLUDE_FROM_ALL false) +set_target_properties(LuaSha1 PROPERTIES EXCLUDE_FROM_ALL false) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 1a4a1f3..7175a72 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -13,3 +13,4 @@ add_subdirectory(lsqlite3) add_subdirectory(lua-cjson) add_subdirectory(fmt) add_subdirectory(LuaSimpleWinHttp) +add_subdirectory(LuaSha1) diff --git a/lib/LuaSha1 b/lib/LuaSha1 new file mode 160000 index 0000000..db79c40 --- /dev/null +++ b/lib/LuaSha1 @@ -0,0 +1 @@ +Subproject commit db79c40585d771b04a8835b7ef82390dac10d236 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9c517e0..b04c313 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -45,6 +45,7 @@ target_link_libraries(LunaPaak lsqlite3-static cjson-static LuaSimpleWinHttp-static + LuaSha1-static ) diff --git a/src/LunaPaak.cpp b/src/LunaPaak.cpp index ee55769..5756986 100644 --- a/src/LunaPaak.cpp +++ b/src/LunaPaak.cpp @@ -12,6 +12,7 @@ extern "C" #include #include #include + #include // lzlib fwd: extern int luaopen_zlib(lua_State *); @@ -247,6 +248,8 @@ int main(int argc, char * argv[]) luaopen_cjson_safe(L); lua_pop(L, 1); luaopen_LuaSimpleWinHttp(L); + lua_pop(L, 1); + luaopen_LuaSha1(L); lua_settop(L, 0); // Trim off all excess values left over by the reg functions // Store the args to the script in a separate "arg" global: diff --git a/tests/LuaSha1.luna b/tests/LuaSha1.luna new file mode 100644 index 0000000..64c073e --- /dev/null +++ b/tests/LuaSha1.luna @@ -0,0 +1,36 @@ +local sha1 = require("LuaSha1") + + + + +--- Hashes the plaintext, then compares the resulting hash with the expected hash, erroring if they differ +local function testHashEqual(aPlainText, aExpectedHexHash) + assert(type(aPlainText) == "string") + assert(type(aExpectedHexHash) == "string") + + local hash = sha1.calc(aPlainText) + if (hash ~= aExpectedHexHash) then + print("Hashing string '" .. aPlainText .. "':") + print("Got hash '" .. hash .. "'") + print("Expected '" .. aExpectedHexHash .."'") + error("Hashing failed") + end +end + +testHashEqual( + "abc", + "a9993e364706816aba3e25717850c26c9cd0d89d" +) + +testHashEqual( + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + "a49b2446a02c645bf419f995b67091253a04a259" +); +testHashEqual( + "The quick brown fox jumps over the lazy dog", + "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12" +); + + +-- Test hashing multiple values: +assert(sha1.calc("a", "b", "c") == "a9993e364706816aba3e25717850c26c9cd0d89d")