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 --- docs/crypto.md | 14 ++++++------- src/crypto.h | 16 ++++++++++----- src/hash/loselose.c | 37 +++++++++++++++++++++++++++++------ src/hash/loselose.h | 12 ++++++++++++ src/hash/pearson.c | 39 ++++++++++++++++++++++++++++++------- src/hash/pearson.h | 20 +++++++++++-------- src/hash/pjw.c | 52 ++++++++++++++++++++++++++++++++++++------------- src/hash/pjw.h | 11 +++++++++++ src/hash/sdbm.c | 37 +++++++++++++++++++++++++++++------ src/hash/sdbm.h | 11 +++++++++++ src/hash/sysvchecksum.c | 45 +++++++++++++++++++++++++++++++----------- src/hash/sysvchecksum.h | 19 ++++++++++-------- src/hash/xor.c | 40 +++++++++++++++++++++++++++++-------- src/hash/xor.h | 18 ++++++++++------- 14 files changed, 284 insertions(+), 87 deletions(-) diff --git a/docs/crypto.md b/docs/crypto.md index 90167df..5800a8f 100644 --- a/docs/crypto.md +++ b/docs/crypto.md @@ -10,7 +10,7 @@ but bad for big files. because of this, i decided not to support inputs over 2^6 insane amount anyways). i likely will go back and rewrite all of these to fix both of these issues. anything marked with % is fixed, -remaining to fix (inc. variants): 24 +remaining to fix (inc. variants): 16 |name|out len|other args|extra| |--|--|--|--| @@ -20,7 +20,7 @@ remaining to fix (inc. variants): 24 | sha1 | 160 | nil | | | sha256 | 256 | nil | | | sha224 | 224 | nil | | -| pearson | 8 | nil | use setpearson(table) to change the table, initial is 0..255| +| % pearson | 8 | nil | use setpearson(table) to change the table, initial is 0..255| | xxh64 | 64 | nil | xxhash | | xxh32 | 32 | nil | | | % crc8 | 8 | nil | | @@ -29,8 +29,8 @@ remaining to fix (inc. variants): 24 | % fletcher8 | 8 | nil | | | % fletcher16 | 16 | nil | | | % fletcher32 | 32 | nil | | -| sysvchecksum | 32 | nil | | -| xor8 | 8 | nil | | +| % sysvchecksum | 32 | nil | | +| % xor8 | 8 | nil | | | buzhash8 | 8 | nil | use setbuzhash(table) to change table (will affect all buzhash functions), does not support updating | | buzhash16 | 16 | nil | ^ | | cityhash32 | 32 | nil | does not support updating| @@ -46,15 +46,15 @@ remaining to fix (inc. variants): 24 | % fnv_1 | 64 | nil | | | % fnv_a | 64 | nil | | | % oaat | 32 | nil | | -| loselose | 64 | nil | | +| % loselose | 64 | nil | | | metrohash64_v1 | 64 | *seed | does not support updating| | metrohash64_v2 | 64 | *seed | ^| | metrohash128_v1 | 128 | *seed | ^| | metrohash128_v2 | 128 | *seed | ^| | murmur1_32 | 32 | *seed | ^| | murmur2_32 | 32 | *seed | ^| -| pjw | 32 | *seed | | -| sdbm | 64 | nil | | +| % pjw | 32 | nil | | +| % sdbm | 64 | nil | | | sha512 | 512 | nil | | | sha384 | 384 | nil | | | sha512_t | length of arg 2 | t (bit length) | bit length range is 0 < t <= 512 (this isnt checked, and it should accept any value) | diff --git a/src/crypto.h b/src/crypto.h index 33fd5da..a9f7adc 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -93,10 +93,10 @@ int l_##luaname##_update(lua_State* L){\ static const luaL_Reg crypto_function_list [] = { {"sha0",l_sha0}, {"sha1",l_sha1}, {"sha256",l_sha256}, {"sha224",l_sha224}, - {"setpearson",l_setpearson}, {"pearson",l_pearson}, {"xxh64",l_xxh64}, + {"setpearson",l_setpearson}, {"xxh64",l_xxh64}, {"xxh32",l_xxh32}, {"fletcher8",l_fletcher8}, {"fletcher16",l_fletcher16}, {"fletcher32",l_fletcher32}, - {"sysvchecksum",l_sysvchecksum}, {"xor8",l_xor8}, {"setbuzhash",l_setbuzhash}, + {"setbuzhash",l_setbuzhash}, {"cityhash32", l_cityhash32}, {"cityhash64", l_cityhash64}, {"cityhash128", l_cityhash128}, {"md5",l_md5}, {"farmhash32", l_farmhash32}, {"farmhash64", l_farmhash64}, @@ -104,8 +104,8 @@ static const luaL_Reg crypto_function_list [] = { {"loselose", l_loselose}, {"metrohash64_v1", l_metrohash64_v1}, {"metrohash64_v2", l_metrohash64_v2}, {"metrohash128_v1", l_metrohash128_v1}, {"metrohash128_v2", l_metrohash128_v2}, - {"murmur1_32", l_murmur1_32}, {"murmur2_32", l_murmur2_32}, {"pjw", l_pjw}, - {"sdbm", l_sdbm}, {"sha512", l_sha512}, {"sha384", l_sha384}, {"sha512_t", l_sha512_t}, + {"murmur1_32", l_murmur1_32}, {"murmur2_32", l_murmur2_32}, + {"sha512", l_sha512}, {"sha384", l_sha384}, {"sha512_t", l_sha512_t}, {"spookyhash128_v1", l_spookyhash128_v1}, {"spookyhash128_v2", l_spookyhash128_v2}, {"spookyhash64_v1", l_spookyhash64_v1}, {"spookyhash64_v2", l_spookyhash64_v2}, {"spookyhash32_v1", l_spookyhash32_v1}, {"spookyhash32_v2", l_spookyhash32_v2}, @@ -126,7 +126,13 @@ static const luaL_Reg crypto_function_list [] = { {"fnv_1", l_fnv_1}, {"fnv_1_init", l_fnv_1_init}, {"fnv_1_update", l_fnv_1_update}, {"fnv_1_final", l_fnv_1_final}, {"fnv_a", l_fnv_a}, {"fnv_a_init", l_fnv_a_init}, {"fnv_a_update", l_fnv_a_update}, {"fnv_a_final", l_fnv_a_final}, {"oaat", l_oaat}, {"oaat_init", l_oaat_init}, {"oaat_update", l_oaat_update}, {"oaat_final", l_oaat_final}, - + {"loselose", l_loselose}, {"loselose_init", l_loselose_init}, {"loselose_update", l_loselose_update}, {"loselose_final", l_loselose_final}, + {"pearson",l_pearson}, {"pearson_init",l_pearson_init}, {"pearson_update",l_pearson_update}, {"pearson_final",l_pearson_final}, + {"pjw", l_pjw}, {"pjw_init", l_pjw_init}, {"pjw_update", l_pjw_update}, {"pjw_final", l_pjw_final}, + {"sdbm", l_sdbm}, {"sdbm_init", l_sdbm_init}, {"sdbm_update", l_sdbm_update}, {"sdbm_final", l_sdbm_final}, + {"sysvchecksum",l_sysvchecksum}, {"sysvchecksum_init",l_sysvchecksum_init}, {"sysvchecksum_update",l_sysvchecksum_update}, {"sysvchecksum_final",l_sysvchecksum_final}, + {"xor8",l_xor8}, {"xor8_init",l_xor8_init}, {"xor8_update",l_xor8_update}, {"xor8_final",l_xor8_final}, + {"uuencode",l_uuencode}, {"uudecode",l_uudecode}, diff --git a/src/hash/loselose.c b/src/hash/loselose.c index f3350f5..55d3fc6 100644 --- a/src/hash/loselose.c +++ b/src/hash/loselose.c @@ -3,18 +3,43 @@ #include #include +struct loselose_hash loselose_init(){ + return (struct loselose_hash){.hash = 0}; +} + +void loselose_update(uint8_t* in, size_t len, struct loselose_hash* hash){ + for(int i = 0; i != len; i++){ + hash->hash += (uint64_t)*in; + in++; + } +} + +uint64_t loselose_final(struct loselose_hash* hash){ + return hash->hash; +} + uint64_t loselose(uint8_t* in, size_t len){ - uint64_t hash = 0; + struct loselose_hash a = loselose_init(); + loselose_update(in, len, &a); + return loselose_final(&a); +} + +common_hash_init_update(loselose); - for(int i = 0; i != len; i++){ - hash += (uint64_t)*in; - in++; - } +int l_loselose_final(lua_State* L){ + lua_pushstring(L, "ud"); + lua_gettable(L, 1); - return hash; + struct loselose_hash* a = (struct loselose_hash*)lua_touserdata(L, -1); + uint64_t u = loselose_final(a); + char digest[64]; + sprintf(digest,"%08x",u); + lua_pushstring(L, digest); + return 1; } int l_loselose(lua_State* L){ + if(lua_gettop(L) == 0) return l_loselose_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); diff --git a/src/hash/loselose.h b/src/hash/loselose.h index 434b8cb..8ce1ed7 100644 --- a/src/hash/loselose.h +++ b/src/hash/loselose.h @@ -1,5 +1,17 @@ #include "../lua.h" #include +struct loselose_hash { + uint64_t hash; +}; + +struct loselose_hash loselose_init(); +void loselose_update(uint8_t*, size_t, struct loselose_hash*); +uint64_t loselose_final(struct loselose_hash*); + uint64_t loselose(uint8_t* in, size_t len); + int l_loselose(lua_State*); +int l_loselose_init(lua_State*); +int l_loselose_update(lua_State*); +int l_loselose_final(lua_State*); diff --git a/src/hash/pearson.c b/src/hash/pearson.c index 4e1ea0a..1110891 100644 --- a/src/hash/pearson.c +++ b/src/hash/pearson.c @@ -19,12 +19,23 @@ static uint8_t pearson_table[256] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14, 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251, 252,253,254,255}; -uint8_t i_pearson(uint8_t *aa, size_t len){ - uint8_t ret = 0; +struct pearson_hash pearson_init(){ + return (struct pearson_hash){.ret = 0}; +} + +void pearson_update(uint8_t* aa, size_t len, struct pearson_hash* hash){ + for(int i = 0; i != len; i++) + hash->ret = pearson_table[(uint8_t)(hash->ret^aa[i])]; +} - for(int i = 0; i != len; i++) ret = pearson_table[(uint8_t)(ret^aa[i])]; +uint8_t pearson_final(struct pearson_hash* hash){ + return hash->ret; +} - return ret; +uint8_t pearson(uint8_t* aa, size_t len){ + struct pearson_hash a = pearson_init(); + pearson_update(aa, len, &a); + return pearson_final(&a); } int l_setpearson(lua_State* L){ @@ -48,13 +59,27 @@ int l_setpearson(lua_State* L){ return 0; } +common_hash_init_update(pearson); + +int l_pearson_final(lua_State* L){ + lua_pushstring(L, "ud"); + lua_gettable(L, 1); + + struct pearson_hash* a = (struct pearson_hash*)lua_touserdata(L, -1); + uint8_t u = pearson_final(a); + char digest[8]; + sprintf(digest,"%x",u); + lua_pushstring(L, digest); + return 1; +} + int l_pearson(lua_State* L){ - + if(lua_gettop(L) == 0) return l_pearson_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - char digest[3]; - uint8_t u = i_pearson(a, len); + char digest[8]; + uint8_t u = pearson(a, len); sprintf(digest,"%x",u); diff --git a/src/hash/pearson.h b/src/hash/pearson.h index 646e6dd..4cdfda9 100644 --- a/src/hash/pearson.h +++ b/src/hash/pearson.h @@ -1,13 +1,17 @@ #include "../lua.h" #include -/** - * calculates a pearson hash of (len) bytes - * - * @param {uint8_t*} input bytes - * @param {size_t} input length - * @return {uint8_t} 8 bit hash -*/ -uint8_t i_pearson(uint8_t*,size_t); + +struct pearson_hash { + uint8_t ret; +}; + +struct pearson_hash pearson_init(); +void pearson_update(uint8_t*, size_t, struct pearson_hash* hash); +uint8_t pearson_final(struct pearson_hash* hash); +uint8_t pearson(uint8_t*,size_t); int l_setpearson(lua_State* L); int l_pearson(lua_State* L); +int l_pearson_init(lua_State* L); +int l_pearson_update(lua_State* L); +int l_pearson_final(lua_State* L); 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; +} diff --git a/src/hash/pjw.h b/src/hash/pjw.h index f1ab910..a4e36e7 100644 --- a/src/hash/pjw.h +++ b/src/hash/pjw.h @@ -1,5 +1,16 @@ #include "../lua.h" #include +struct pjw_hash { + uint32_t hash, high; +}; + +struct pjw_hash pjw_init(); +void pjw_update(uint8_t*, size_t, struct pjw_hash*); +uint32_t pjw_final(struct pjw_hash*); uint32_t pjw(uint8_t* in, size_t len); + int l_pjw(lua_State*); +int l_pjw_init(lua_State*); +int l_pjw_update(lua_State*); +int l_pjw_final(lua_State*); diff --git a/src/hash/sdbm.c b/src/hash/sdbm.c index b32a627..bc5f0d9 100644 --- a/src/hash/sdbm.c +++ b/src/hash/sdbm.c @@ -2,18 +2,43 @@ #include #include +struct sdbm_hash sdbm_init(){ + return (struct sdbm_hash){.hash = 0}; +} + +void sdbm_update(uint8_t* in, size_t len, struct sdbm_hash* hash){ + for(int i = 0; i != len; i++){ + hash->hash = (uint64_t)*in + (hash->hash << 6) + (hash->hash << 16) - hash->hash; + in++; + } +} + +uint64_t sdbm_final(struct sdbm_hash* hash){ + return hash->hash; +} + uint64_t sdbm(uint8_t* in, size_t len){ - uint64_t hash = 0; + struct sdbm_hash a = sdbm_init(); + sdbm_update(in, len, &a); + return sdbm_final(&a); +} + +common_hash_init_update(sdbm); - for(int i = 0; i != len; i++){ - hash = (uint64_t)*in + (hash << 6) + (hash << 16) - hash; - in++; - } +int l_sdbm_final(lua_State* L){ + lua_pushstring(L, "ud"); + lua_gettable(L, 1); - return hash; + struct sdbm_hash* a = (struct sdbm_hash*)lua_touserdata(L, -1); + uint64_t u = sdbm_final(a); + char digest[64]; + sprintf(digest,"%016llx",u); + lua_pushstring(L, digest); + return 1; } int l_sdbm(lua_State* L){ + if(lua_gettop(L) == 0) return l_sdbm_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); diff --git a/src/hash/sdbm.h b/src/hash/sdbm.h index 797cc22..a937c92 100644 --- a/src/hash/sdbm.h +++ b/src/hash/sdbm.h @@ -1,5 +1,16 @@ #include "../lua.h" #include +struct sdbm_hash { + uint64_t hash; +}; + +struct sdbm_hash sdbm_init(); +void sdbm_update(uint8_t*, size_t len, struct sdbm_hash*); +uint64_t sdbm_final(struct sdbm_hash*); uint64_t sdbm(uint8_t* in, size_t len); + int l_sdbm(lua_State*); +int l_sdbm_init(lua_State*); +int l_sdbm_update(lua_State*); +int l_sdbm_final(lua_State*); diff --git a/src/hash/sysvchecksum.c b/src/hash/sysvchecksum.c index a4ce824..d22270d 100644 --- a/src/hash/sysvchecksum.c +++ b/src/hash/sysvchecksum.c @@ -3,25 +3,48 @@ #include #include -uint32_t i_sysvchecksum(uint8_t *aa, size_t len){ - uint32_t check = 0x0; - - for(int i = 0; i != len; i++){ - check += aa[i]; - } - - uint32_t r = check % (int)pow(2,16) + (check % (int)pow(2,32)) / (int)pow(2,16); - return (r % (int)pow(2,16)) + r / (int)pow(2,16); +struct sysvchecksum_hash sysvchecksum_init(){ + return (struct sysvchecksum_hash){.check = 0}; } +void sysvchecksum_update(uint8_t* aa, size_t len, struct sysvchecksum_hash* hash){ + for(int i = 0; i != len; i++) + hash->check += aa[i]; +} + +uint32_t sysvchecksum_final(struct sysvchecksum_hash* hash){ + uint32_t r = hash->check % (int)pow(2,16) + (hash->check % (int)pow(2,32)) / (int)pow(2,16); + return (r % (int)pow(2,16)) + r / (int)pow(2,16); +} + +uint32_t sysvchecksum(uint8_t* aa, size_t len){ + struct sysvchecksum_hash a = sysvchecksum_init(); + sysvchecksum_update(aa, len, &a); + return sysvchecksum_final(&a); +} + +common_hash_init_update(sysvchecksum); + +int l_sysvchecksum_final(lua_State* L){ + lua_pushstring(L, "ud"); + lua_gettable(L, 1); + + struct sysvchecksum_hash* a = (struct sysvchecksum_hash*)lua_touserdata(L, -1); + uint32_t u = sysvchecksum_final(a); + char digest[32]; + sprintf(digest,"%x",u); + lua_pushstring(L, digest); + return 1; +} + int l_sysvchecksum(lua_State* L){ - + if(lua_gettop(L) == 0) return l_sysvchecksum_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); char digest[32]; - uint32_t u = i_sysvchecksum(a, len); + uint32_t u = sysvchecksum(a, len); sprintf(digest,"%x",u); lua_pushstring(L, digest); diff --git a/src/hash/sysvchecksum.h b/src/hash/sysvchecksum.h index e45cff8..3414f7f 100644 --- a/src/hash/sysvchecksum.h +++ b/src/hash/sysvchecksum.h @@ -1,13 +1,16 @@ #include "../lua.h" #include -/** - * calculates a sysv checksum of (len) bytes - * - * @param {uint8_t*} input bytes - * @param {size_t} input length - * @return {uint32_t} 32 bit checksum -*/ -uint32_t i_sysvchecksum(uint8_t*, size_t); +struct sysvchecksum_hash { + uint32_t check; +}; + +struct sysvchecksum_hash sysvchecksum_init(); +void sysvchecksum_update(uint8_t*, size_t, struct sysvchecksum_hash*); +uint32_t sysvchecksum_final(struct sysvchecksum_hash*); +uint32_t sysvchecksum(uint8_t*, size_t); int l_sysvchecksum(lua_State*); +int l_sysvchecksum_init(lua_State*); +int l_sysvchecksum_update(lua_State*); +int l_sysvchecksum_final(lua_State*); 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); diff --git a/src/hash/xor.h b/src/hash/xor.h index bd9aaed..6065524 100644 --- a/src/hash/xor.h +++ b/src/hash/xor.h @@ -1,12 +1,16 @@ #include "../lua.h" #include -/** - * calculates a xor hash of (len) bytes - * - * @param {uint8_t*} input bytes - * @param {size_t} input length - * @return {uint8_t} 8 bit checksum -*/ + +struct xor8_hash { + uint8_t a; +}; + +struct xor8_hash xor8_init(); +void xor8_update(uint8_t*, size_t, struct xor8_hash*); +uint8_t xor8_final(struct xor8_hash*); uint8_t xor8(uint8_t*, size_t); int l_xor8(lua_State*); +int l_xor8_init(lua_State*); +int l_xor8_update(lua_State*); +int l_xor8_final(lua_State*); \ No newline at end of file -- cgit v1.2.3