---
title: "nth5"
author: "mmdev"
date: "2011-12-19T20:55:00Z"
tags: []
slug: "nth5"
draft: false
lastmod: "2012-07-23T23:48:59Z"
id: 1604804
url: "https://mmdev.iteye.com/blog/1604804"
---

#include &lt;stdlib.h&gt;  
#include &lt;string.h&gt;  
#include &lt;stdio.h&gt;  
#include &lt;math.h&gt;  
#define SINGLE\_ONE\_BIT 0x80  
#define BLOCK\_SIZE 512  
#define MOD\_SIZE 448  
#define APP\_SIZE 64  
#define BITS 8  
// MD5 Chaining Variable  
#define A 0x67452301UL  
#define B 0xEFCDAB89UL  
#define C 0x98BADCFEUL  
#define D 0x10325476UL  
// Creating own types  
#ifdef UINT64  
\# undef UINT64  
#endif  
#ifdef UINT32  
\# undef UINT32  
#endif  
typedef unsigned long long UINT64;  
typedef unsigned long UINT32;  
typedef unsigned char UINT8;  
typedef struct  
{  
char * message;  
UINT64 length;  
}STRING;  
const UINT32 X\[4]\[2] = {{0, 1}, {1, 5}, {5, 3}, {0, 7}};  
// Constants for MD5 transform routine.  
const UINT32 S\[4]\[4] = {  
{ 7, 12, 17, 22 },  
{ 5, 9, 14, 20 },  
{ 4, 11, 16, 23 },  
{ 6, 10, 15, 21 }  
};  
// F, G, H and I are basic MD5 functions.  
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 );  
}  
// rotates x left s bits.  
UINT32 rotate\_left( UINT32 x, UINT32 s )  
{  
return ( x &lt;&lt; s ) | ( x &gt;&gt; ( 32 - s ) );  
}  
// Pre-processin  
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;  
string.message = (char \*)malloc(msg\_length + bit\_length + APP\_SIZE / BITS);  
// Save message  
strncpy ( string.message, argv, msg\_length );  
// Pad out to mod 64.  
memset ( string.message + msg\_length, 0, bit\_length );  
string.message \[ msg\_length ] = SINGLE\_ONE\_BIT;  
// Append length (before padding).  
memmove ( string.message + msg\_length + bit\_length, (char \*)&app\_length, sizeof( UINT64 ) );  
string.length = msg\_length + bit\_length + sizeof( UINT64 );  
return string;  
}  
int main ( int argc, char \*argv\[] )  
{  
STRING string;  
UINT32 w\[16];  
UINT32 chain\[4];  
UINT32 state\[4];  
UINT8 r\[16];  
UINT32 ( \*auxi\[ 4 ])( UINT32, UINT32, UINT32 ) = { F, G, H, I };  
int roundIdx;  
int argIdx;  
int sIdx;  
int wIdx;  
int i;  
int j;  
if ( argc &lt; 2 )  
{  
fprintf ( stderr, "usage: %s string ...\\n", argv\[ 0 ] );  
return EXIT\_FAILURE;  
}  
for ( argIdx = 1; argIdx &lt; argc; argIdx++ )  
{  
string = append\_padding\_bits ( argv\[ argIdx ] );  
// MD5 initialization.  
chain\[0] = A;  
chain\[1] = B;  
chain\[2] = C;  
chain\[3] = D;  
for ( j = 0; j &lt; string.length; j += BLOCK\_SIZE / BITS)  
{  
memmove ( (char \*)w, string.message + j, BLOCK\_SIZE / BITS );  
memmove ( state, chain, sizeof(chain) );  
for ( roundIdx = 0; roundIdx &lt; 4; roundIdx++ )  
{  
wIdx = X\[ roundIdx ]\[ 0 ];  
sIdx = 0;  
for ( i = 0; i &lt; 16; i++ )  
{  
// FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.  
// Rotation is separate from addition to prevent recomputation.  
state\[sIdx] = state \[ (sIdx + 1) % 4 ] +  
rotate\_left ( state\[sIdx] +  
( \*auxi\[ roundIdx ] )  
( state\[(sIdx+1) % 4], state\[(sIdx+2) % 4], state\[(sIdx+3) % 4]) +  
w\[ wIdx ] +  
(UINT32)floor( (1ULL &lt;&lt; 32) * fabs(sin( roundIdx * 16 + i + 1 )) ),  
S\[ roundIdx ]\[ i % 4 ]);  
sIdx = ( sIdx + 3 ) % 4;  
wIdx = ( wIdx + X\[ roundIdx ]\[ 1 ] ) & 0xF;  
}  
}  
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) );  
for ( i = 0; i &lt; 16; i++ )  
printf ( "%02x", r\[i] );  
putchar ( '\\n' );  
}  
return EXIT\_SUCCESS;  
}