#ifndef UINT32
typedef unsigned int UINT32;
#endif
#ifndef UINT64
typedef unsigned __int64 UINT64;
#endif
#define SINGLE_ONE_BIT 0x80
#define BLOCK_SIZE 512
#define MOD_SIZE 448
#define APP_SIZE 64
#define BITS 8
typedef struct
{
char *message;
UINT32 length;
}STRING;
#define A 0x67452301
#define B 0xefcdab89
#define C 0x98badcfe
#define D 0x10325476
#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21
UINT32 count_padding_bits(UINT32 length)
{
UINT32 div = length * BITS / BLOCK_SIZE;
UINT32 mod = length * BITS % BLOCK_SIZE;
UINT32 c_bits;
if(0 == mod)
c_bits = MOD_SIZE;
else
c_bits = (MOD_SIZE - mod + BLOCK_SIZE) % BLOCK_SIZE;
return c_bits / 8;
}
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;
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[msg_length] = SINGLE_ONE_BIT;
memmove(string.message + msg_length + bit_length,(char*)&app_length,sizeof(UINT64));
string.length = msg_length + bit_length + sizeof(UINT64);
return string;
}
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));
}
void FF(UINT32 &X, UINT32 Y, UINT32 Z, UINT32 W, UINT32 m, UINT32 s, UINT32 t)
{
X = rotate_left((X + F(Y,Z,W) + m + t),s) + Y;
}
void GG(UINT32 &X, UINT32 Y, UINT32 Z, UINT32 W, UINT32 m, UINT32 s, UINT32 t)
{
X = rotate_left((X + G(Y,Z,W) + m + t),s) + Y;
}
void HH(UINT32 &X, UINT32 Y, UINT32 Z, UINT32 W, UINT32 m, UINT32 s, UINT32 t)
{
X = rotate_left((X + H(Y,Z,W) + m + t),s) + Y;
}
void II(UINT32 &X, UINT32 Y, UINT32 Z, UINT32 W, UINT32 m, UINT32 s, UINT32 t)
{
X = rotate_left((X + I(Y,Z,W) + m + t),s) + Y;
}
void ComputeMD5(char buf[])
{
UINT32 chain[4];
UINT32 state[4];
UINT32 w[16];
chain[0] = A;
chain[1] = B;
chain[2] = C;
chain[3] = D;
STRING string = append_padding_bits(buf);
for(int i=0;i<string.length;i+=BLOCK_SIZE/BITS)
{
memmove((char*)w,string.message+i,BLOCK_SIZE/BITS);
memmove(state,chain,sizeof(chain));
/* Round 1 */
FF (state[0], state[1], state[2], state[3], w[ 0], S11, 0xd76aa478); /* 1 */
FF (state[3], state[0], state[1], state[2], w[ 1], S12, 0xe8c7b756); /* 2 */
FF (state[2], state[3], state[0], state[1], w[ 2], S13, 0x242070db); /* 3 */
FF (state[1], state[2], state[3], state[0], w[ 3], S14, 0xc1bdceee); /* 4 */
FF (state[0], state[1], state[2], state[3], w[ 4], S11, 0xf57c0faf); /* 5 */
FF (state[3], state[0], state[1], state[2], w[ 5], S12, 0x4787c62a); /* 6 */
FF (state[2], state[3], state[0], state[1], w[ 6], S13, 0xa8304613); /* 7 */
FF (state[1], state[2], state[3], state[0], w[ 7], S14, 0xfd469501); /* 8 */
FF (state[0], state[1], state[2], state[3], w[ 8], S11, 0x698098d8); /* 9 */
FF (state[3], state[0], state[1], state[2], w[ 9], S12, 0x8b44f7af); /* 10 */
FF (state[2], state[3], state[0], state[1], w[10], S13, 0xffff5bb1); /* 11 */
FF (state[1], state[2], state[3], state[0], w[11], S14, 0x895cd7be); /* 12 */
FF (state[0], state[1], state[2], state[3], w[12], S11, 0x6b901122); /* 13 */
FF (state[3], state[0], state[1], state[2], w[13], S12, 0xfd987193); /* 14 */
FF (state[2], state[3], state[0], state[1], w[14], S13, 0xa679438e); /* 15 */
FF (state[1], state[2], state[3], state[0], w[15], S14, 0x49b40821); /* 16 */
/* Round 2 */
GG (state[0], state[1], state[2], state[3], w[ 1], S21, 0xf61e2562); /* 17 */
GG (state[3], state[0], state[1], state[2], w[ 6], S22, 0xc040b340); /* 18 */
GG (state[2], state[3], state[0], state[1], w[11], S23, 0x265e5a51); /* 19 */
GG (state[1], state[2], state[3], state[0], w[ 0], S24, 0xe9b6c7aa); /* 20 */
GG (state[0], state[1], state[2], state[3], w[ 5], S21, 0xd62f105d); /* 21 */
GG (state[3], state[0], state[1], state[2], w[10], S22, 0x2441453); /* 22 */
GG (state[2], state[3], state[0], state[1], w[15], S23, 0xd8a1e681); /* 23 */
GG (state[1], state[2], state[3], state[0], w[ 4], S24, 0xe7d3fbc8); /* 24 */
GG (state[0], state[1], state[2], state[3], w[ 9], S21, 0x21e1cde6); /* 25 */
GG (state[3], state[0], state[1], state[2], w[14], S22, 0xc33707d6); /* 26 */
GG (state[2], state[3], state[0], state[1], w[ 3], S23, 0xf4d50d87); /* 27 */
GG (state[1], state[2], state[3], state[0], w[ 8], S24, 0x455a14ed); /* 28 */
GG (state[0], state[1], state[2], state[3], w[13], S21, 0xa9e3e905); /* 29 */
GG (state[3], state[0], state[1], state[2], w[ 2], S22, 0xfcefa3f8); /* 30 */
GG (state[2], state[3], state[0], state[1], w[ 7], S23, 0x676f02d9); /* 31 */
GG (state[1], state[2], state[3], state[0], w[12], S24, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH (state[0], state[1], state[2], state[3], w[ 5], S31, 0xfffa3942); /* 33 */
HH (state[3], state[0], state[1], state[2], w[ 8], S32, 0x8771f681); /* 34 */
HH (state[2], state[3], state[0], state[1], w[11], S33, 0x6d9d6122); /* 35 */
HH (state[1], state[2], state[3], state[0], w[14], S34, 0xfde5380c); /* 36 */
HH (state[0], state[1], state[2], state[3], w[ 1], S31, 0xa4beea44); /* 37 */
HH (state[3], state[0], state[1], state[2], w[ 4], S32, 0x4bdecfa9); /* 38 */
HH (state[2], state[3], state[0], state[1], w[ 7], S33, 0xf6bb4b60); /* 39 */
HH (state[1], state[2], state[3], state[0], w[10], S34, 0xbebfbc70); /* 40 */
HH (state[0], state[1], state[2], state[3], w[13], S31, 0x289b7ec6); /* 41 */
HH (state[3], state[0], state[1], state[2], w[ 0], S32, 0xeaa127fa); /* 42 */
HH (state[2], state[3], state[0], state[1], w[ 3], S33, 0xd4ef3085); /* 43 */
HH (state[1], state[2], state[3], state[0], w[ 6], S34, 0x4881d05); /* 44 */
HH (state[0], state[1], state[2], state[3], w[ 9], S31, 0xd9d4d039); /* 45 */
HH (state[3], state[0], state[1], state[2], w[12], S32, 0xe6db99e5); /* 46 */
HH (state[2], state[3], state[0], state[1], w[15], S33, 0x1fa27cf8); /* 47 */
HH (state[1], state[2], state[3], state[0], w[ 2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II (state[0], state[1], state[2], state[3], w[ 0], S41, 0xf4292244); /* 49 */
II (state[3], state[0], state[1], state[2], w[ 7], S42, 0x432aff97); /* 50 */
II (state[2], state[3], state[0], state[1], w[14], S43, 0xab9423a7); /* 51 */
II (state[1], state[2], state[3], state[0], w[ 5], S44, 0xfc93a039); /* 52 */
II (state[0], state[1], state[2], state[3], w[12], S41, 0x655b59c3); /* 53 */
II (state[3], state[0], state[1], state[2], w[ 3], S42, 0x8f0ccc92); /* 54 */
II (state[2], state[3], state[0], state[1], w[10], S43, 0xffeff47d); /* 55 */
II (state[1], state[2], state[3], state[0], w[ 1], S44, 0x85845dd1); /* 56 */
II (state[0], state[1], state[2], state[3], w[ 8], S41, 0x6fa87e4f); /* 57 */
II (state[3], state[0], state[1], state[2], w[15], S42, 0xfe2ce6e0); /* 58 */
II (state[2], state[3], state[0], state[1], w[ 6], S43, 0xa3014314); /* 59 */
II (state[1], state[2], state[3], state[0], w[13], S44, 0x4e0811a1); /* 60 */
II (state[0], state[1], state[2], state[3], w[ 4], S41, 0xf7537e82); /* 61 */
II (state[3], state[0], state[1], state[2], w[11], S42, 0xbd3af235); /* 62 */
II (state[2], state[3], state[0], state[1], w[ 2], S43, 0x2ad7d2bb); /* 63 */
II (state[1], state[2], state[3], state[0], w[ 9], S44, 0xeb86d391); /* 64 */
}
chain[0] += state[0];
chain[1] += state[1];
chain[2] += state[2];
chain[3] += state[3];
memmove(r+0,(char*)&chain[0],sizeof(UINT32));
memmove(r+4,(char*)&chain[1],sizeof(UINT32));
memmove(r+8,(char*)&chain[2],sizeof(UINT32));
memmove(r+12,(char*)&chain[3],sizeof(UINT32));
}
void main(int argc,TCHAR*argv[])
{
ComputeMD5("");
}