Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Topics

Recent Status Updates

View All Updates

Photo
- - - - -

MD5 algoritham

md5

  • Please log in to reply
14 replies to this topic

#1 ferovac

ferovac

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 83 posts

Posted 04 August 2010 - 12:02 AM

So im trying to program a md5 hash algoritham in C in as simple way as possible , but i just cant get the right result, so if anyone has the time and the pacience to go thrugh my code.

the algoritham is working but i just can seem to get the right md5 sum

since im simplyfying it, the algorithm is only taking 64byte of data and returning a pointer to hash


typedef unsigned int INT ;

unsigned int t[] = {
0xd76aa478L, /* 1 */
.
.
.
.
the sinus results copied from the net
.
.
.
0xbd3af235L, /* 62 */
0x2ad7d2bbL, /* 63 */
0xeb86d391L  /* 64 */
};

unsigned int r[] = 	{   7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,  
			    5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,   
			    4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,   
			    6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21   
			};
unsigned int leftrotate(INT x,INT c){

	return (x << c) | (x >> (32-c));
}

unsigned char * md5(unsigned char *input){

	unsigned char  buff[64];

	INT len=strlen((char *)input),w[16],stat[4],f,a,b,c,d,g,i,j,temp;

	bzero((char *)buff,sizeof(buff));

	for(i=0;i<len && i<59;i++){

		buff[i]=input[i];
	}

	if(len<59){ buff[len]=0x80;  len*=8;}
	else { buff[59]=0x80; len=59*8; }

	for(i=0;i<4;i++){

		buff[i+60]=len >> (24 - i*8);
	}

	for(i=0,j=0;i<16;i++,j+=4){
		

		w[i]=0;
		w[i]=(((INT)buff[j]<< 24) | ((INT)buff[j+1]<<16) | ((INT)buff[j+2] << 8) | ((INT)buff[j+3]));

	}

	stat[0]=0x01234567;
	stat[1]=0x89abcdef;
	stat[2]=0xfedcba98;
	stat[3]=0x76543210;


	a=stat[0];
	b=stat[1];
	c=stat[2];
	d=stat[3];

	for(i=0;i<64;i++){

		if (0 <= i && i<= 15 ){
			f = (b & c) | ((~ b) & d);
			g = i;
		}else if (16 <= i && i<= 31){
			f = (d & b) | ((~ d) & c);
			g = (5*i + 1) % 16;
		}else if (32 <= i && i<= 47){
			f = b ^ c ^ d;
			g = (3*i + 5) % 16;
		}else if (48 <= i && i<= 63){
			f = c ^ (b | (~ d));
			g = (7*i) % 16;
		}

		temp = d;
		   d = c;
		   c = b;
		   b = b + leftrotate((a + f + t[i] + w[g]) , r[i]);
		   a = temp;

	}

	stat[0]=a + stat[0];
	stat[1]=b + stat[1];
	stat[2]=c + stat[2];
	stat[3]=d + stat[3];

	
	for(i=0;i<4;i++){

		printf("%02x",stat[i]);

	}

return  input;
}

thanks

Edited by ferovac, 04 August 2010 - 10:57 AM.

  • 0

#2 Beeko

Beeko

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 105 posts

Posted 04 August 2010 - 03:30 AM

Ummm sory I cant be helpful, I'm just nosy, never worked with C before, but can you tell me what exactly is MD5?
  • 0

#3 ferovac

ferovac

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 83 posts

Posted 04 August 2010 - 10:55 AM

well best explanations are already on the internet all you need is to look

wiki

nice little explanation

for more info
  • 0

#4 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 04 August 2010 - 10:58 AM

MD5 is a hash algorithm (replying to beeko)
  • 0

#5 Beeko

Beeko

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 105 posts

Posted 04 August 2010 - 11:15 AM

Duno wt hash algorithm is mebob :D Tx though xD
Tx alot ferovac xD
  • 0

#6 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 04 August 2010 - 12:22 PM

A hash algorithm is a one way function used in cryptography. For example, when you create an account with a website, it applies a hash algorithm on your password, and stores it on the server, and when you log in, it hashes the password you try to log in with, and compares it to the hash on the server. Pretty much, a hash algorithm is impossible to reverse (although you can brute force it).
  • 0

#7 Beeko

Beeko

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 105 posts

Posted 04 August 2010 - 12:47 PM

basically creating a dictionary for the website to use for encrypting Passwords? as a Website's own language?
  • 0

#8 ferovac

ferovac

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 83 posts

Posted 04 August 2010 - 01:36 PM

No, it doesnt create any dictionary it encrypts a string or any type of a file, basicly a bunch of bits into a 128 bit string, webpage passwords are just a small area where md5 is used, well at leaste for now, because computers are getting faster and faster and there are tones of already decrypted md5 hashes out there,so it was declared unsafe and its not recommended for use in any area, sha-512 is the new safe algorithm to use, at leaste for now
  • 0

#9 Beeko

Beeko

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 105 posts

Posted 04 August 2010 - 01:37 PM

so is a solve for this is what the Author of the post doing? making his own Hash Algorithm?
  • 0

#10 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 04 August 2010 - 02:53 PM

I created a modified version of MD5 a while back on here. It's the same algorithm, but operating on fewer bits.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#11 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 04 August 2010 - 03:18 PM

No, he is just making his own implementation of a hash algorithm that already exists, MD5. He is probably just doing this for the experience of doing it.
  • 0

#12 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 04 August 2010 - 05:22 PM

He can use mine as a comparison. It's basically the same logic.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/






Also tagged with one or more of these keywords: md5

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download