Skip to content

Commit

Permalink
Merge pull request whitecatboard#306 from the0ne/md5_hash
Browse files Browse the repository at this point in the history
added md5 hash support
  • Loading branch information
jolivepetrus authored Oct 15, 2019
2 parents 6e4b535 + f11b108 commit 97536de
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 8 deletions.
131 changes: 131 additions & 0 deletions components/lua/modules/sys/md5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright (C) 2015 - 2018, IBEROXARXA SERVICIOS INTEGRALES, S.L.
* Copyright (C) 2015 - 2019, Thomas E. Horner ([email protected])
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
* * The WHITECAT logotype cannot be changed, you can remove it, but you
* cannot change it in any way. The WHITECAT logotype is:
*
* /\ /\
* / \_____/ \
* /_____________\
* W H I T E C A T
*
* * Redistributions in binary form must retain all copyright notices printed
* to any local or remote output device. This include any reference to
* Lua RTOS, whitecatboard.org, Lua, and other copyright notices that may
* appear in the future.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Lua RTOS, Lua md5 module
*
*/

#include "lua.h"
#include "lauxlib.h"
#include "modules.h"

#if CONFIG_LUA_RTOS_LUA_USE_MD5

#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <rom/md5_hash.h>
#include <sys/misc/hex_string.h>

#define DIGEST_VAL_LENGTH 16
#define DIGEST_HEX_LENGTH 2 * DIGEST_VAL_LENGTH

static int lmd5_string(lua_State *L) {
size_t length;
const uint8_t *string = (uint8_t *) luaL_checklstring(L, 1, &length);
unsigned char digest[DIGEST_VAL_LENGTH];

struct MD5Context ctx;
MD5Init(&ctx);
MD5Update(&ctx, string, length);
MD5Final(digest, &ctx);

char digest_string[DIGEST_HEX_LENGTH];
val_to_hex_string_caps(digest_string, (char *)digest, DIGEST_VAL_LENGTH, 0, 0, 0);

lua_pushlstring(L, digest_string, DIGEST_HEX_LENGTH);
return 1;
}

#define BUFFER_SIZE 64
static int lmd5_file(lua_State *L) {
const char* binary = luaL_checkstring(L, 1);

FILE *fp;
fp = fopen(binary, "rb");
if (!fp) {
return luaL_fileresult(L, 0, binary);
}

struct MD5Context ctx;
MD5Init(&ctx);

uint8_t buffer[BUFFER_SIZE];
size_t length;
while((length = fread(buffer, 1, BUFFER_SIZE, fp))) {
MD5Update(&ctx, buffer, length);
}
if (ferror(fp)) {
int retval = luaL_fileresult(L, 0, binary);
fclose(fp);
return retval;
}
fclose(fp);

unsigned char digest[DIGEST_VAL_LENGTH];
MD5Final(digest, &ctx);

char digest_string[DIGEST_HEX_LENGTH];
val_to_hex_string_caps(digest_string, (char *)digest, DIGEST_VAL_LENGTH, 0, 0, 0);

lua_pushlstring(L, digest_string, DIGEST_HEX_LENGTH);
return 1;
}

static const LUA_REG_TYPE md5_map[] =
{
{ LSTRKEY( "file" ), LFUNCVAL( lmd5_file ) },
{ LSTRKEY( "string" ), LFUNCVAL( lmd5_string ) },
{ LNILKEY, LNILVAL }
};

int luaopen_md5(lua_State *L) {
#if !LUA_USE_ROTABLE
luaL_newlib(L, md5_map);
return 1;
#else
return 0;
#endif
}

MODULE_REGISTER_ROM(MD5, md5, md5_map, luaopen_md5, 1);

#endif
4 changes: 4 additions & 0 deletions components/sys/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,10 @@ menu "Lua RTOS"
config LUA_RTOS_LUA_USE_CRC
bool "Include crc module in build"
default y

config LUA_RTOS_LUA_USE_MD5
bool "Include md5 module in build"
default y
endmenu

menu "Lua RTOS Modules"
Expand Down
20 changes: 13 additions & 7 deletions components/sys/misc/hex_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@

// Convert a buffer coded into an hex string (hbuff) into a byte buffer (vbuff)
// Length of byte buffer is len
void hex_string_to_val(char *hbuff, char *vbuff, int len, int rev) {
void hex_string_to_val(char *hbuff, char *vbuff, int len, int reverse) {
int i;
char c;

// If reverse, put hbuff at the last byte
if (rev) {
if (reverse) {
while(*hbuff) hbuff++;
hbuff -= 2;
}
Expand Down Expand Up @@ -88,7 +88,7 @@ void hex_string_to_val(char *hbuff, char *vbuff, int len, int rev) {

*vbuff = c;

if (rev) {
if (reverse) {
hbuff -= 3;
} else {
hbuff++;
Expand All @@ -100,8 +100,9 @@ void hex_string_to_val(char *hbuff, char *vbuff, int len, int rev) {

// Convert byte buffer (vbuff argument) of len argument size into a hex
// string buffer (hbuff argument) into a )
void val_to_hex_string(char *hbuff, char *vbuff, int len, int reverse) {
void val_to_hex_string_caps(char *hbuff, char *vbuff, int len, int reverse, int caps, int terminate) {
int i;
char base = (caps ? 'A':'a');

if (reverse) {
vbuff += (len - 1);
Expand All @@ -113,7 +114,7 @@ void val_to_hex_string(char *hbuff, char *vbuff, int len, int reverse) {
}

if ((((*vbuff & 0xf0) >> 4) >= 10) && (((*vbuff & 0xf0) >> 4) <= 15)) {
*hbuff = 'A' + (((*vbuff & 0xf0) >> 4) - 10);
*hbuff = base + (((*vbuff & 0xf0) >> 4) - 10);
}
hbuff++;

Expand All @@ -122,7 +123,7 @@ void val_to_hex_string(char *hbuff, char *vbuff, int len, int reverse) {
}

if (((*vbuff & 0x0f) >= 10) && ((*vbuff & 0x0f) <= 15)) {
*hbuff = 'A' + ((*vbuff & 0x0f) - 10);
*hbuff = base + ((*vbuff & 0x0f) - 10);
}
hbuff++;

Expand All @@ -133,6 +134,11 @@ void val_to_hex_string(char *hbuff, char *vbuff, int len, int reverse) {
}
}

*hbuff = 0x00;
if (terminate) {
*hbuff = 0x00;
}
}

void val_to_hex_string(char *hbuff, char *vbuff, int len, int reverse) {
val_to_hex_string_caps(hbuff, vbuff, len, reverse, 1, 1);
}
4 changes: 3 additions & 1 deletion components/sys/misc/hex_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
#ifndef _LORA_COMMON_H
#define _LORA_COMMON_H

void hex_string_to_val(char *hbuff, char *vbuff, int len, int rev);
void hex_string_to_val(char *hbuff, char *vbuff, int len, int reverse);

void val_to_hex_string(char *hbuff, char *vbuff, int len, int reverse);
void val_to_hex_string_caps(char *hbuff, char *vbuff, int len, int reverse, int caps, int terminate);

#endif /* _LORA_COMMON_H */

0 comments on commit 97536de

Please sign in to comment.