From b1f57a05f31a04b6d0dfbdfaf358ce8adfd45dda Mon Sep 17 00:00:00 2001 From: ame Date: Tue, 26 Mar 2024 13:36:42 -0500 Subject: pretty:3 --- docs/crypto.md | 13 +++++++++++++ src/hash/adler.c | 21 +++++++++++++++++++-- tests/hash.lua | 6 ++---- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/docs/crypto.md b/docs/crypto.md index 06689d9..1f71f43 100644 --- a/docs/crypto.md +++ b/docs/crypto.md @@ -75,6 +75,19 @@ llib.crypto.sha512("meow") -- e88348269bad036160f0d9558b7c5de68163b50e1a6ce46e85 llib.crypto.sha512_t("meow", 224) -- would be sha512/224 - ad5e403e0d74532187f4e1665c7e705ab5eb3c2fe07ae73a3ff998b2 ``` +functions supporting updates (listed with %, see note above) can be used like so: + +```lua +obj = llib.crypto.adler32_init() +llib.crypto.adler32_update(obj, "meow") +local hash = llib.crypto.adler32_final(obj) --043c01b9 + +--or you can chain them! +obj = llib.crypto.adler32_init() +obj:update("meow") +hash = obj:final() --043c01b9s (the same) +``` + ## en/decoding all functions have 1 argument which is a string, unless noted otherwise diff --git a/src/hash/adler.c b/src/hash/adler.c index ed7a84e..7d922e9 100644 --- a/src/hash/adler.c +++ b/src/hash/adler.c @@ -24,23 +24,40 @@ uint32_t adler32(uint8_t* aa, size_t len){ } int l_adler32_init(lua_State* L){ + lua_newtable(L); + int t = lua_gettop(L); + struct adler32_hash* a = (struct adler32_hash*)lua_newuserdata(L, sizeof * a); + int ud = lua_gettop(L); *a = adler32_init(); + + luaI_tsetv(L, t, "ud", ud); + luaI_tsetcf(L, t, "update", l_adler32_update); + luaI_tsetcf(L, t, "final", l_adler32_final); + + lua_pushvalue(L, t); return 1; } int l_adler32_update(lua_State* L){ - struct adler32_hash* a = (struct adler32_hash*)lua_touserdata(L, 1); + lua_pushstring(L, "ud"); + lua_gettable(L, 1); + + struct adler32_hash* a = (struct adler32_hash*)lua_touserdata(L, -1); size_t len = 0; uint8_t* b = (uint8_t*)luaL_checklstring(L, 2, &len); adler32_update(b, len, a); + lua_pushvalue(L, 1); return 1; } int l_adler32_final(lua_State* L){ - struct adler32_hash* a = (struct adler32_hash*)lua_touserdata(L, 1); + lua_pushstring(L, "ud"); + lua_gettable(L, 1); + + struct adler32_hash* a = (struct adler32_hash*)lua_touserdata(L, -1); uint32_t u = adler32_final(a); char digest[32]; sprintf(digest,"%08x",u); diff --git a/tests/hash.lua b/tests/hash.lua index ca9d85c..67155d3 100644 --- a/tests/hash.lua +++ b/tests/hash.lua @@ -12,13 +12,11 @@ function test(name,b,exp,oargs) end if(llib.crypto[name.."_init"] ~= nil) then - local t = llib.crypto[name.."_init"]() - llib.crypto[name.."_update"](t, b) - hash2 = llib.crypto[name.."_final"](t) + hash2 = llib.crypto[name.."_init"]():update(b):final() if(hash2 ~= hash) then llib.io.error(name.." init-update-final method not working, got:\n\t"..hash2.." other was:\n\t"..hash) else - llib.io.log(name.." alt method working") + llib.io.log(name.." alt method working "..hash2.." == "..hash) end end -- cgit v1.2.3