aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorame <[email protected]>2024-04-05 22:15:55 -0500
committerame <[email protected]>2024-04-05 22:15:55 -0500
commitb8c10dac99316974d142c530356ad5cd8fdf9afa (patch)
treeb91c803a9a3ed3b391166684f0ac940c44977f17
parent18657b5d226f5fe20ea049308b5078fd188ae96c (diff)
+ + final fixes
-rw-r--r--docs/crypto.md5
-rw-r--r--src/crypto.c2
-rw-r--r--src/crypto.h62
-rw-r--r--src/hash/adler.c5
-rw-r--r--src/hash/bsdchecksum.c5
-rw-r--r--src/hash/crc.c15
-rw-r--r--src/hash/djb2.c5
-rw-r--r--src/hash/fletcher.c15
-rw-r--r--src/hash/fnv.c11
-rw-r--r--src/hash/jenkins.c14
-rw-r--r--src/hash/loselose.c5
-rw-r--r--src/hash/md5.c14
-rw-r--r--src/hash/pearson.c5
-rw-r--r--src/hash/pjw.c5
-rw-r--r--src/hash/sdbm.c5
-rw-r--r--src/hash/sha01.c13
-rw-r--r--src/hash/sha2-256.c44
-rw-r--r--src/hash/sysvchecksum.c5
-rw-r--r--src/hash/xor.c5
-rw-r--r--tests/hash.lua29
20 files changed, 134 insertions, 135 deletions
diff --git a/docs/crypto.md b/docs/crypto.md
index d46a7ad..75ae5f0 100644
--- a/docs/crypto.md
+++ b/docs/crypto.md
@@ -87,6 +87,11 @@ obj = llib.crypto.adler32()
obj:update("meow")
hash = obj:final() --043c01b9s (the same)
+--along with the + operator being overloaded to work as obj:update and returning the current final
+obj = llib.crypto.adler32()
+hash = obj + meow -- (the same again)
+hash = obj:final() -- (the same, again)
+
--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)
diff --git a/src/crypto.c b/src/crypto.c
index 4985e0d..8bb896a 100644
--- a/src/crypto.c
+++ b/src/crypto.c
@@ -22,4 +22,4 @@ uint64_t rotl64(uint64_t y, uint64_t offset){
uint64_t rotr64(uint64_t x, uint64_t n) {
return (x >> n) | (x << (64-n));
-} \ No newline at end of file
+}
diff --git a/src/crypto.h b/src/crypto.h
index 809470f..e58d3a7 100644
--- a/src/crypto.h
+++ b/src/crypto.h
@@ -39,50 +39,52 @@ unsigned rotr32(unsigned, unsigned);
uint64_t rotl64(uint64_t, uint64_t);
uint64_t rotr64(uint64_t, uint64_t);
+int tp(lua_State*);
+
#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) lua_common_hash_init_ni(hashname, luaname, hashname##_init())
+
+#define lua_common_hash_meta(luaname)\
+int _##luaname##_hash_add(lua_State*L){\
+ l_##luaname##_update(L);\
+ lua_pushvalue(L, 1);\
+ return l_##luaname##_final(L);\
+ }\
+int _##luaname##_common_hash(lua_State* L){\
+ lua_newtable(L);\
+ int ti = lua_gettop(L);\
+ luaI_tsetcf(L, ti, "update", l_##luaname##_update);\
+ luaI_tsetcf(L, ti, "final", l_##luaname##_final);\
+ \
+ lua_pushvalue(L, 2);\
+ lua_gettable(L, ti);\
+ return 1;\
+ }
+
+#define lua_common_hash_meta_def(luaname)\
+ lua_newtable(L);\
+ int mt = lua_gettop(L);\
+ luaI_tsetcf(L, mt, "__index", _##luaname##_common_hash);\
+ luaI_tsetcf(L, mt, "__add", _##luaname##_hash_add);\
+ lua_pushvalue(L, mt);\
+ lua_setmetatable(L, ud);\
+
#define lua_common_hash_init_ni(hashname, luaname, initf)\
+ lua_common_hash_meta(luaname);\
int l_##luaname##_init(lua_State* L){\
- 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 = initf;\
- \
- 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);\
+ lua_common_hash_meta_def(luaname);\
+ lua_pushvalue(L, ud);\
return 1;\
}
-#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);\
- \
- struct hashname##_hash* a = (struct hashname##_hash*)lua_touserdata(L, -1);\
+ struct hashname##_hash* a = (struct hashname##_hash*)lua_touserdata(L, 1);\
size_t len = 0;\
uint8_t* b = (uint8_t*)luaL_checklstring(L, 2, &len);\
\
diff --git a/src/hash/adler.c b/src/hash/adler.c
index c6b4d7f..932c02c 100644
--- a/src/hash/adler.c
+++ b/src/hash/adler.c
@@ -26,10 +26,7 @@ uint32_t adler32(uint8_t* aa, size_t len){
common_hash_init_update(adler32);
int l_adler32_final(lua_State* L){
- lua_pushstring(L, "ud");
- lua_gettable(L, 1);
-
- struct adler32_hash* a = (struct adler32_hash*)lua_touserdata(L, -1);
+ struct adler32_hash* a = (struct adler32_hash*)lua_touserdata(L, 1);
uint32_t u = adler32_final(a);
char digest[32];
sprintf(digest,"%08x",u);
diff --git a/src/hash/bsdchecksum.c b/src/hash/bsdchecksum.c
index a816b5c..ebcf091 100644
--- a/src/hash/bsdchecksum.c
+++ b/src/hash/bsdchecksum.c
@@ -28,10 +28,7 @@ uint16_t bsdchecksum(uint8_t* a, size_t len){
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);
+ struct bsdchecksum_hash* a = (struct bsdchecksum_hash*)lua_touserdata(L, 1);
uint32_t u = bsdchecksum_final(a);
char digest[32];
sprintf(digest,"%i",u);
diff --git a/src/hash/crc.c b/src/hash/crc.c
index 71047cc..f52e005 100644
--- a/src/hash/crc.c
+++ b/src/hash/crc.c
@@ -87,10 +87,7 @@ common_hash_init_update(crc16);
common_hash_init_update(crc8);
int l_crc8_final(lua_State* L){
- lua_pushstring(L, "ud");
- lua_gettable(L, 1);
-
- struct crc8_hash* a = (struct crc8_hash*)lua_touserdata(L, -1);
+ struct crc8_hash* a = (struct crc8_hash*)lua_touserdata(L, 1);
uint32_t u = crc8_final(a);
char digest[8];
sprintf(digest,"%x",u);
@@ -99,10 +96,7 @@ int l_crc8_final(lua_State* L){
}
int l_crc16_final(lua_State* L){
- lua_pushstring(L, "ud");
- lua_gettable(L, 1);
-
- struct crc16_hash* a = (struct crc16_hash*)lua_touserdata(L, -1);
+ struct crc16_hash* a = (struct crc16_hash*)lua_touserdata(L, 1);
uint32_t u = crc16_final(a);
char digest[16];
sprintf(digest,"%04x",u);
@@ -111,10 +105,7 @@ int l_crc16_final(lua_State* L){
}
int l_crc32_final(lua_State* L){
- lua_pushstring(L, "ud");
- lua_gettable(L, 1);
-
- struct crc32_hash* a = (struct crc32_hash*)lua_touserdata(L, -1);
+ struct crc32_hash* a = (struct crc32_hash*)lua_touserdata(L, 1);
uint32_t u = crc32_final(a);
char digest[32];
sprintf(digest,"%08x",u);
diff --git a/src/hash/djb2.c b/src/hash/djb2.c
index cd1fb2a..99f2a75 100644
--- a/src/hash/djb2.c
+++ b/src/hash/djb2.c
@@ -26,10 +26,7 @@ uint32_t djb2(uint8_t * in, size_t len){
common_hash_init_update(djb2);
int l_djb2_final(lua_State* L){
- lua_pushstring(L, "ud");
- lua_gettable(L, 1);
-
- struct djb2_hash* a = (struct djb2_hash*)lua_touserdata(L, -1);
+ struct djb2_hash* a = (struct djb2_hash*)lua_touserdata(L, 1);
uint32_t u = djb2_final(a);
char digest[64];
sprintf(digest,"%08x",u);
diff --git a/src/hash/fletcher.c b/src/hash/fletcher.c
index 1ab1857..16c8b40 100644
--- a/src/hash/fletcher.c
+++ b/src/hash/fletcher.c
@@ -70,10 +70,7 @@ 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);
-
- struct fletcher8_hash* a = (struct fletcher8_hash*)lua_touserdata(L, -1);
+ struct fletcher8_hash* a = (struct fletcher8_hash*)lua_touserdata(L, 1);
uint8_t u = fletcher8_final(a);
char digest[8];
sprintf(digest,"%02x",u);
@@ -82,10 +79,7 @@ int l_fletcher8_final(lua_State* L){
}
int l_fletcher16_final(lua_State* L){
- lua_pushstring(L, "ud");
- lua_gettable(L, 1);
-
- struct fletcher16_hash* a = (struct fletcher16_hash*)lua_touserdata(L, -1);
+ struct fletcher16_hash* a = (struct fletcher16_hash*)lua_touserdata(L, 1);
uint16_t u = fletcher16_final(a);
char digest[16];
sprintf(digest,"%04x",u);
@@ -94,10 +88,7 @@ int l_fletcher16_final(lua_State* L){
}
int l_fletcher32_final(lua_State* L){
- lua_pushstring(L, "ud");
- lua_gettable(L, 1);
-
- struct fletcher32_hash* a = (struct fletcher32_hash*)lua_touserdata(L, -1);
+ struct fletcher32_hash* a = (struct fletcher32_hash*)lua_touserdata(L, 1);
uint32_t u = fletcher32_final(a);
char digest[32];
sprintf(digest,"%08x",u);
diff --git a/src/hash/fnv.c b/src/hash/fnv.c
index 47af105..10c8ca2 100644
--- a/src/hash/fnv.c
+++ b/src/hash/fnv.c
@@ -39,16 +39,13 @@ 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);
+lua_common_hash_init_ni(fnv_1, fnv_1, fnv_1_init(v_1));
+lua_common_hash_init_ni(fnv_1, fnv_0, fnv_1_init(v_0));
+lua_common_hash_init_ni(fnv_1, fnv_a, fnv_1_init(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);\
+ 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,"%16lx",u);\
diff --git a/src/hash/jenkins.c b/src/hash/jenkins.c
index 6daf831..dc96ff6 100644
--- a/src/hash/jenkins.c
+++ b/src/hash/jenkins.c
@@ -16,11 +16,12 @@ void jenkins_oaat_update(uint8_t* in, size_t len, struct jenkins_oaat_hash* hash
}
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;
+ uint32_t h = hash->hash;
+ h += h << 3;
+ h ^= h >> 11;
+ h += h << 15;
- return hash->hash;
+ return h;
}
@@ -33,10 +34,7 @@ uint32_t jenkins_oaat(uint8_t* in, size_t len){
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);
+ 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);
diff --git a/src/hash/loselose.c b/src/hash/loselose.c
index 737fd2c..6ab8677 100644
--- a/src/hash/loselose.c
+++ b/src/hash/loselose.c
@@ -27,10 +27,7 @@ uint64_t loselose(uint8_t* in, size_t len){
common_hash_init_update(loselose);
int l_loselose_final(lua_State* L){
- lua_pushstring(L, "ud");
- lua_gettable(L, 1);
-
- struct loselose_hash* a = (struct loselose_hash*)lua_touserdata(L, -1);
+ struct loselose_hash* a = (struct loselose_hash*)lua_touserdata(L, 1);
uint64_t u = loselose_final(a);
char digest[64];
sprintf(digest,"%08lx",u);
diff --git a/src/hash/md5.c b/src/hash/md5.c
index 5806b48..014eefa 100644
--- a/src/hash/md5.c
+++ b/src/hash/md5.c
@@ -104,6 +104,11 @@ void md5_update(uint8_t* input, size_t len, struct md5_hash* hash){
}
void md5_final(struct md5_hash* hash, char out_stream[64]){
+ uint8_t old[bs];
+ struct md5_hash old_hash;
+ memcpy(&old_hash, hash, sizeof * hash);
+ memcpy(old, hash->buffer, bs);
+
hash->buffer[hash->bufflen] = 0x80;
if(hash->bufflen > 55) {
@@ -122,6 +127,10 @@ void md5_final(struct md5_hash* hash, char out_stream[64]){
((uint8_t*)&hash->b0)[0], ((uint8_t*)&hash->b0)[1], ((uint8_t*)&hash->b0)[2], ((uint8_t*)&hash->b0)[3],
((uint8_t*)&hash->c0)[0], ((uint8_t*)&hash->c0)[1], ((uint8_t*)&hash->c0)[2], ((uint8_t*)&hash->c0)[3],
((uint8_t*)&hash->d0)[0], ((uint8_t*)&hash->d0)[1], ((uint8_t*)&hash->d0)[2], ((uint8_t*)&hash->d0)[3]);
+
+ memcpy(hash->buffer, old, bs);
+ memcpy(hash, &old_hash, sizeof * hash);
+
}
lua_common_hash_init_ni(md5, md5, md5_init_l(L));
@@ -129,10 +138,7 @@ lua_common_hash_update(md5, md5);
//common_hash_init_update(md5);
int l_md5_final(lua_State* L){
- lua_pushstring(L, "ud");
- lua_gettable(L, 1);
-
- struct md5_hash* a = (struct md5_hash*)lua_touserdata(L, -1);
+ struct md5_hash* a = (struct md5_hash*)lua_touserdata(L, 1);
char digest[128] = {0};
md5_final(a, digest);
diff --git a/src/hash/pearson.c b/src/hash/pearson.c
index 1110891..c5fcf19 100644
--- a/src/hash/pearson.c
+++ b/src/hash/pearson.c
@@ -62,10 +62,7 @@ int l_setpearson(lua_State* L){
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);
+ struct pearson_hash* a = (struct pearson_hash*)lua_touserdata(L, 1);
uint8_t u = pearson_final(a);
char digest[8];
sprintf(digest,"%x",u);
diff --git a/src/hash/pjw.c b/src/hash/pjw.c
index 5dea872..ce2c38c 100644
--- a/src/hash/pjw.c
+++ b/src/hash/pjw.c
@@ -30,10 +30,7 @@ uint32_t pjw(uint8_t* in, size_t len){
common_hash_init_update(pjw);
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);
+ struct pjw_hash* a = (struct pjw_hash*)lua_touserdata(L, 1);
uint32_t u = pjw_final(a);
char digest[32];
sprintf(digest,"%08x",u);
diff --git a/src/hash/sdbm.c b/src/hash/sdbm.c
index 6c8e437..7b79526 100644
--- a/src/hash/sdbm.c
+++ b/src/hash/sdbm.c
@@ -26,10 +26,7 @@ uint64_t sdbm(uint8_t* in, size_t len){
common_hash_init_update(sdbm);
int l_sdbm_final(lua_State* L){
- lua_pushstring(L, "ud");
- lua_gettable(L, 1);
-
- struct sdbm_hash* a = (struct sdbm_hash*)lua_touserdata(L, -1);
+ struct sdbm_hash* a = (struct sdbm_hash*)lua_touserdata(L, 1);
uint64_t u = sdbm_final(a);
char digest[64];
sprintf(digest,"%016lx",u);
diff --git a/src/hash/sha01.c b/src/hash/sha01.c
index 243dd4f..3605dcd 100644
--- a/src/hash/sha01.c
+++ b/src/hash/sha01.c
@@ -111,6 +111,11 @@ void sha01_update(uint8_t* input, size_t len, struct sha01_hash* hash){
}
void sha01_final(struct sha01_hash* hash, char* out_stream){
+ uint8_t old[bs];
+ struct sha01_hash old_hash;
+ memcpy(&old_hash, hash, sizeof * hash);
+ memcpy(old, hash->buffer, bs);
+
hash->buffer[hash->bufflen] = 0x80;
if(hash->bufflen > 55) {
@@ -126,6 +131,9 @@ void sha01_final(struct sha01_hash* hash, char* out_stream){
sha01_round(hash);
sprintf(out_stream,"%02x%02x%02x%02x%02x",hash->h0,hash->h1,hash->h2,hash->h3,hash->h4);
+
+ memcpy(hash, &old_hash, sizeof * hash);
+ memcpy(hash->buffer, old, bs);
}
struct sha01_hash sha0_init(){
@@ -171,10 +179,7 @@ lua_common_hash_init_ni(sha0, sha0, sha01_init_l(0, L));
lua_common_hash_update(sha0, sha0);
int l_sha1_final(lua_State* L){
- lua_pushstring(L, "ud");
- lua_gettable(L, 1);
-
- struct sha01_hash* a = (struct sha01_hash*)lua_touserdata(L, -1);
+ struct sha01_hash* a = (struct sha01_hash*)lua_touserdata(L, 1);
char digest[160];
sha1_final(a, digest);
diff --git a/src/hash/sha2-256.c b/src/hash/sha2-256.c
index 99cdf64..f102a00 100644
--- a/src/hash/sha2-256.c
+++ b/src/hash/sha2-256.c
@@ -168,6 +168,11 @@ void _sha512_t_final(struct sha512_hash* hash){
}
void sha512_final(struct sha512_hash* hash, char* out_stream){
+ uint8_t old[bs];
+ struct sha512_hash old_hash;
+ memcpy(&old_hash, hash, sizeof * hash);
+ memcpy(old, hash->buffer, bs);
+
_sha512_t_final(hash);
sprintf((char*)out_stream, "%s%016lx", out_stream, hash->h0);
@@ -178,17 +183,28 @@ void sha512_final(struct sha512_hash* hash, char* out_stream){
sprintf((char*)out_stream, "%s%016lx", out_stream, hash->h5);
sprintf((char*)out_stream, "%s%016lx", out_stream, hash->h6);
sprintf((char*)out_stream, "%s%016lx", out_stream, hash->h7);
+
+ memcpy(hash, &old_hash, sizeof * hash);
+ memcpy(hash->buffer, old, bs);
}
void sha384_final(struct sha512_hash* hash, char* out_stream){
+ uint8_t old[bs];
+ struct sha512_hash old_hash;
+ memcpy(&old_hash, hash, sizeof * hash);
+ memcpy(old, hash->buffer, bs);
_sha512_t_final(hash);
+
sprintf((char*)out_stream, "%s%016lx", out_stream, hash->h0);
sprintf((char*)out_stream, "%s%016lx", out_stream, hash->h1);
sprintf((char*)out_stream, "%s%016lx", out_stream, hash->h2);
sprintf((char*)out_stream, "%s%016lx", out_stream, hash->h3);
sprintf((char*)out_stream, "%s%016lx", out_stream, hash->h4);
sprintf((char*)out_stream, "%s%016lx", out_stream, hash->h5);
+
+ memcpy(hash, &old_hash, sizeof * hash);
+ memcpy(hash->buffer, old, bs);
}
void sha512(uint8_t* in, size_t len, char* out){
@@ -219,7 +235,7 @@ struct iv sha_iv_gen(int i){
sprintf((char*)in, "SHA-512/%i",i);
struct sha512_hash a = sha512_t_init(oh);
sha512_update(in, strlen((char*)in), &a);
- sha512_final(&a, (char*)nh);
+ _sha512_t_final(&a);
return (struct iv){.h0 = a.h0, .h1 = a.h1, .h2 = a.h2, .h3 = a.h3, .h4 = a.h4, .h5 = a.h5, .h6 = a.h6, .h7 = a.h7};
}
@@ -227,10 +243,7 @@ lua_common_hash_init_ni(sha512, sha512, sha512_t_init_l(sha512_iv, L));
lua_common_hash_update(sha512, sha512);
int l_sha512_final(lua_State* L){
- lua_pushstring(L, "ud");
- lua_gettable(L, 1);
-
- struct sha512_hash* a = (struct sha512_hash*)lua_touserdata(L, -1);
+ struct sha512_hash* a = (struct sha512_hash*)lua_touserdata(L, 1);
char digest[512] = {0};
sha512_final(a, digest);
@@ -243,10 +256,7 @@ lua_common_hash_init_ni(sha384, sha384, sha512_t_init_l(sha384_iv, L));
lua_common_hash_update(sha384, sha384);
int l_sha384_final(lua_State* L){
- lua_pushstring(L, "ud");
- lua_gettable(L, 1);
-
- struct sha512_hash* a = (struct sha512_hash*)lua_touserdata(L, -1);
+ struct sha512_hash* a = (struct sha512_hash*)lua_touserdata(L, 1);
char digest[384] = {0};
sha384_final(a, digest);
@@ -255,6 +265,7 @@ int l_sha384_final(lua_State* L){
return 1;
}
+lua_common_hash_meta(sha512_t);
int l_sha512_t_init(lua_State* L){
int tt = luaL_checkinteger(L, 1);
lua_newtable(L);
@@ -265,21 +276,16 @@ int l_sha512_t_init(lua_State* L){
*a = sha512_t_init_l(sha_iv_gen(tt), L);
a->t = tt;
- luaI_tsetv(L, t, "ud", ud);
- luaI_tsetcf(L, t, "update", l_sha512_t_update);
- luaI_tsetcf(L, t, "final", l_sha512_t_final);
-
- lua_pushvalue(L, t);
+ lua_common_hash_meta_def(sha512_t);
+
+ lua_pushvalue(L, ud);
return 1;
}
lua_common_hash_update(sha512, sha512_t);
int l_sha512_t_final(lua_State* L){
- lua_pushstring(L, "ud");
- lua_gettable(L, 1);
-
- struct sha512_hash* a = (struct sha512_hash*)lua_touserdata(L, -1);
+ struct sha512_hash* a = (struct sha512_hash*)lua_touserdata(L, 1);
char digest[512] = {0};
sha512_final(a, digest);
@@ -313,7 +319,7 @@ int l_sha384(lua_State* L){
}
int l_sha512_t(lua_State* L){
- if(lua_gettop(L) == 1) return l_sha512_t_init(L);
+ if(lua_gettop(L) == 1) return l_sha512_t_init(L);
size_t len = 0;
uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
uint64_t t = luaL_checkinteger(L, 2);
diff --git a/src/hash/sysvchecksum.c b/src/hash/sysvchecksum.c
index d22270d..71f203b 100644
--- a/src/hash/sysvchecksum.c
+++ b/src/hash/sysvchecksum.c
@@ -26,10 +26,7 @@ uint32_t sysvchecksum(uint8_t* aa, size_t len){
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);
+ struct sysvchecksum_hash* a = (struct sysvchecksum_hash*)lua_touserdata(L, 1);
uint32_t u = sysvchecksum_final(a);
char digest[32];
sprintf(digest,"%x",u);
diff --git a/src/hash/xor.c b/src/hash/xor.c
index 1785414..f2e4ff0 100644
--- a/src/hash/xor.c
+++ b/src/hash/xor.c
@@ -24,10 +24,7 @@ uint8_t xor8(uint8_t* aa, size_t len){
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);
+ struct xor8_hash* a = (struct xor8_hash*)lua_touserdata(L, 1);
uint8_t u = xor8_final(a);
char digest[8];
sprintf(digest,"%02x",u);
diff --git a/tests/hash.lua b/tests/hash.lua
index 23714a9..edc754b 100644
--- a/tests/hash.lua
+++ b/tests/hash.lua
@@ -11,6 +11,8 @@ function test(name,b,exp,oargs)
local hash2
local hash3
local hash4
+ local hash5
+ local hash6
local add = ""
if oargs == nil then
hash = llib.crypto[name](b)
@@ -22,8 +24,32 @@ function test(name,b,exp,oargs)
if(llib.crypto[name.."_init"] ~= nil) then
if(oargs == nil) then
hash2 = llib.crypto[name]():update(b):final()
- else
+ hash5 = llib.crypto[name]()
+ hash6 = hash5 + b;
+ hash5 = hash5:final()
+ else
hash2 = llib.crypto[name](table.unpack(oargs)):update(b):final()
+ hash5 = llib.crypto[name](table.unpack(oargs))
+ hash6 = hash5 + b;
+ hash5 = hash5:final()
+ end
+
+ if(hash5 ~= exp) then
+ fail = true
+ functions_failed = functions_failed + 1
+ llib.io.error(name.." + then final method not working, got:\n\t"..hash5.." other was:\n\t"..exp)
+ else
+ functions_working = functions_working + 1
+ llib.io.log(name.." + then final method working "..hash5.." == "..exp)
+ end
+
+ if(hash6 ~= exp) then
+ fail = true
+ functions_failed = functions_failed + 1
+ llib.io.error(name.." + method not working, got:\n\t"..hash6.." other was:\n\t"..exp)
+ else
+ functions_working = functions_working + 1
+ llib.io.log(name.." + method working "..hash6.." == "..exp)
end
if(oargs == nil) then
@@ -85,7 +111,6 @@ function test(name,b,exp,oargs)
else
hashes_working=hashes_working + 1
end
-
end
test("adler32","meow","043c01b9")