Skip to content

Commit

Permalink
Add optional 'binary' parameter to crypto.md5
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan committed Feb 8, 2025
1 parent 08da954 commit dda08fd
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/lcryptolib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,20 @@ static int sdbm(lua_State *L)
static int md5(lua_State *L)
{
size_t len;
unsigned char buffer[16];
const auto str = luaL_checklstring(L, 1, &len);
const bool binary = lua_istrue(L, 2);

unsigned char buffer[16];
md5_fn((unsigned char*)str, (int)len, buffer);

char hexbuff[32];
soup::string::bin2hexAt(hexbuff, (const char*)buffer, sizeof(buffer), soup::string::charset_hex_lower);
lua_pushlstring(L, hexbuff, sizeof(hexbuff));

if (binary) {
lua_pushlstring(L, (const char*)buffer, sizeof(buffer));
}
else {
char hexbuff[32];
soup::string::bin2hexAt(hexbuff, (const char*)buffer, sizeof(buffer), soup::string::charset_hex_lower);
lua_pushlstring(L, hexbuff, sizeof(hexbuff));
}
return 1;
}

Expand Down

0 comments on commit dda08fd

Please sign in to comment.