Skip to content

Commit

Permalink
Merge pull request #1 from starwing/master
Browse files Browse the repository at this point in the history
a improve to md5 module.
  • Loading branch information
Tomás Guisasola authored Apr 15, 2019
2 parents 064d4a0 + d137a15 commit 2d8e18d
Show file tree
Hide file tree
Showing 20 changed files with 93 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.o
*.obj
*.so
*.dll
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ src/$(DES56_LIBNAME) : $(DES56_OBJS) $(COMPAT52_OBJS)

install: src/$(MD5_LIBNAME) src/$(DES56_LIBNAME)
mkdir -p $(LUA_LIBDIR)/md5
cp src/$(MD5_LIBNAME) $(LUA_LIBDIR)/md5/core.so
cp src/$(MD5_LIBNAME) $(LUA_LIBDIR)/md5.so
mkdir -p $(LUA_DIR)
cp $(MD5_LUAS) $(LUA_DIR)
cp src/$(DES56_LIBNAME) $(LUA_LIBDIR)

clean:
Expand Down
Empty file modified Makefile.win
100644 → 100755
Empty file.
Empty file modified config.win
100644 → 100755
Empty file.
Empty file modified doc/us/md5.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified rockspec/md5-1.1.1-1.rockspec
100644 → 100755
Empty file.
Empty file modified rockspec/md5-1.1.2-1.rockspec
100644 → 100755
Empty file.
Empty file modified rockspec/md5-cvs-1.rockspec
100644 → 100755
Empty file.
Empty file modified src/des56.c
100644 → 100755
Empty file.
Empty file modified src/des56.h
100644 → 100755
Empty file.
Empty file modified src/ldes56.h
100644 → 100755
Empty file.
74 changes: 65 additions & 9 deletions src/md5.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <lua.h>
#include <lauxlib.h>

#if ! defined (LUA_VERSION_NUM) || LUA_VERSION_NUM < 501
#include "compat-5.1.h"
#endif


#include "md5.h"


#define WORD 32
#define MASK 0xFFFFFFFF
#if __STDC_VERSION__ >= 199901L
#include <stdint.h>
typedef uint32_t WORD32;
#else
typedef unsigned int WORD32;
#endif


/**
Expand All @@ -27,9 +32,31 @@ typedef unsigned int WORD32;
* @param output: buffer to receive the hash value. Its size must be
* (at least) HASHSIZE.
*/
void md5 (const char *message, long len, char *output);
void md5 (const char *message, size_t len, char output[HASHSIZE]);

/**
* init a new md5 calculate context
* @param m a uninitialized md5_t type context
*/
void md5_init (md5_t *m);

/**
* update message to md5 context
* @param m a initialized md5_t type context
* @param message aribtary string
* @param len message length
* @return true if update completed, means len is not the multiples of 64.
* false if you can continue update.
*/
int md5_update (md5_t *m, const char *message, size_t len);

/**
* finish md5 calculate.
* @param m a md5_type context which previous md5_update on it return true.
* @param output buffer to receive the hash value. its size must be
* (at least) HASHSIZE.
*/
void md5_finish (md5_t *m, char output[HASHSIZE]);

/*
** Realiza a rotacao no sentido horario dos bits da variavel 'D' do tipo WORD32.
Expand Down Expand Up @@ -191,8 +218,7 @@ static int converte (WORD32 *x, const char *pt, int num, int old_status) {
}



void md5 (const char *message, long len, char *output) {
void md5 (const char *message, size_t len, char output[HASHSIZE]) {
WORD32 d[4];
int status = 0;
long i = 0;
Expand All @@ -212,3 +238,33 @@ void md5 (const char *message, long len, char *output) {
word32tobytes(d, output);
}

void md5_init(md5_t *m) {
inic_digest(m->d);
m->len = 0;
}

int md5_update(md5_t *m, const char *message, size_t len) {
WORD32 *d = m->d;
size_t addlen = m->len;
int status = 0, i = 0;
while (status != 2) {
WORD32 d_old[4];
WORD32 wbuff[16];
int numbytes = (len-i >= 64) ? 64 : len-i;
if (status != 1 && numbytes == 0 && len != 0)
break;
/*salva os valores do vetor digest*/
d_old[0]=d[0]; d_old[1]=d[1]; d_old[2]=d[2]; d_old[3]=d[3];
status = converte(wbuff, message+i, numbytes, status);
if (status == 2) put_length(wbuff, addlen+len);
digest(wbuff, d);
d[0]+=d_old[0]; d[1]+=d_old[1]; d[2]+=d_old[2]; d[3]+=d_old[3];
i += numbytes;
}
m->len += len;
return status == 2;
}

void md5_finish (md5_t *m, char output[HASHSIZE]) {
word32tobytes(m->d, output);
}
2 changes: 1 addition & 1 deletion src/md5.def
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ LIBRARY core.dll
DESCRIPTION "LuaMD5"
VERSION 1.2
EXPORTS
luaopen_md5_core
luaopen_md5
24 changes: 22 additions & 2 deletions src/md5.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,32 @@
#define md5_h

#include <lua.h>
#include <stddef.h>


#define HASHSIZE 16

void md5 (const char *message, long len, char *output);
int luaopen_md5_core (lua_State *L);
#if __STDC_VERSION__ >= 199901L
#include <stdint.h>
typedef uint32_t WORD32;
#else
/* static assert that int equal or greater than 32bit. */
typedef char static_assert_sizeof_int
[sizeof(unsigned int) >= 4 ? 1 : -1];
typedef unsigned int WORD32;
#endif

typedef struct md5_t {
WORD32 d[4];
size_t len;
} md5_t;

void md5_init (md5_t *m);
int md5_update (md5_t *m, const char *message, size_t len);
void md5_finish (md5_t *m, char output[HASHSIZE]);
void md5 (const char *message, size_t len, char output[HASHSIZE]);

LUALIB_API int luaopen_md5 (lua_State *L);


#endif
Empty file modified tests/DES56/Makefile
100644 → 100755
Empty file.
Empty file modified tests/DES56/fdemo.c
100644 → 100755
Empty file.
Empty file modified tests/DES56/ftest.c
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion vc6/md5.def
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ LIBRARY core.dll
DESCRIPTION "LuaMD5"
VERSION 1.0.1
EXPORTS
luaopen_md5_core
luaopen_md5
Empty file modified vc6/md5.dsw
100644 → 100755
Empty file.
6 changes: 1 addition & 5 deletions vc6/md5_dll.dsp
100644 → 100755

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2d8e18d

Please sign in to comment.