From c177b7a0ec55307e4f1352ca8b68c3e059c2add6 Mon Sep 17 00:00:00 2001 From: ame Date: Mon, 23 Oct 2023 21:35:42 -0500 Subject: move md5 and fix memory leaks lol --- src/hash/md5.c | 24 +++++++++++++++--------- src/hash/sha01.c | 4 ++-- 2 files changed, 17 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/hash/md5.c b/src/hash/md5.c index d898d5d..7671066 100644 --- a/src/hash/md5.c +++ b/src/hash/md5.c @@ -20,16 +20,14 @@ static const uint32_t s[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21}; -int l_md5(lua_State* L){ +void i_md5(char* input, char out_stream[64]){ uint32_t a0 = 0x67452301; uint32_t b0 = 0xefcdab89; uint32_t c0 = 0x98badcfe; uint32_t d0 = 0x10325476; int len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, NULL); - for(int i = 0; a[i]!='\0'; i++) len++; - + for(int i = 0; input[i] != '\0'; i++) len++; int tlen = ((((len + 8) /64) + 1) * 64) - 8; uint8_t* b = NULL; @@ -37,7 +35,7 @@ int l_md5(lua_State* L){ b = calloc(tlen + 64, 1); //add padding (0x80 to the end) - memcpy(b, a, len); + memcpy(b, input, len); b[len] = 0x80; //add length to end @@ -86,16 +84,24 @@ int l_md5(lua_State* L){ d0 += D; } - char ou[64]; - sprintf(ou,"%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x", + + sprintf(out_stream,"%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x", ((uint8_t*)&a0)[0], ((uint8_t*)&a0)[1], ((uint8_t*)&a0)[2], ((uint8_t*)&a0)[3], ((uint8_t*)&b0)[0], ((uint8_t*)&b0)[1], ((uint8_t*)&b0)[2], ((uint8_t*)&b0)[3], ((uint8_t*)&c0)[0], ((uint8_t*)&c0)[1], ((uint8_t*)&c0)[2], ((uint8_t*)&c0)[3], ((uint8_t*)&d0)[0], ((uint8_t*)&d0)[1], ((uint8_t*)&d0)[2], ((uint8_t*)&d0)[3]); - lua_pushstring(L, ou); - free(b); +} + +int l_md5(lua_State* L){ + + + char* a = (char*)luaL_checklstring(L, 1, NULL); + + char digest[64]; + i_md5(a, digest); + lua_pushstring(L, digest); return 1; }; diff --git a/src/hash/sha01.c b/src/hash/sha01.c index e0cf1c7..b606daa 100644 --- a/src/hash/sha01.c +++ b/src/hash/sha01.c @@ -29,7 +29,7 @@ void i_sha01(unsigned version, char* out_stream, const char* input){ uint32_t hat = 0; for(int z = 0; z < tlen; z+=(512/8)){ uint32_t W[80]; - memset (W, 0, 80 * sizeof (uint32_t)); + memset(W, 0, 80 * sizeof (uint32_t)); for(int i = 0; i != 16; i++){ int t = 24; @@ -83,7 +83,7 @@ void i_sha01(unsigned version, char* out_stream, const char* input){ } sprintf(out_stream,"%02x%02x%02x%02x%02x",h0,h1,h2,h3,h4); - return; + free(by); } int l_sha1(lua_State* L){ -- cgit v1.2.3