diff options
| author | ame <[email protected]> | 2024-03-28 12:41:17 -0500 |
|---|---|---|
| committer | ame <[email protected]> | 2024-03-28 12:41:17 -0500 |
| commit | cb280d5d1816ddbe0587775def896ab9c237bc4f (patch) | |
| tree | fc3dcf74e40ade1ebeb8cbb270fe28f8ce7b63b5 /src/hash/djb2.c | |
| parent | e8654eece8832495f7e1189a866340227082a5ec (diff) | |
more hashes fixed
Diffstat (limited to 'src/hash/djb2.c')
| -rw-r--r-- | src/hash/djb2.c | 39 |
1 files changed, 32 insertions, 7 deletions
diff --git a/src/hash/djb2.c b/src/hash/djb2.c index 89f3a00..cd1fb2a 100644 --- a/src/hash/djb2.c +++ b/src/hash/djb2.c @@ -2,18 +2,43 @@ #include <stdio.h> #include <stdint.h> -uint32_t djb2(uint8_t* in, size_t len){ - uint32_t hash = 5381; +struct djb2_hash djb2_init(){ + return (struct djb2_hash){.hash = 5381}; +} + +void djb2_update(uint8_t * in, size_t len, struct djb2_hash * hash){ + for(int i = 0; i != len; i++){ + hash->hash = ((hash->hash << 5) + hash->hash) + (uint32_t)*in; + in++; + } +} + +uint32_t djb2_final(struct djb2_hash * hash){ + return hash->hash; +} + +uint32_t djb2(uint8_t * in, size_t len){ + struct djb2_hash a = djb2_init(); + djb2_update(in, len, &a); + return djb2_final(&a); +} + +common_hash_init_update(djb2); - for(int i = 0; i != len; i++){ - hash = ((hash << 5) + hash) + (uint32_t)*in; - in++; - } +int l_djb2_final(lua_State* L){ + lua_pushstring(L, "ud"); + lua_gettable(L, 1); - return hash; + struct djb2_hash* a = (struct djb2_hash*)lua_touserdata(L, -1); + uint32_t u = djb2_final(a); + char digest[64]; + sprintf(digest,"%08x",u); + lua_pushstring(L, digest); + return 1; } int l_djb2(lua_State* L){ + if(lua_gettop(L) == 0) return l_djb2_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); |
