From dae9e34168b56a399b2b1e04e657e322b9c6f803 Mon Sep 17 00:00:00 2001 From: ame Date: Thu, 4 Apr 2024 10:06:53 -0500 Subject: all basic hash funcs --- src/hash/xor.c | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) (limited to 'src/hash/xor.c') diff --git a/src/hash/xor.c b/src/hash/xor.c index 5fe23b7..1785414 100644 --- a/src/hash/xor.c +++ b/src/hash/xor.c @@ -2,23 +2,47 @@ #include #include -uint8_t i_xor8(uint8_t *aa, size_t len){ - uint8_t a = 0; +struct xor8_hash xor8_init(){ + return (struct xor8_hash){.a = 0}; +} + +void xor8_update(uint8_t* aa, size_t len, struct xor8_hash* hash){ + for(int i = 0; i != len; i++) + hash->a += aa[i] & 0xff; +} + +uint8_t xor8_final(struct xor8_hash* hash){ + return ((hash->a ^ 0xff) + 1) & 0xff; +} + +uint8_t xor8(uint8_t* aa, size_t len){ + struct xor8_hash a = xor8_init(); + xor8_update(aa, len, &a); + return xor8_final(&a); +} - for(int i = 0; i != len; i++) - a += aa[i] & 0xff; - - return ((a ^ 0xff) + 1) & 0xff; +common_hash_init_update(xor8); + +int l_xor8_final(lua_State* L){ + lua_pushstring(L, "ud"); + lua_gettable(L, 1); + + struct xor8_hash* a = (struct xor8_hash*)lua_touserdata(L, -1); + uint8_t u = xor8_final(a); + char digest[8]; + sprintf(digest,"%02x",u); + lua_pushstring(L, digest); + return 1; } int l_xor8(lua_State* L){ - + if(lua_gettop(L) == 0) return l_xor8_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); char digest[8]; - uint8_t u = i_xor8(a, len); + uint8_t u = xor8(a, len); sprintf(digest,"%02x",u); lua_pushstring(L, digest); -- cgit v1.2.3