Skip to content

Commit

Permalink
update md5_app
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelLiang committed Jul 27, 2018
1 parent c127edb commit 745c975
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 41 deletions.
61 changes: 21 additions & 40 deletions D3-md5_app/user/md5.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,24 @@
* filename: md5.c
*/

#define _ESP8266
#define DEBUG

#ifdef _WIN32
#include <stdio.h>
#include <string.h>
#define MEMCPY memcpy
#define STRLEN strlen
#define STRNCMP strncmp

#elif defined(_ESP8266)
#include "ets_sys.h"
#include "osapi.h"
#include "user_interface.h"

#define MEMCPY os_memcpy
#define STRLEN os_strlen
#define STRNCMP os_strncmp
#define MEMCMP os_memcmp

#endif

#include "md5.h"

/****************************************************************/
#define DEBUG


#ifdef DEBUG
#ifdef _WIN32
#define debug(fmt, args...) printf(fmt, ##args)
#define debugX(level, fmt, args...) if(DEBUG>=level) printf(fmt, ##args);
#elif defined(__linux__)
#define debug(fmt, args...) printf(fmt, ##args)
#define debugX(level, fmt, args...) if(DEBUG>=level) printf(fmt, ##args);
#elif defined(_ESP8266)
#define debug(fmt, args...) os_printf(fmt, ##args)
#define debugX(level, fmt, args...) if(DEBUG>=level) os_printf(fmt, ##args);
#endif
#else
#define debug(fmt, args...)
#define debugX(level, fmt, args...)
Expand All @@ -49,15 +31,18 @@ unsigned char PADDING[] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

void MD5Init(MD5_CTX *context) {
void ICACHE_FLASH_ATTR
MD5Init(MD5_CTX *context) {
context->count[0] = 0;
context->count[1] = 0;
context->state[0] = 0x67452301;
context->state[1] = 0xEFCDAB89;
context->state[2] = 0x98BADCFE;
context->state[3] = 0x10325476;
}
void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputlen) {

void ICACHE_FLASH_ATTR
MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputlen) {
unsigned int i = 0, index = 0, partlen = 0;
index = (context->count[0] >> 3) & 0x3F;
partlen = 64 - index;
Expand All @@ -77,7 +62,9 @@ void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputlen) {
}
MEMCPY(&context->buffer[index], &input[i], inputlen - i);
}
void MD5Final(MD5_CTX *context, unsigned char digest[16]) {

void ICACHE_FLASH_ATTR
MD5Final(MD5_CTX *context, unsigned char digest[16]) {
unsigned int index = 0, padlen = 0;
unsigned char bits[8];
index = (context->count[0] >> 3) & 0x3F;
Expand All @@ -87,7 +74,8 @@ void MD5Final(MD5_CTX *context, unsigned char digest[16]) {
MD5Update(context, bits, 8);
MD5Encode(digest, context->state, 16);
}
void MD5Encode(unsigned char *output, unsigned int *input, unsigned int len) {
void ICACHE_FLASH_ATTR
MD5Encode(unsigned char *output, unsigned int *input, unsigned int len) {
unsigned int i = 0, j = 0;
while (j < len) {
output[j] = input[i] & 0xFF;
Expand All @@ -98,7 +86,8 @@ void MD5Encode(unsigned char *output, unsigned int *input, unsigned int len) {
j += 4;
}
}
void MD5Decode(unsigned int *output, unsigned char *input, unsigned int len) {
void ICACHE_FLASH_ATTR
MD5Decode(unsigned int *output, unsigned char *input, unsigned int len) {
unsigned int i = 0, j = 0;
while (j < len) {
output[i] = (input[j]) | (input[j + 1] << 8) | (input[j + 2] << 16)
Expand All @@ -107,7 +96,9 @@ void MD5Decode(unsigned int *output, unsigned char *input, unsigned int len) {
j += 4;
}
}
void MD5Transform(unsigned int state[4], unsigned char block[64]) {

void ICACHE_FLASH_ATTR
MD5Transform(unsigned int state[4], unsigned char block[64]) {
unsigned int a = state[0];
unsigned int b = state[1];
unsigned int c = state[2];
Expand Down Expand Up @@ -187,14 +178,16 @@ void MD5Transform(unsigned int state[4], unsigned char block[64]) {
state[3] += d;
}

void md5_test(void) {
void ICACHE_FLASH_ATTR
md5_test(void) {
MD5_CTX md5;
MD5Init(&md5);
int i;
unsigned char encrypt[] = "admin";
unsigned char decrypt[16];
MD5Update(&md5, encrypt, STRLEN((char *) encrypt));
MD5Final(&md5, decrypt);
debug("md5 test\r\n");
debug("*********************************\r\n");
debug("before encrypt:%s\r\nafter encrypt 16bit:", encrypt);
for (i = 4; i < 12; i++) {
Expand All @@ -207,16 +200,4 @@ void md5_test(void) {
}
debug("\r\n");
debug("*********************************\r\n");

#ifdef _WIN32
getchar();
#endif
}

#ifdef _WIN32
int main(int argc, char *argv[])
{
md5_test();
}

#endif
1 change: 0 additions & 1 deletion D3-md5_app/user/user_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ init_done_cb_init(void) {
void ICACHE_FLASH_ATTR
user_init(void) {
//uart_init(BIT_RATE_115200, BIT_RATE_115200);
os_printf("SDK version:%s\n", system_get_sdk_version());
system_init_done_cb(init_done_cb_init);
}

0 comments on commit 745c975

Please sign in to comment.