From c6e8e66aa91bffa649e9192d3e4e0658d78d0ad6 Mon Sep 17 00:00:00 2001 From: ame Date: Tue, 26 Mar 2024 13:03:34 -0500 Subject: docs and rolling adler --- docs/crypto.md | 11 +++++++++++ src/crypto.h | 3 ++- src/hash/adler.c | 52 ++++++++++++++++++++++++++++++++++++++++++++-------- src/hash/adler.h | 20 ++++++++++++-------- tests/hash.lua | 13 +++++++++++++ 5 files changed, 82 insertions(+), 17 deletions(-) diff --git a/docs/crypto.md b/docs/crypto.md index 6a375b8..06689d9 100644 --- a/docs/crypto.md +++ b/docs/crypto.md @@ -4,8 +4,15 @@ \* is optional +sadly i didnt think about being able to update hashes, using the common init-update-final. +this is a pretty big problem meaning the input must be given at once, this is better for passwords, +but bad for big files. because of this, i decided not to support inputs over 2^64 characters (which is an +insane amount anyways). i likely will go back and rewrite all of these to fix both of these issues. +anything marked with % is fixed + |name|out len|other args|extra| |--|--|--|--| +| % adler32 | 32 | nil | | | sha0 | 160 | nil | insecure, use sha1| | sha1 | 160 | nil | | | sha256 | 256 | nil | | @@ -56,6 +63,10 @@ | spookyhash32_v2 | 32 | *seed | | | blake2b | length of arg 2 * 8 | *output len (default is 64), *key | | | blake2s | length of arg 2 * 8 | *output len (default is 32), *key | | +| blake256 | 256 | nil | | +| blake224 | 224 | nil | | +| blake512 | 512 | nil | | +| blake384 | 384 | nil | | ### usage diff --git a/src/crypto.h b/src/crypto.h index 400e503..2d3dcca 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -41,7 +41,7 @@ uint64_t rotr64(uint64_t, uint64_t); 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}, - {"xxh32",l_xxh32}, {"adler32",l_adler32}, {"bsdchecksum",l_bsdchecksum}, + {"xxh32",l_xxh32}, {"bsdchecksum",l_bsdchecksum}, {"crc8",l_crc8}, {"crc16",l_crc16}, {"crc32",l_crc32}, {"fletcher8",l_fletcher8}, {"fletcher16",l_fletcher16}, {"fletcher32",l_fletcher32}, {"sysvchecksum",l_sysvchecksum}, {"xor8",l_xor8}, {"setbuzhash",l_setbuzhash}, @@ -60,6 +60,7 @@ static const luaL_Reg crypto_function_list [] = { {"blake2b", l_blake2b}, {"blake2s", l_blake2s}, {"blake256", l_blake256}, {"blake224", l_blake224}, {"blake512", l_blake512}, {"blake384", l_blake384}, + {"adler32",l_adler32}, {"adler32_init",l_adler32_init}, {"adler32_update",l_adler32_update}, {"adler32_final",l_adler32_final}, {"uuencode",l_uuencode}, {"uudecode",l_uudecode}, diff --git a/src/hash/adler.c b/src/hash/adler.c index 3bd865a..ed7a84e 100644 --- a/src/hash/adler.c +++ b/src/hash/adler.c @@ -2,15 +2,50 @@ #include #include -uint32_t i_adler32(uint8_t *aa, size_t len){ - uint16_t a = 1, b = 0; +struct adler32_hash adler32_init(){ + return (struct adler32_hash){.a = 1, .b = 0}; +} + +void adler32_update(uint8_t* aa, size_t len, struct adler32_hash* hash){ + for(int i = 0; i != len; i++){ + hash->a += aa[i]; + hash->b += hash->a; + } +} + +uint32_t adler32_final(struct adler32_hash* hash){ + return hash->b * 65536 + hash->a; +} + +uint32_t adler32(uint8_t* aa, size_t len){ + struct adler32_hash dig = adler32_init(); + adler32_update(aa, len, &dig); + return adler32_final(&dig); +} + +int l_adler32_init(lua_State* L){ + struct adler32_hash* a = (struct adler32_hash*)lua_newuserdata(L, sizeof * a); + *a = adler32_init(); + return 1; +} - for(int i = 0; i != len; i++){ - a += aa[i]; - b += a; - } +int l_adler32_update(lua_State* L){ + struct adler32_hash* a = (struct adler32_hash*)lua_touserdata(L, 1); + size_t len = 0; + uint8_t* b = (uint8_t*)luaL_checklstring(L, 2, &len); + + adler32_update(b, len, a); + + return 1; +} - return b * 65536 + a; +int l_adler32_final(lua_State* L){ + struct adler32_hash* a = (struct adler32_hash*)lua_touserdata(L, 1); + uint32_t u = adler32_final(a); + char digest[32]; + sprintf(digest,"%08x",u); + lua_pushstring(L, digest); + return 1; } int l_adler32(lua_State* L){ @@ -19,7 +54,8 @@ int l_adler32(lua_State* L){ char digest[32]; - uint32_t u = i_adler32(a, len); + uint32_t u = adler32(a, len); + sprintf(digest,"%08x",u); lua_pushstring(L, digest); diff --git a/src/hash/adler.h b/src/hash/adler.h index 3bd8925..29a74f0 100644 --- a/src/hash/adler.h +++ b/src/hash/adler.h @@ -1,13 +1,17 @@ #include "../lua.h" #include -/*! - * \brief calculates a adler hash of (len) bytes - * - * @param {uint8_t*} input bytes - * @param {size_t} input length - * @return {uint32_t} 32 bit hash -*/ -uint32_t i_adler32(uint8_t*, size_t); +struct adler32_hash { + uint16_t a; + uint16_t b; +}; + +struct adler32_hash adler32_init(); +void adler32_update(uint8_t*, size_t, struct adler32_hash*); +uint32_t adler32_final(struct adler32_hash*); +uint32_t adler32(uint8_t*, size_t); int l_adler32(lua_State*); +int l_adler32_init(lua_State*); +int l_adler32_update(lua_State*); +int l_adler32_final(lua_State*); diff --git a/tests/hash.lua b/tests/hash.lua index c72d08c..ca9d85c 100644 --- a/tests/hash.lua +++ b/tests/hash.lua @@ -2,6 +2,7 @@ require "llib" function test(name,b,exp,oargs) local hash + local hash2 local add = "" if oargs == nil then hash = llib.crypto[name](b) @@ -9,6 +10,18 @@ function test(name,b,exp,oargs) hash = llib.crypto[name](b,table.unpack(oargs)) add = table.concat(oargs, ", ") end + + if(llib.crypto[name.."_init"] ~= nil) then + local t = llib.crypto[name.."_init"]() + llib.crypto[name.."_update"](t, b) + hash2 = llib.crypto[name.."_final"](t) + if(hash2 ~= hash) then + llib.io.error(name.." init-update-final method not working, got:\n\t"..hash2.." other was:\n\t"..hash) + else + llib.io.log(name.." alt method working") + end + end + if not (hash == exp) then llib.io.error(name.." not working, got:\n\t"..hash.." wanted:\n\t"..exp.."\n\twith args: {"..add.."}") else -- cgit v1.2.3