Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

MD5 hash function - C Code Bank


MD5 hash function
Works only with 55 bytes of data(string no bigger then 55 characters), it only prints out the hash
                
/* reduce typing */
typedef unsigned int INT ;


/* results of a sinus function, this is constant in every md5 hash process */
unsigned int t[] = {
0xd76aa478L, /* 1 */
0xe8c7b756L, /* 2 */
0x242070dbL, /* 3 */
0xc1bdceeeL, /* 4 */
0xf57c0fafL, /* 5 */
0x4787c62aL, /* 6 */
0xa8304613L, /* 7 */
0xfd469501L, /* 8 */
0x698098d8L, /* 9 */
0x8b44f7afL, /* 10 */
0xffff5bb1L, /* 11 */
0x895cd7beL, /* 12 */
0x6b901122L, /* 13 */
0xfd987193L, /* 14 */
0xa679438eL, /* 15 */
0x49b40821L, /* 16 */
0xf61e2562L, /* 17 */
0xc040b340L, /* 18 */
0x265e5a51L, /* 19 */
0xe9b6c7aaL, /* 20 */
0xd62f105dL, /* 21 */
0x02441453L, /* 22 */
0xd8a1e681L, /* 23 */
0xe7d3fbc8L, /* 24 */
0x21e1cde6L, /* 25 */
0xc33707d6L, /* 26 */
0xf4d50d87L, /* 27 */
0x455a14edL, /* 28 */
0xa9e3e905L, /* 29 */
0xfcefa3f8L, /* 30 */
0x676f02d9L, /* 31 */
0x8d2a4c8aL, /* 32 */
0xfffa3942L, /* 33 */
0x8771f681L, /* 34 */
0x6d9d6122L, /* 35 */
0xfde5380cL, /* 36 */
0xa4beea44L, /* 37 */
0x4bdecfa9L, /* 38 */
0xf6bb4b60L, /* 39 */
0xbebfbc70L, /* 40 */
0x289b7ec6L, /* 41 */
0xeaa127faL, /* 42 */
0xd4ef3085L, /* 43 */
0x04881d05L, /* 44 */
0xd9d4d039L, /* 45 */
0xe6db99e5L, /* 46 */
0x1fa27cf8L, /* 47 */
0xc4ac5665L, /* 48 */
0xf4292244L, /* 49 */
0x432aff97L, /* 50 */
0xab9423a7L, /* 51 */
0xfc93a039L, /* 52 */
0x655b59c3L, /* 53 */
0x8f0ccc92L, /* 54 */
0xffeff47dL, /* 55 */
0x85845dd1L, /* 56 */
0x6fa87e4fL, /* 57 */
0xfe2ce6e0L, /* 58 */
0xa3014314L, /* 59 */
0x4e0811a1L, /* 60 */
0xf7537e82L, /* 61 */
0xbd3af235L, /* 62 */
0x2ad7d2bbL, /* 63 */
0xeb86d391L  /* 64 */
};
/* another constant thing in md5 hash algoritham */
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));
}

void md5(unsigned char *input){

	/* md5 works with block of 512 bits or 64 bytes so if a message is 150 bytes the algorithm should break it into 64+64+22 block and append at the
	last block a bite with value 1 and the length of the message in bites in this case 150*8 */

	/* this algorithm assumes that the input wont be bigger then  55 bytes, so in the end we will have only one block of 64 bytes (after appending 
		all the data(bite 1 and the length))*/ 

	unsigned char output[16],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<55;i++){

		buff[i]=input[i];
	}

	/* appending a bite with value of 1 and length of the string at the and of the block */

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

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

		buff[i+56]=len >> (i*8);
	}

	/* dividing 512 block into 16x4 byte blocks e.g. int */ 

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

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

	}

	/* another static value in md5 */

	stat[0]=0x67452301;
	stat[1]=0xefcdab89;
	stat[2]=0x98badcfe;
	stat[3]=0x10325476;

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

	/* md5 hash functions */

	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];

	/* appending the result into one string */

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

		output[j]=(unsigned char)( stat[i]);
		output[j+1]=(unsigned char)( stat[i]>>8);
		output[j+2]=(unsigned char)( stat[i]>>16);
		output[j+3]=(unsigned char)(stat[i]>>24);
	}
	/* printing hex value of the result */
	for(j=0;j<16;j++){
		printf("%02x",output[j]);
	}

return ;
}
            
Comments
Sorry but there are no comments to display