32 bit source array
UINT nLength : the number of 8 bit data items in the source array
NOTES: One DWORD from the input array is transferred into four BYTES
in the output array. The first (0-7) bits of the first DWORD are
transferred to the first output BYTE, bits bits 8-15 are transferred from
the second BYTE etc.
The algorithm assumes that the output array is a multiple of 4 bytes long
so that there is a perfect fit of 8 bit BYTES into the 32 bit DWORDs.
*****************************************************************************************/
void CMD5Checksum::DWordToByte(BYTE* Output, DWORD* Input, UINT nLength )
{
//entry invariants
ASSERT( nLength % 4 == 0 );
ASSERT( AfxIsValidAddress(Output, nLength, TRUE) );
ASSERT( AfxIsValidAddress(Input, nLength/4, FALSE) );
//transfer the data by shifting and copying
UINT i = 0;
UINT j = 0;
for ( ; j < nLength; i++, j += 4)
{
Output[j] = (UCHAR)(Input[i] & 0xff);
Output[j+1] = (UCHAR)((Input[i] >> 8) & 0xff);
Output[j+2] = (UCHAR)((Input[i] >> 16) & 0xff);
Output[j+3] = (UCHAR)((Input[i] >> 24) & 0xff);
}
}
/*****************************************************************************************
FUNCTION: CMD5Checksum::Final
DETAILS: protected
DESCRIPTION: Implementation of main MD5 checksum algorithm; ends the checksum calculation.
RETURNS: CString : the final hexadecimal MD5 checksum result
ARGUMENTS: None
NOTES: Performs the final MD5 checksum calculation (''''Update'''' does most of the work,
this function just finishes the calculation.)
*****************************************************************************************/
CString CMD5Checksum::Final()
{
//Save number of bits
BYTE Bits;
DWordToByte( Bits, m_nCount, 8 );
//Pad out to 56 mod 64.
UINT nIndex = (UINT)((m_nCount[0] >> 3) & 0x3f);
UINT nPadLen = (nIndex < 56) ? (56 - nIndex) : (120 - nIndex);
Update( PADDING, nPadLen );
//Append length (before padding)
Update( Bits, 8 );
//Store final state in ''''lpszMD5''''
const int nMD5Size = 16;
unsigned char lpszMD5[ nMD5Size ];
DWordToByte( lpszMD5, m_lMD5, nMD5Size );
//Convert the hexadecimal checksum to a CString
CString strMD5;
for ( int i=0; i < nMD5Size; i++)
{
CString Str;
if (lpszMD5[i] == 0) {
Str = CString("00");
}
else if (lpszMD5[i] <= 15) {
Str.Format("0%x",lpszMD5[i]);
}
else {
Str.Format("%x",lpszMD5[i]);
}
ASSERT( Str.GetLength() == 2 );
strMD5 += Str;
}
ASSERT( strMD5.GetLength() == 32 );
return strMD5;
}
/*******