MD5算法代码帮助 [英] MD5 algorithm code help

查看:66
本文介绍了MD5算法代码帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)



这是针对MD5算法的.有人可以向我解释以上几行吗?



This is for an MD5 algorithm. Can someone explain to me some of the lines above?

推荐答案

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





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
)


//为什么我们只在nInputLen上添加前3 msb位?

在MSDN论坛上为您提供的 answer 绝对正确-看来m_nCount [0]和m_nCount [1]一起形成了一个64位的位计数器,分为两个32位的一半."

MD5算法自1992年以来就存在.long为32位.
在这里,您将看到处理大于2 ^ 32的位数的技术:
1.他们创建两个32位计数器-m_nCount [0]和m_nCount [1];
2.当m_nCount [0]使nInputLen <. 3它会导致最高位丢失的溢出;
3.因此,第二个计数器m_nCount [1]将这些位累加到
m_nCount [1] + =(nInputLen>> 29);
4.这两个32位计数器一起产生结果64位值.

想象一下,例如,十进制数m_nCount [0]-计算十进制和一,而m_nCount [1]-计算数百个.然后,将这两个数字串联起来将产生结果值.
//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.


这篇关于MD5算法代码帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆