aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorame <[email protected]>2024-03-28 12:41:17 -0500
committerame <[email protected]>2024-03-28 12:41:17 -0500
commit35c37ef1221d5860731435137059dc4533adff42 (patch)
treefc3dcf74e40ade1ebeb8cbb270fe28f8ce7b63b5
parent2a2615374054f42221bd5b9a58c7b18628a1dbea (diff)
more hashes fixed
-rw-r--r--docs/crypto.md55
-rw-r--r--src/crypto.h51
-rw-r--r--src/hash/djb2.c39
-rw-r--r--src/hash/djb2.h11
-rw-r--r--src/hash/fletcher.c127
-rw-r--r--src/hash/fletcher.h63
-rw-r--r--src/hash/fnv.c74
-rw-r--r--src/hash/fnv.h19
-rw-r--r--src/hash/jenkins.c51
-rw-r--r--src/hash/jenkins.h11
-rw-r--r--src/net.c2
-rw-r--r--tests/hash.lua41
12 files changed, 415 insertions, 129 deletions
diff --git a/docs/crypto.md b/docs/crypto.md
index 3f7b3f1..90167df 100644
--- a/docs/crypto.md
+++ b/docs/crypto.md
@@ -10,6 +10,8 @@ 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
+
|name|out len|other args|extra|
|--|--|--|--|
| % adler32 | 32 | nil | |
@@ -24,9 +26,9 @@ anything marked with % is fixed,
| % crc8 | 8 | nil | |
| % crc16 | 16 | nil | |
| % crc32 | 32 | nil | |
-| fletcher8 | 8 | nil | |
-| fletcher16 | 16 | nil | |
-| fletcher32 | 32 | nil | |
+| % fletcher8 | 8 | nil | |
+| % fletcher16 | 16 | nil | |
+| % fletcher32 | 32 | 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 |
@@ -35,33 +37,33 @@ anything marked with % is fixed,
| cityhash64 | 64 | nil | ^ |
| cityhash128 | 128 | nil | ^ |
| md5 | 128 | nil | |
-| djb2 | 64 | nil | |
-| farmhash32 | 32 | nil | |
-| farmhash64 | 64 | nil | |
-| fasthash32 | 32 | *seed | |
-| fasthash64 | 64 | *seed | |
-| fnv_0 | 64 | nil | |
-| fnv_1 | 64 | nil | |
-| fnv_a | 64 | nil | |
-| oaat | 32 | nil | |
-| lostlose | 64 | nil | |
-| metrohash64_v1 | 64 | *seed | |
-| metrohash64_v2 | 64 | *seed | |
-| metrohash128_v1 | 128 | *seed | |
-| metrohash128_v2 | 128 | *seed | |
-| murmur1_32 | 32 | *seed | |
-| murmur2_32 | 32 | *seed | |
+| % djb2 | 64 | nil | |
+| farmhash32 | 32 | nil | does not support updating|
+| farmhash64 | 64 | nil | ^|
+| fasthash32 | 32 | *seed | ^|
+| fasthash64 | 64 | *seed | ^|
+| % fnv_0 | 64 | nil | |
+| % fnv_1 | 64 | nil | |
+| % fnv_a | 64 | nil | |
+| % oaat | 32 | 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 | |
| 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) |
-| spookyhash128_v1 | 128 | *seed | |
-| spookyhash128_v2 | 128 | *seed | |
-| spookyhash64_v1 | 64 | *seed | |
-| spookyhash64_v2 | 64 | *seed | |
-| spookyhash32_v1 | 32 | *seed | |
-| spookyhash32_v2 | 32 | *seed | |
+| spookyhash128_v1 | 128 | *seed | does not support updating|
+| spookyhash128_v2 | 128 | *seed | ^|
+| spookyhash64_v1 | 64 | *seed | ^|
+| spookyhash64_v2 | 64 | *seed | ^|
+| spookyhash32_v1 | 32 | *seed | ^|
+| 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 | |
@@ -87,6 +89,9 @@ local hash = llib.crypto.adler32_final(obj) --043c01b9
obj = llib.crypto.adler32()
obj:update("meow")
hash = obj:final() --043c01b9s (the same)
+
+--and of course, the single function method still works too (will still do init-update-final in the backend)
+hash = llib.crypto.adler32("meow") --043c01b9s (the same)
```
## en/decoding
diff --git a/src/crypto.h b/src/crypto.h
index 8bda7c5..33fd5da 100644
--- a/src/crypto.h
+++ b/src/crypto.h
@@ -39,8 +39,10 @@ unsigned rotr32(unsigned, unsigned);
uint64_t rotl64(uint64_t, uint64_t);
uint64_t rotr64(uint64_t, uint64_t);
-#define common_hash_init_update(hashname)\
- int l_##hashname##_init(lua_State* L){\
+#define common_hash_init_update(hashname) lua_common_hash_init_update(hashname, hashname)
+#define lua_common_hash_init_update(hashname, luaname) lua_common_hash_init(hashname, luaname) lua_common_hash_update(hashname, luaname)
+#define lua_common_hash_init(hashname, luaname)\
+ int l_##luaname##_init(lua_State* L){\
lua_newtable(L);\
int t = lua_gettop(L);\
\
@@ -49,14 +51,33 @@ uint64_t rotr64(uint64_t, uint64_t);
*a = hashname##_init();\
\
luaI_tsetv(L, t, "ud", ud);\
- luaI_tsetcf(L, t, "update", l_##hashname##_update);\
- luaI_tsetcf(L, t, "final", l_##hashname##_final);\
+ luaI_tsetcf(L, t, "update", l_##luaname##_update);\
+ luaI_tsetcf(L, t, "final", l_##luaname##_final);\
\
lua_pushvalue(L, t);\
return 1;\
}\
-\
-int l_##hashname##_update(lua_State* L){\
+
+#define lua_common_hash_init_warg(hashname, luaname, hcode, arg)\
+ int l_##luaname##_init(lua_State* L){\
+ hcode;\
+ lua_newtable(L);\
+ int t = lua_gettop(L);\
+ \
+ struct hashname##_hash* a = (struct hashname##_hash*)lua_newuserdata(L, sizeof * a);\
+ int ud = lua_gettop(L);\
+ *a = hashname##_init(arg);\
+ \
+ luaI_tsetv(L, t, "ud", ud);\
+ luaI_tsetcf(L, t, "update", l_##luaname##_update);\
+ luaI_tsetcf(L, t, "final", l_##luaname##_final);\
+ \
+ lua_pushvalue(L, t);\
+ return 1;\
+}\
+
+#define lua_common_hash_update(hashname, luaname)\
+int l_##luaname##_update(lua_State* L){\
lua_pushstring(L, "ud");\
lua_gettable(L, 1);\
\
@@ -76,11 +97,11 @@ static const luaL_Reg crypto_function_list [] = {
{"xxh32",l_xxh32}, {"fletcher8",l_fletcher8},
{"fletcher16",l_fletcher16}, {"fletcher32",l_fletcher32},
{"sysvchecksum",l_sysvchecksum}, {"xor8",l_xor8}, {"setbuzhash",l_setbuzhash},
- {"cityhash32", l_cityhash32},
+ {"cityhash32", l_cityhash32},
{"cityhash64", l_cityhash64}, {"cityhash128", l_cityhash128}, {"md5",l_md5},
- {"djb2", l_djb2}, {"farmhash32", l_farmhash32}, {"farmhash64", l_farmhash64},
- {"fasthash32", l_fasthash32}, {"fasthash64", l_fasthash64}, {"fnv_0", l_fnv_0},
- {"fnv_1", l_fnv_1}, {"fnv_a", l_fnv_a}, {"oaat", l_oaat}, {"loselose", l_loselose},
+ {"farmhash32", l_farmhash32}, {"farmhash64", l_farmhash64},
+ {"fasthash32", l_fasthash32}, {"fasthash64", l_fasthash64},
+ {"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},
@@ -97,7 +118,15 @@ static const luaL_Reg crypto_function_list [] = {
{"crc8",l_crc8}, {"crc8_init",l_crc8_init}, {"crc8_update",l_crc8_update}, {"crc8_final",l_crc8_final},
{"crc16",l_crc16}, {"crc16_init",l_crc16_init}, {"crc16_update",l_crc16_update}, {"crc16_final",l_crc16_final},
{"crc32",l_crc32}, {"crc32_init",l_crc32_init}, {"crc32_update",l_crc32_update}, {"crc32_final",l_crc32_final},
-
+ {"djb2", l_djb2}, {"djb2_init", l_djb2_init}, {"djb2_update", l_djb2_update}, {"djb2_final", l_djb2_final},
+ {"fletcher8",l_fletcher8}, {"fletcher8_init",l_fletcher8_init}, {"fletcher8_update",l_fletcher8_update}, {"fletcher8_final",l_fletcher8_final},
+ {"fletcher16",l_fletcher16}, {"fletcher16_init",l_fletcher16_init}, {"fletcher16_update",l_fletcher16_update}, {"fletcher16_final",l_fletcher16_final},
+ {"fletcher32",l_fletcher32}, {"fletcher32_init",l_fletcher32_init}, {"fletcher32_update",l_fletcher32_update}, {"fletcher32_final",l_fletcher32_final},
+ {"fnv_0", l_fnv_0}, {"fnv_0_init", l_fnv_0_init}, {"fnv_0_update", l_fnv_0_update}, {"fnv_0_final", l_fnv_0_final},
+ {"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},
+
{"uuencode",l_uuencode},
{"uudecode",l_uudecode},
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);
diff --git a/src/hash/djb2.h b/src/hash/djb2.h
index ab0d20f..d944d11 100644
--- a/src/hash/djb2.h
+++ b/src/hash/djb2.h
@@ -1,5 +1,16 @@
#include "../lua.h"
#include <stdint.h>
+struct djb2_hash {
+ uint32_t hash;
+};
+
+struct djb2_hash djb2_init();
+void djb2_update(uint8_t*, size_t, struct djb2_hash*);
+uint32_t djb2_final(struct djb2_hash*);
uint32_t djb2(uint8_t*, size_t);
+
int l_djb2(lua_State*);
+int l_djb2_init(lua_State*);
+int l_djb2_update(lua_State*);
+int l_djb2_final(lua_State*);
diff --git a/src/hash/fletcher.c b/src/hash/fletcher.c
index 0d82fd0..1ab1857 100644
--- a/src/hash/fletcher.c
+++ b/src/hash/fletcher.c
@@ -2,44 +2,117 @@
#include <stdio.h>
#include <stdint.h>
-uint8_t i_fletcher8(uint8_t *aa, size_t len){
- uint8_t s1 = 0, s2 = 0;
+struct fletcher8_hash fletcher8_init(){
+ return (struct fletcher8_hash){.s1 = 0, .s2 = 0};
+}
+
+void fletcher8_update(uint8_t *aa, size_t len, struct fletcher8_hash *hash){
+ for(int i = 0; i != len; i++){
+ hash->s1 = (hash->s1 + aa[i]) % 15;
+ hash->s2 = (hash->s2 + hash->s1) % 15;
+ }
+}
+
+uint8_t fletcher8_final(struct fletcher8_hash *hash){
+ return (hash->s2 << 4) | hash->s1;
+}
+
+uint8_t fletcher8(uint8_t *aa, size_t len){
+ struct fletcher8_hash a = fletcher8_init();
+ fletcher8_update(aa, len, &a);
+ return fletcher8_final(&a);
+}
+
+struct fletcher16_hash fletcher16_init(){
+ return (struct fletcher16_hash){.s1 = 0, .s2 = 0};
+}
+
+void fletcher16_update(uint8_t *aa, size_t len, struct fletcher16_hash *hash){
+ for(int i = 0; i != len; i++){
+ hash->s1 = (hash->s1 + aa[i]) % 255;
+ hash->s2 = (hash->s2 + hash->s1) % 255;
+ }
+}
+
+uint16_t fletcher16_final(struct fletcher16_hash *hash){
+ return (hash->s2 << 8) | hash->s1;
+}
+
+uint16_t fletcher16(uint8_t *aa, size_t len){
+ struct fletcher16_hash a = fletcher16_init();
+ fletcher16_update(aa, len, &a);
+ return fletcher16_final(&a);
+}
+
+struct fletcher32_hash fletcher32_init(){
+ return (struct fletcher32_hash){.s1 = 0, .s2 = 0};
+}
+
+void fletcher32_update(uint8_t *aa, size_t len, struct fletcher32_hash *hash){
+ for(int i = 0; i != len; i++){
+ hash->s1 = (hash->s1 + aa[i]) % 65535;
+ hash->s2 = (hash->s2 + hash->s1) % 65535;
+ }
+}
+
+uint32_t fletcher32_final(struct fletcher32_hash *hash){
+ return (hash->s2 << 16) | hash->s1;
+}
+
+uint32_t fletcher32(uint8_t *aa, size_t len){
+ struct fletcher32_hash a = fletcher32_init();
+ fletcher32_update(aa, len, &a);
+ return fletcher32_final(&a);
+}
+
+common_hash_init_update(fletcher8);
+common_hash_init_update(fletcher16);
+common_hash_init_update(fletcher32);
+
+int l_fletcher8_final(lua_State* L){
+ lua_pushstring(L, "ud");
+ lua_gettable(L, 1);
- for(int i = 0; i != len; i++){
- s1 = (s1 + aa[i]) % 15;
- s2 = (s2 + s1) % 15;
- }
- return (s2 << 4) | s1;
+ struct fletcher8_hash* a = (struct fletcher8_hash*)lua_touserdata(L, -1);
+ uint8_t u = fletcher8_final(a);
+ char digest[8];
+ sprintf(digest,"%02x",u);
+ lua_pushstring(L, digest);
+ return 1;
}
-uint16_t i_fletcher16(uint8_t *aa, size_t len){
- uint16_t s1 = 0, s2 = 0;
+int l_fletcher16_final(lua_State* L){
+ lua_pushstring(L, "ud");
+ lua_gettable(L, 1);
- for(int i = 0; i != len; i++){
- s1 = (s1 + aa[i]) % 255;
- s2 = (s2 + s1) % 255;
- }
- return (s2 << 8) | s1;
+ struct fletcher16_hash* a = (struct fletcher16_hash*)lua_touserdata(L, -1);
+ uint16_t u = fletcher16_final(a);
+ char digest[16];
+ sprintf(digest,"%04x",u);
+ lua_pushstring(L, digest);
+ return 1;
}
-uint32_t i_fletcher32(uint8_t *aa, size_t len){
- uint32_t s1 = 0, s2 = 0;
+int l_fletcher32_final(lua_State* L){
+ lua_pushstring(L, "ud");
+ lua_gettable(L, 1);
- for(int i = 0; i != len; i++){
- s1 = (s1 + aa[i]) % 65535;
- s2 = (s2 + s1) % 65535;
- }
- return (s2 << 16) | s1;
+ struct fletcher32_hash* a = (struct fletcher32_hash*)lua_touserdata(L, -1);
+ uint32_t u = fletcher32_final(a);
+ char digest[32];
+ sprintf(digest,"%08x",u);
+ lua_pushstring(L, digest);
+ return 1;
}
int l_fletcher32(lua_State* L){
-
+ if(lua_gettop(L) == 0) return l_fletcher32_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
char digest[32];
- uint32_t u = i_fletcher32(a, len);
+ uint32_t u = fletcher32(a, len);
sprintf(digest,"%08x",u);
lua_pushstring(L, digest);
@@ -47,13 +120,13 @@ int l_fletcher32(lua_State* L){
}
int l_fletcher16(lua_State* L){
-
+ if(lua_gettop(L) == 0) return l_fletcher16_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
char digest[16];
- uint16_t u = i_fletcher16(a, len);
+ uint16_t u = fletcher16(a, len);
sprintf(digest,"%04x",u);
lua_pushstring(L, digest);
@@ -61,13 +134,13 @@ int l_fletcher16(lua_State* L){
}
int l_fletcher8(lua_State* L){
-
+ if(lua_gettop(L) == 0) return l_fletcher8_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
char digest[8];
- uint8_t u = i_fletcher8(a, len);
+ uint8_t u = fletcher8(a, len);
sprintf(digest,"%02x",u);
lua_pushstring(L, digest);
diff --git a/src/hash/fletcher.h b/src/hash/fletcher.h
index c77bce4..d66692f 100644
--- a/src/hash/fletcher.h
+++ b/src/hash/fletcher.h
@@ -1,33 +1,44 @@
#include "../lua.h"
#include <stdint.h>
-/**
- * calculates a fletcher hash of (len) bytes
- *
- * @param {uint8_t*} input bytes
- * @param {size_t} input length
- * @return {uint8_t} 8 bit hash
-*/
-uint8_t i_fletcher8(uint8_t*,size_t);
-
-/**
- * calculates a fletcher hash of (len) bytes
- *
- * @param {uint8_t*} input bytes
- * @param {size_t} input length
- * @return {uint16_t} 16 bit checksum
-*/
-uint16_t i_fletcher16(uint8_t*,size_t);
-
-/**
- * calculates a fletcher hash of (len) bytes
- *
- * @param {uint8_t*} input bytes
- * @param {size_t} input length
- * @return {uint32_t} 32 bit checksum
-*/
-uint32_t i_fletcher32(uint8_t*,size_t);
+struct fletcher8_hash {
+ uint8_t s1, s2;
+};
+
+struct fletcher16_hash {
+ uint16_t s1, s2;
+};
+
+struct fletcher32_hash {
+ uint32_t s1, s2;
+};
+
+uint8_t fletcher8(uint8_t*,size_t);
+struct fletcher8_hash fletcher8_init();
+void fletcher8_update(uint8_t*, size_t, struct fletcher8_hash*);
+uint8_t fletcher8_final(struct fletcher8_hash*);
+
+uint16_t fletcher16(uint8_t*,size_t);
+struct fletcher16_hash fletcher16_init();
+void fletcher16_update(uint8_t*, size_t, struct fletcher16_hash*);
+uint16_t fletcher16_final(struct fletcher16_hash*);
+
+uint32_t fletcher32(uint8_t*,size_t);
+struct fletcher32_hash fletcher32_init();
+void fletcher32_update(uint8_t*, size_t, struct fletcher32_hash*);
+uint32_t fletcher32_final(struct fletcher32_hash*);
int l_fletcher32(lua_State*);
+int l_fletcher32_init(lua_State*);
+int l_fletcher32_update(lua_State*);
+int l_fletcher32_final(lua_State*);
+
int l_fletcher16(lua_State*);
+int l_fletcher16_init(lua_State*);
+int l_fletcher16_update(lua_State*);
+int l_fletcher16_final(lua_State*);
+
int l_fletcher8(lua_State*);
+int l_fletcher8_init(lua_State*);
+int l_fletcher8_update(lua_State*);
+int l_fletcher8_final(lua_State*); \ No newline at end of file
diff --git a/src/hash/fnv.c b/src/hash/fnv.c
index 9d5c646..e0fc981 100644
--- a/src/hash/fnv.c
+++ b/src/hash/fnv.c
@@ -3,29 +3,65 @@
#include <stdio.h>
#include <stdint.h>
+struct fnv_1_hash fnv_1_init(enum fnv_version A){
+ return (struct fnv_1_hash){.A = A, .hash = (A != v_0) * 0xcbf29ce484222325};
+}
-uint64_t fnv_1(uint8_t* in, size_t len, enum fnv_version A){
- uint64_t hash = (A != v_0) * 0xcbf29ce484222325;
- uint64_t prime = 0x100000001b3;
-
- for(int i = 0; i != len; i++){
- switch(A){
- case v_1:
- case v_0:
- hash *= prime;
- hash ^= in[i];
- break;
- case v_a:
- hash ^= in[i];
- hash *= prime;
- break;
- }
+void fnv_1_update(uint8_t* in, size_t len, struct fnv_1_hash* hash){
+ uint64_t prime = 0x100000001b3;
+
+ for(int i = 0; i != len; i++){
+ switch(hash->A){
+ case v_1:
+ case v_0:
+ hash->hash *= prime;
+ hash->hash ^= in[i];
+ break;
+ case v_a:
+ hash->hash ^= in[i];
+ hash->hash *= prime;
+ break;
}
+ }
+}
+
+uint64_t fnv_1_final(struct fnv_1_hash* hash){
+ return hash->hash;
+}
- return hash;
+uint64_t fnv_1(uint8_t* in, size_t len, enum fnv_version A){
+ struct fnv_1_hash a = fnv_1_init(A);
+ fnv_1_update(in, len, &a);
+ return fnv_1_final(&a);
+}
+
+lua_common_hash_update(fnv_1, fnv_1);
+lua_common_hash_update(fnv_1, fnv_0);
+lua_common_hash_update(fnv_1, fnv_a);
+
+lua_common_hash_init_warg(fnv_1, fnv_1, ;, v_1);
+lua_common_hash_init_warg(fnv_1, fnv_0, ;, v_0);
+lua_common_hash_init_warg(fnv_1, fnv_a, ;, v_a);
+
+#define aaa(v)\
+int l_fnv_##v##_final(lua_State* L){\
+ lua_pushstring(L, "ud");\
+ lua_gettable(L, 1);\
+\
+ struct fnv_1_hash* a = (struct fnv_1_hash*)lua_touserdata(L, -1);\
+ uint64_t u = fnv_1_final(a);\
+ char digest[64];\
+ sprintf(digest,"%16llx",u);\
+ lua_pushstring(L, digest);\
+ return 1;\
}
-int l_fnv_0(lua_State* L){
+aaa(0);
+aaa(1);
+aaa(a);
+
+int l_fnv_0(lua_State* L){
+ if(lua_gettop(L) == 0) return l_fnv_0_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
@@ -38,6 +74,7 @@ int l_fnv_0(lua_State* L){
}
int l_fnv_1(lua_State* L){
+ if(lua_gettop(L) == 0) return l_fnv_1_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
@@ -50,6 +87,7 @@ int l_fnv_1(lua_State* L){
}
int l_fnv_a(lua_State* L){
+ if(lua_gettop(L) == 0) return l_fnv_a_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
diff --git a/src/hash/fnv.h b/src/hash/fnv.h
index 74f1722..a4299c9 100644
--- a/src/hash/fnv.h
+++ b/src/hash/fnv.h
@@ -5,8 +5,27 @@ enum fnv_version {
v_1, v_a, v_0
};
+struct fnv_1_hash {
+ enum fnv_version A;
+ uint64_t hash;
+};
+
uint64_t fnv_1(uint8_t*, size_t, enum fnv_version);
+struct fnv_1_hash fnv_1_init(enum fnv_version);
+void fnv_1_update(uint8_t*, size_t, struct fnv_1_hash*);
+uint64_t fnv_1_final(struct fnv_1_hash*);
int l_fnv_1(lua_State*);
+int l_fnv_1_init(lua_State*);
+int l_fnv_1_update(lua_State*);
+int l_fnv_1_final(lua_State*);
+
int l_fnv_a(lua_State*);
+int l_fnv_a_init(lua_State*);
+int l_fnv_a_update(lua_State*);
+int l_fnv_a_final(lua_State*);
+
int l_fnv_0(lua_State*);
+int l_fnv_0_init(lua_State*);
+int l_fnv_0_update(lua_State*);
+int l_fnv_0_final(lua_State*);
diff --git a/src/hash/jenkins.c b/src/hash/jenkins.c
index a08f526..6daf831 100644
--- a/src/hash/jenkins.c
+++ b/src/hash/jenkins.c
@@ -3,22 +3,49 @@
#include <stdio.h>
#include <stdint.h>
+struct jenkins_oaat_hash jenkins_oaat_init(){
+ return (struct jenkins_oaat_hash){.hash = 0};
+}
+
+void jenkins_oaat_update(uint8_t* in, size_t len, struct jenkins_oaat_hash* hash){
+ for(int i = 0; i != len;){
+ hash->hash += in[i++];
+ hash->hash += hash->hash << 10;
+ hash->hash ^= hash->hash >> 6;
+ }
+}
+
+uint32_t jenkins_oaat_final(struct jenkins_oaat_hash* hash){
+ hash->hash += hash->hash << 3;
+ hash->hash ^= hash->hash >> 11;
+ hash->hash += hash->hash << 15;
+
+ return hash->hash;
+}
+
+
uint32_t jenkins_oaat(uint8_t* in, size_t len){
- uint32_t hash = 0;
-
- for(int i = 0; i != len;){
- hash += in[i++];
- hash += hash << 10;
- hash ^= hash >> 6;
- }
- hash += hash << 3;
- hash ^= hash >> 11;
- hash += hash << 15;
-
- return hash;
+ struct jenkins_oaat_hash a = jenkins_oaat_init();
+ jenkins_oaat_update(in, len, &a);
+ return jenkins_oaat_final(&a);
+}
+
+lua_common_hash_init_update(jenkins_oaat, oaat);
+
+int l_oaat_final(lua_State* L){
+ lua_pushstring(L, "ud");
+ lua_gettable(L, 1);
+
+ struct jenkins_oaat_hash* a = (struct jenkins_oaat_hash*)lua_touserdata(L, -1);
+ uint32_t u = jenkins_oaat_final(a);
+ char digest[64];
+ sprintf(digest,"%04x",u);
+ lua_pushstring(L, digest);
+ return 1;
}
int l_oaat(lua_State* L){
+ if(lua_gettop(L) == 0) return l_oaat_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
diff --git a/src/hash/jenkins.h b/src/hash/jenkins.h
index d295896..9b5b465 100644
--- a/src/hash/jenkins.h
+++ b/src/hash/jenkins.h
@@ -1,5 +1,16 @@
#include "../lua.h"
#include <stdint.h>
+struct jenkins_oaat_hash {
+ uint32_t hash;
+};
+
uint32_t jenkins_oaat(uint8_t* in, size_t len);
+struct jenkins_oaat_hash jenkins_oaat_init();
+void jenkins_oaat_update(uint8_t*, size_t, struct jenkins_oaat_hash*);
+uint32_t jenkins_oaat_final(struct jenkins_oaat_hash*);
+
int l_oaat(lua_State*);
+int l_oaat_init(lua_State*);
+int l_oaat_update(lua_State*);
+int l_oaat_final(lua_State*);
diff --git a/src/net.c b/src/net.c
index c829ead..103f164 100644
--- a/src/net.c
+++ b/src/net.c
@@ -79,7 +79,6 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* st
if(*header_eof == -1 && (header = strstr(buffer, "\r\n\r\n")) != NULL){
*header_eof = header - buffer;
char* cont_len_raw = strstr(buffer, "Content-Length: ");
- //printf("%s\n", buffer);
if(cont_len_raw == NULL) {
len += n;
@@ -111,7 +110,6 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* st
if(content_len != -1 && len - *header_eof - 4 >= content_len) break;
}
- //printf("%li\n%li",len, content_len + *header_eof + 4);
*_buffer = buffer;
return len;
}
diff --git a/tests/hash.lua b/tests/hash.lua
index ca96f34..c994312 100644
--- a/tests/hash.lua
+++ b/tests/hash.lua
@@ -1,9 +1,16 @@
require "llib"
+local hashes_working = 0
+local hashes_failed = 0
+local functions_working = 0
+local functions_failed = 0
+
function test(name,b,exp,oargs)
+ local fail = false
local hash
local hash2
local hash3
+ local hash4
local add = ""
if oargs == nil then
hash = llib.crypto[name](b)
@@ -18,26 +25,55 @@ function test(name,b,exp,oargs)
hash3 = llib.crypto[name]()
b:gsub(".", function(c) hash3:update(c) end)
hash3 = hash3:final()
+
+ hash4 = llib.crypto[name.."_init"]()
+ llib.crypto[name.."_update"](hash4, b)
+ hash4 = llib.crypto[name.."_final"](hash4)
if(hash2 ~= hash) then
+ fail = true
+ functions_failed=functions_failed + 1
llib.io.error(name.." alt method not working, got:\n\t"..hash2.." other was:\n\t"..hash)
else
+ functions_working=functions_working + 1
llib.io.log(name.." alt method working "..hash2.." == "..hash)
end
+ if(hash4 ~= hash) then
+ fail = true
+ functions_failed=functions_failed + 1
+ llib.io.error(name.." alt method 2 not working, got:\n\t"..hash4.." other was:\n\t"..hash)
+ else
+ functions_working=functions_working + 1
+ llib.io.log(name.." alt method 2 working "..hash4.." == "..hash)
+ end
+
if(hash3 ~= hash) then
+ fail = true
+ functions_failed=functions_failed + 1
llib.io.error(name.." alt char-b-char method not working, got:\n\t"..hash3.." other was:\n\t"..hash)
else
+ functions_working=functions_working + 1
llib.io.log(name.." alt char-b-char method working "..hash3.." == "..hash)
end
end
if not (hash == exp) then
+ fail = true
+ functions_failed=functions_failed + 1
llib.io.error(name.." not working, got:\n\t"..hash.." wanted:\n\t"..exp.."\n\twith args: {"..add.."}")
else
+ functions_working=functions_working + 1
llib.io.log(name.." was correct, "..hash)
end
+
+ if(fail) then
+ hashes_failed=hashes_failed + 1
+ else
+ hashes_working=hashes_working + 1
+ end
+
end
test("adler32","meow","043c01b9")
@@ -91,4 +127,7 @@ test("blake2s","meow","f461bed24c982ccb29cb967acdaebc9494b51c1d0f88f6bc478509522
test("blake256", "meow", "067805dd21a4ef97460c6613f231437917a1c1c7f1dcd1bfe67d952d09ccb028")
test("blake224", "meow", "0a099d692027cfbe69d2424a5b2520a7398fa4945e0878f6c541f5ce")
test("blake512", "meow", "09d5abe166c4ad855d4527d0be21df2b1a01c3d7c5637572561ebc247908fd7db30bf342391dd0a834fd35f391807480fb31e8a7ee3b1098e46d996d5601948f")
-test("blake384", "meow", "7edb2ff04616f5551a789217029496c3c8601ac7aba2d40d7fcd1ec85fc63f37514e5884f2ebc807b11854247620446c") \ No newline at end of file
+test("blake384", "meow", "7edb2ff04616f5551a789217029496c3c8601ac7aba2d40d7fcd1ec85fc63f37514e5884f2ebc807b11854247620446c")
+
+print(hashes_working.."/"..hashes_failed.." hashes working/failed")
+print(functions_working.."/"..functions_failed.." functions working/failed") \ No newline at end of file