Sunday, May 1, 2011

MD5 algorithm

You can get the information regarding MD5 algorithm on Wikipedia.
Follow http://en.wikipedia.org/wiki/MD5

MD5 implementation:
Code Language : C++
System : Linux
----------------------------------------------------------------------------------------------------------------------------


/*Implement MD5 algorithm*/

#include "iostream"
#include "string.h"
#include "math.h"
#include "stdlib.h"


using namespace std;

char * ip=new char[512];
unsigned long * ipl;
int len=0;
unsigned long aa=0x01234567;
unsigned long bb=0x89abcdef;
unsigned long cc=0x76543210;
unsigned long dd=0xfedcba98;

unsigned long k[64];

unsigned long LittleEndian(unsigned long n)
{
n=(n>>24)|  //move first byte to last
 ((n<<8)&0x00ff0000)  //move 3rd to 2nd
 |((n>>8)&0x0000ff00) //move 2nd to 3rd
 |(n<<24);   //move last to first
return n;
}
unsigned long leftrotate(unsigned long a,int n)
{
a=((a>>8)&0x0fffffff)|((a<<24)&0xf0000000);
return a;
}
int main()
{
system("clear");
/*Set sine constant*/
for(int i=0;i<64;i++)
k[i]=((unsigned long)floor(abs(sin(i+1))*2^32))&0xffffffff;//sin() returns 'double' so this provision


for(int i=0;i<512;i++)
ip[i]=NULL;
//Enter data
cout<<"\nEnter the String :: ";
cin>>ip;
//get length of inpt string
len=strlen(ip);

/*Step 1: Padding*/
//check if the length is multiple of 512
int addl=0,l=(len+1);//1 word is reserved for padding
while(l%16!=14)
{
l++;
addl++;//these many hex 0 should be added
}
l=l+2;
cout<<"l="<<l;
//convert char to long
ipl=new unsigned long[l];
for(int i=0;i<len;i++)
ipl[i]=(unsigned long)ip[i];
//padding
ipl[len]=0x80000000;
len++;
//add 0 till it becomes i%512=448
for(int i=len;i<l-2;i++) //-2 means 64 bit short
ipl[i]=0;


/*Step 2: Adding*/
//add last 64 bits
ipl[l-2]=ipl[0];
ipl[l-1]=ipl[1];

/*takes 512 bits out of them*/
int n=l/16;//16*32=512 bits
int st=0,ed=16;
while(n!=0)
{
unsigned long* ihex=new unsigned long[16];
int j=0;
for(int i=st;i<ed;i++)
ihex[j++]=ipl[i];
cout<<"The hex input : ";
for(int i=0;i<16;i++)
cout<<" "<<hex<<ihex[i];//this is how we show hex values in C++

/*Step 3: Convert to Little Endian Format*/
/*Little Endian : In little endian, you store the least significant byte in the smallest address*/
cout<<"\n\nThe hex input in Little Endian Format: ";
for(int i=0;i<16;i++)
{
ihex[i]=LittleEndian(ihex[i]);
cout<<" "<<hex<<ihex[i];
}


unsigned long a=aa;
unsigned long b=bb;
unsigned long c=cc;
unsigned long d=dd;
int index=0;
unsigned long f;
/*Step 4: Actual Processing*/
cout<<"\n\n\nOutput :: ";
for(int i=0;i<64;i++)
{
if(i>=0&&i<=15)
{
f=(b & c) | ((~b) & d);
index=i;
}
if(i>=16&&i<=31)
{
f=(b & c) | (c & ~d);
index=(5*i+1)%16;
}
if(i>=32&&i<=47)
{
f=b^c^d;
index=(3*i+1)%16;
}
if(i>=48&&i<=63)
{
f=c ^ (b | ~d);
index=(7*i+1)%16;
}
unsigned long temp;
temp=d;
d=c;
c=b;
b=b+leftrotate((f+a+k[i]+ihex[index]),8);
a=temp;
}
aa=a+aa;
bb=b+bb;
cc=c+cc;
dd=d+dd;

cout<<"  "<<aa<<" "<<bb<<" "<<cc<<" "<<dd;

st=ed;
ed=ed+16;
n--;
}



cout<<"\n";
return 0;


}

----------------------------------------------------------------
----------------------------------------------------------------
Executing the code:
:~$     g++   md5.cpp
:~$   ./a.out





Enter the String :: chakresh
l=16The hex input :  63 68 61 6b 72 65 73 68 80000000 0 0 0 0 0 63 68

The hex input in Little Endian Format:  63000000 68000000 61000000 6b000000 72000000 65000000 73000000 68000000 80 0 0 0 0 0 63000000 68000000


Output ::   c8cb3aac a27325e8 1e9525d1 26b4053a

No comments:

Post a Comment