Click here to Skip to main content
15,909,437 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
void CMD5Checksum::Update( BYTE* Input,	ULONG nInputLen )
{
	//Compute number of bytes mod 64
	UINT nIndex = (UINT)((m_nCount[0] >> 3) & 0x3F); //this line is confusing to me

	//Update number of bits
	if ( ( m_nCount[0] += nInputLen << 3 ) < ( nInputLen << 3) ) //this one too
	{
		m_nCount[1]++;
	}

        //why do we add only the first 3 msb bits on nInputLen?
	m_nCount[1] += (nInputLen >> 29); 

	//Transform as many times as possible.
	UINT i=0;		
	UINT nPartLen = 64 - nIndex; //what is nPartLen
	if (nInputLen >= nPartLen) 	
	{
		memcpy( &m_lpszBuffer[nIndex], Input, nPartLen );
		Transform( m_lpszBuffer );
		for (i = nPartLen; i + 63 < nInputLen; i += 64) 
		{
			Transform( &Input[i] );
		}
		nIndex = 0;
	} 
	else 
	{
		i = 0;
	}
	// Buffer remaining input
	memcpy( &m_lpszBuffer[nIndex], &Input[i], nInputLen-i);
}




ULONG m_nCount[2];   //number of bits, modulo 2^64 (lsb first)


This is for an MD5 algorithm. Can someone explain to me some of the lines above?
Posted
Comments
Mehdi Gholam 4-Sep-11 0:56am    
Do you want to know what the lines do or what MD5 is?
kittydas 4-Sep-11 1:06am    
What the lines do...
I read about MD5 already. But, I don't understand the lines. I am trying to understand the MD5 more by an example of code, but I am finding it a bit hard.

C++
UINT  // unsigned int32 variable
nIndex = // variable name
(UINT) // cast the result to unsigned int32
(
(
m_nCount[0] // take the zero'th element of the array
>> 3 // shift right 3 places = 2^3 = 8 or divide by 8 
) 
& 0x3F // logical AND the results with 0x3f which is 0011 1111 in bits 
);


C++
if ( ( // logical if operation
m_nCount[0] += // take the zero'th element of the array and add the following to it
nInputLen << 3 // take the variable and shift left 3 places = 2^3 = 8 or multiply by 8
)
 
Share this answer
 
Comments
kittydas 4-Sep-11 2:31am    
How about m_nCount[1] += (nInputLen >> 29); ?
Mehdi Gholam 4-Sep-11 2:45am    
if nInputLen is 32bits then >>29 will take the top 3 bits and add them to the first element in m_nCount (top 3 bits will be between 0 and 7 when shifted)
kittydas 4-Sep-11 2:47am    
ok, my question is why the top 3 bits?
Mehdi Gholam 4-Sep-11 2:58am    
That is how the algorithm is implemented.
kittydas 4-Sep-11 6:50am    
Ok, then it must be that my MD5 algorithm knowledge is still lacking. Thanks! I will read more about it, and get back if I have more questions.
//why do we add only the first 3 msb bits on nInputLen?

The answer given to you on MSDN Forum is absolutely correct - "It seems that m_nCount[0] and m_nCount[1] together form a 64-bit counter of bits, split into two 32-bit halves."

The MD5 algorithm exists since 1992. The long was 32-bits.
And here you see the technique to handle number of bits greater than 2^32:
1. They create two 32-bits counters - m_nCount[0] and m_nCount[1];
2. When m_nCount[0] makes nInputLen << 3 it can result in overflow with highest bits lost;
3. Thus, the second counter m_nCount[1] accumulates these bits in
m_nCount[1] += (nInputLen >> 29);
4. Together these two 32-bits counters produce the resulting 64-bits value.

Imagine, for example, in decimal numbers m_nCount[0] - calculating tens and ones, and m_nCount[1] - calculating hundreds. Then, concatenated this two numbers will produce the resulting value.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900