lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Andre Carregal wrote:
Any comments and feedback are welcome.

I propose to implement sumhexa in C and avoid 2-step loading
(the patch is attached).

--
Shmuel
--- md5lib.c	Tue May 08 19:06:34 2007
+++ md5lib_new.c	Wed May 09 14:57:02 2007
@@ -171,6 +171,26 @@
 }
 
 
+/**
+*  @param message: arbitrary binary string.
+*  @return String with the md5 hash value converted to hexadecimal digits
+*/
+static int sumhexa (lua_State *L) {
+  char buff[16], buff2[32];
+  size_t l;
+  const char *message = luaL_checklstring(L, 1, &l);
+  md5(message, l, buff);
+  for(l=0; l<16; ++l) {
+    int high = (unsigned char)buff[l] >> 4;
+    int low = buff[l] & 0xf;
+    buff2[l+l] = high < 10 ? high + '0' : high - 10 + 'a';
+    buff2[l+l+1] = low < 10 ? low + '0' : low - 10 + 'a';
+  }
+  lua_pushlstring(L, buff2, 32L);
+  return 1;
+}
+
+
 /*
 ** Assumes the table is on top of the stack.
 */
@@ -192,12 +212,13 @@
   {"exor", ex_or},
   {"crypt", crypt},
   {"decrypt", decrypt},
+  {"sumhexa", sumhexa},
   {NULL, NULL}
 };
 
 
-int luaopen_md5_core (lua_State *L) {
-  luaL_openlib(L, "md5", md5lib, 0);
+int luaopen_md5 (lua_State *L) {
+  luaL_register(L, "md5", md5lib);
   set_info (L);
   return 1;
 }