Skip to content

Commit

Permalink
Added LuaSha1 for SHA-1 calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
madmaxoft committed May 30, 2022
1 parent 795fbc5 commit 5a5b911
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ add_subdirectory(lsqlite3)
add_subdirectory(lua-cjson)
add_subdirectory(fmt)
add_subdirectory(LuaSimpleWinHttp)
add_subdirectory(LuaSha1)
1 change: 1 addition & 0 deletions lib/LuaSha1
Submodule LuaSha1 added at db79c4
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ target_link_libraries(LunaPaak
lsqlite3-static
cjson-static
LuaSimpleWinHttp-static
LuaSha1-static
)


Expand Down
3 changes: 3 additions & 0 deletions src/LunaPaak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern "C"
#include <lauxlib.h>
#include <lfs.h>
#include <LuaSimpleWinHttp.h>
#include <LuaSha1.h>

// lzlib fwd:
extern int luaopen_zlib(lua_State *);
Expand Down Expand Up @@ -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:
Expand Down
36 changes: 36 additions & 0 deletions tests/LuaSha1.luna
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 5a5b911

Please sign in to comment.