diff options
| author | ame <[email protected]> | 2024-03-27 13:34:34 -0500 |
|---|---|---|
| committer | ame <[email protected]> | 2024-03-27 13:34:34 -0500 |
| commit | 57d67eb910aa765df2495df9f625bfc00436014b (patch) | |
| tree | 11db0e99732c9851c4a2c94f3e318da1cdec7716 /src/hash/bsdchecksum.c | |
| parent | b1f57a05f31a04b6d0dfbdfaf358ce8adfd45dda (diff) | |
fixed a few hashes
Diffstat (limited to 'src/hash/bsdchecksum.c')
| -rw-r--r-- | src/hash/bsdchecksum.c | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/src/hash/bsdchecksum.c b/src/hash/bsdchecksum.c index 9904352..a816b5c 100644 --- a/src/hash/bsdchecksum.c +++ b/src/hash/bsdchecksum.c @@ -2,26 +2,52 @@ #include <stdio.h> #include <stdint.h> -uint16_t i_bsdchecksum(uint8_t *aa, size_t len){ - uint16_t check = 0x0; - - for(int i = 0; i != len; i++){ - uint8_t a = aa[i]; - check = (check >> 1) + ((check & 1) << 15); - check += a; - check &= 0xffff; - } - return check; +struct bsdchecksum_hash bsdchecksum_init(){ + return (struct bsdchecksum_hash){.check = 0x0}; +} + +void bsdchecksum_update(uint8_t* aa, size_t len, struct bsdchecksum_hash* hash){ + for(int i = 0; i != len; i++){ + uint8_t a = aa[i]; + hash->check = (hash->check >> 1) + ((hash->check & 1) << 15); + hash->check += a; + hash->check &= 0xffff; + } +} + +uint16_t bsdchecksum_final(struct bsdchecksum_hash* hash){ + return hash->check; +} + +uint16_t bsdchecksum(uint8_t* a, size_t len){ + struct bsdchecksum_hash b = bsdchecksum_init(); + bsdchecksum_update(a, len, &b); + return bsdchecksum_final(&b); +} + +common_hash_init_update(bsdchecksum); + +int l_bsdchecksum_final(lua_State* L){ + lua_pushstring(L, "ud"); + lua_gettable(L, 1); + + struct bsdchecksum_hash* a = (struct bsdchecksum_hash*)lua_touserdata(L, -1); + uint32_t u = bsdchecksum_final(a); + char digest[32]; + sprintf(digest,"%i",u); + lua_pushstring(L, digest); + return 1; } int l_bsdchecksum(lua_State* L){ - + if(lua_gettop(L) == 0) return l_bsdchecksum_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); char digest[16]; - uint16_t u = i_bsdchecksum(a, len); + //uint16_t u = i_bsdchecksum(a, len); + uint16_t u = bsdchecksum(a, len); sprintf(digest,"%i",u); lua_pushstring(L, digest); |
