brucefight 发表于 2013-1-26 13:34:40

md5

./md5.h
#ifndef _md5_h_
#define _md5_h_

#define SINGLE_ONE_BIT 0x80
#define BLOCK_SIZE 512
#define MOD_SIZE 448
#define APP_SIZE 64
#define BITS 8
#define A 0x67452301UL
#define B 0xEFCDAB89UL
#define C 0x98BADCFEUL
#define D 0x10325476UL
#ifdef UINT64
# undef UINT64
#endif
#ifdef UINT32
# undef UINT32
#endif
typedef unsigned long long UINT64;
typedef unsigned long UINT32;
typedef unsigned char UINT8;
typedef int INT32;
typedef struct
{
char * message;
UINT64 length;
}STRING;

STRING append_padding_bits ( char * argv );
UINT32 count_padding_bits ( UINT32 length );
void DBG_print(char *fmt, ...);
UINT32 F( UINT32 X, UINT32 Y, UINT32 Z );
UINT32 G( UINT32 X, UINT32 Y, UINT32 Z );
UINT32 H( UINT32 X, UINT32 Y, UINT32 Z );
UINT32 I( UINT32 X, UINT32 Y, UINT32 Z );
int md5(char *str, char *key, char *sign);
UINT32 rotate_left( UINT32 x, UINT32 s );

#endif

/*
*/
./md5.c
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "md5.h"

void DBG_print(char *fmt, ...)
{
return;
}
UINT32 F( UINT32 X, UINT32 Y, UINT32 Z )
{
return ( X & Y ) | ( ~X & Z );
}

UINT32 G( UINT32 X, UINT32 Y, UINT32 Z )
{
return ( X & Z ) | ( Y & ~Z );
}

UINT32 H( UINT32 X, UINT32 Y, UINT32 Z )
{
return X ^ Y ^ Z;
}

UINT32 I( UINT32 X, UINT32 Y, UINT32 Z )
{
return Y ^ ( X | ~Z );
}

UINT32 rotate_left( UINT32 x, UINT32 s )
{
return ( x << s ) | ( x >> ( 32 - s ) );
}

UINT32 count_padding_bits ( UINT32 length )
{
UINT32 div = length * BITS / BLOCK_SIZE;
UINT32 mod = length * BITS % BLOCK_SIZE;
UINT32 c_bits;
if ( mod == 0 )
c_bits = MOD_SIZE;
else
c_bits = ( MOD_SIZE + BLOCK_SIZE - mod ) % BLOCK_SIZE;
return c_bits / BITS;
}

STRING append_padding_bits ( char * argv )
{
UINT32 msg_length = strlen ( argv );
UINT32 bit_length = count_padding_bits ( msg_length );
UINT64 app_length = msg_length * BITS;
STRING string;
UINT64 i;
UINT8 temp;
string.message = (char *)malloc(msg_length + bit_length + APP_SIZE / BITS);
strncpy (string.message, argv, msg_length);
memset (string.message + msg_length, 0, bit_length);
string.message = SINGLE_ONE_BIT;
memmove(string.message + msg_length + bit_length, (char *)&app_length, sizeof( UINT64 ));
string.length = msg_length + bit_length + sizeof( UINT64 );
for(i = 0; i< string.length; i = i +1){
DBG_print("%c%02x",(i % 20)?' ':'\n', string.message);
}
for(i = 0; i< string.length; i = i + 4){
DBG_print("%c0x%x",(i % 40)?' ':'\n', *((int *)(string.message + i)));
}
DBG_print("\n");
/*change string.message to big endian */
for(i = 0; i < 4; i++)
{
temp = string.message;
string.message = string.message;
DBG_print("temp%02x\n",string.message);
string.message = temp;
}
for(i = 0; i < string.length; i= i + 4)
{
temp = string.message;
string.message = string.message;
string.message = temp;
temp = string.message;
string.message = string.message;
string.message = temp;
}

for(i = 0; i< string.length; i = i +1){
DBG_print("%c%02x",(i % 20)?' ':'\n', string.message);
}
for(i = 0; i< string.length; i = i + 4){
DBG_print("%c0x%x",(i % 40)?' ':'\n', *((int *)(string.message + i)));
}
DBG_print("\n");
return string;
}

int md5(char *str, char *key, char *sign)
{
STRING string;
UINT32 w;
INT32 chain;
UINT32 state;
UINT8 r;
UINT32 ( *auxi[ 4 ])( UINT32, UINT32, UINT32 ) = { F, G, H, I };
int roundIdx;
int argIdx;
int sIdx;
int wIdx;
int i;
int j;
const UINT32 X = {{0, 1}, {1, 5}, {5, 3}, {0, 7}};
const UINT32 S = {
{ 7, 12, 17, 22 },
{ 5, 9, 14, 20 },
{ 4, 11, 16, 23 },
{ 6, 10, 15, 21 }
};
char *temp = (char *)malloc(strlen(str) + strlen(key) +1);
memset(temp, '\0', strlen(str) + strlen(key) +1);
strncpy(temp, str, strlen(str));
strcat(temp, key);
/*strncat(temp, key, strlen(key));*/
string = append_padding_bits (temp);
free(temp);
chain = A;
chain = B;
chain = C;
chain = D;
for ( j = 0; j < string.length; j += BLOCK_SIZE / BITS)
{
memmove( (char *)w, string.message + j, BLOCK_SIZE / BITS );
memmove( state, chain, sizeof(chain) );
for ( roundIdx = 0; roundIdx < 4; roundIdx++ )
{
wIdx = X[ roundIdx ][ 0 ];
sIdx = 0;
for ( i = 0; i < 16; i++ )
{
DBG_print("%d: 0x%x 0x%x 0x%x 0x%x\n",roundIdx * 16 + i + 1, state, state, state,state);
state = state[(sIdx + 1) % 4] +
rotate_left(state +
(*auxi[ roundIdx])(state[(sIdx+1) % 4], state[(sIdx+2) % 4], state[(sIdx+3) % 4]) +
w +
(UINT32)floor((1ULL << 32) * fabs(sin(roundIdx * 16 + i + 1))),
S);
sIdx = (sIdx + 3) % 4;
wIdx = (wIdx + X) & 0xF;
}
}
chain[ 0 ] += state[ 0 ];
chain[ 1 ] += state[ 1 ];
chain[ 2 ] += state[ 2 ];
chain[ 3 ] += state[ 3 ];
}
char *temp2;
for ( i = 0; i < 4; i++ ){
temp2 = (char *)&chain ;
for(j = 3; j >= 0; j--){
sprintf (sign, "%02X", *(temp2 + j));
sign += 2;
}
}
free(string.message);
return 0;
}


int main(int argc, char** argv)
{
char sign = {'\0'};
md5("00010005            0987654321098765432101686075            2011011817533256789012345678901234BOC201101181234SZANSH01686075 000000000000000000000000000000000000                                                            112233445566", "112233445566", sign);
printf("%s\n", sign);

md5("abcdefgh", "11111111", sign);
printf("%s\n", sign);

return 0;
}
页: [1]
查看完整版本: md5