aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorame <[email protected]>2024-03-26 13:36:42 -0500
committerame <[email protected]>2024-03-26 13:36:42 -0500
commitb1f57a05f31a04b6d0dfbdfaf358ce8adfd45dda (patch)
tree9121bda8e2fa09f13e7f4d78266ce755a69786e8
parentc6e8e66aa91bffa649e9192d3e4e0658d78d0ad6 (diff)
pretty:3
-rw-r--r--docs/crypto.md13
-rw-r--r--src/hash/adler.c21
-rw-r--r--tests/hash.lua6
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