From b8b5f8c7725d7431b82cb065b2b18891c1bc3fc9 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/pjw.c | 52 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 14 deletions(-) (limited to 'src/hash/pjw.c') diff --git a/src/hash/pjw.c b/src/hash/pjw.c index 50593b9..5dea872 100644 --- a/src/hash/pjw.c +++ b/src/hash/pjw.c @@ -4,28 +4,52 @@ #include #include -uint32_t pjw(uint8_t* in, size_t len){ - uint32_t hash = 0; - uint32_t high; - +struct pjw_hash pjw_init(){ + return (struct pjw_hash){.hash = 0, .high = 0}; +} + +void pjw_update(uint8_t* in, size_t len, struct pjw_hash* hash){ for(int i = 0; i != len; i++){ - hash = (hash << 4) + *in++; - if((high = (hash & 0xf0000000))) - hash ^= high >> 24; - hash &= ~high; + hash->hash = (hash->hash << 4) + *in++; + if((hash->high = (hash->hash & 0xf0000000))) + hash->hash ^= hash->high >> 24; + hash->hash &= ~hash->high; } +} - return hash; +uint32_t pjw_final(struct pjw_hash* hash){ + return hash->hash; } -int l_pjw(lua_State* L){ - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); +uint32_t pjw(uint8_t* in, size_t len){ + struct pjw_hash a = pjw_init(); + pjw_update(in, len, &a); + return pjw_final(&a); +} - char digest[32]; +common_hash_init_update(pjw); - uint32_t u = pjw(a, len); +int l_pjw_final(lua_State* L){ + lua_pushstring(L, "ud"); + lua_gettable(L, 1); + + struct pjw_hash* a = (struct pjw_hash*)lua_touserdata(L, -1); + uint32_t u = pjw_final(a); + char digest[32]; sprintf(digest,"%08x",u); lua_pushstring(L, digest); return 1; } + +int l_pjw(lua_State* L){ + if(lua_gettop(L) == 0) return l_pjw_init(L); + size_t len = 0; + uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); + + char digest[32]; + + uint32_t u = pjw(a, len); + sprintf(digest,"%08x",u); + lua_pushstring(L, digest); + return 1; +} -- cgit v1.2.3