📄 vmmd5checksum.cpp
字号:
GG( a, b, c, d, X[ 9 ], VM_MD5_S21, VM_MD5_T25 );
GG( d, a, b, c, X[ 14 ], VM_MD5_S22, VM_MD5_T26 );
GG( c, d, a, b, X[ 3 ], VM_MD5_S23, VM_MD5_T27 );
GG( b, c, d, a, X[ 8 ], VM_MD5_S24, VM_MD5_T28 );
GG( a, b, c, d, X[ 13 ], VM_MD5_S21, VM_MD5_T29 );
GG( d, a, b, c, X[ 2 ], VM_MD5_S22, VM_MD5_T30 );
GG( c, d, a, b, X[ 7 ], VM_MD5_S23, VM_MD5_T31 );
GG( b, c, d, a, X[ 12 ], VM_MD5_S24, VM_MD5_T32 );
// Perform Round 3 of the transformation
//
HH( a, b, c, d, X[ 5 ], VM_MD5_S31, VM_MD5_T33 );
HH( d, a, b, c, X[ 8 ], VM_MD5_S32, VM_MD5_T34 );
HH( c, d, a, b, X[ 11 ], VM_MD5_S33, VM_MD5_T35 );
HH( b, c, d, a, X[ 14 ], VM_MD5_S34, VM_MD5_T36 );
HH( a, b, c, d, X[ 1 ], VM_MD5_S31, VM_MD5_T37 );
HH( d, a, b, c, X[ 4 ], VM_MD5_S32, VM_MD5_T38 );
HH( c, d, a, b, X[ 7 ], VM_MD5_S33, VM_MD5_T39 );
HH( b, c, d, a, X[ 10 ], VM_MD5_S34, VM_MD5_T40 );
HH( a, b, c, d, X[ 13 ], VM_MD5_S31, VM_MD5_T41 );
HH( d, a, b, c, X[ 0 ], VM_MD5_S32, VM_MD5_T42 );
HH( c, d, a, b, X[ 3 ], VM_MD5_S33, VM_MD5_T43 );
HH( b, c, d, a, X[ 6 ], VM_MD5_S34, VM_MD5_T44 );
HH( a, b, c, d, X[ 9 ], VM_MD5_S31, VM_MD5_T45 );
HH( d, a, b, c, X[ 12 ], VM_MD5_S32, VM_MD5_T46 );
HH( c, d, a, b, X[ 15 ], VM_MD5_S33, VM_MD5_T47 );
HH( b, c, d, a, X[ 2 ], VM_MD5_S34, VM_MD5_T48 );
// Perform Round 4 of the transformation
//
II( a, b, c, d, X[ 0 ], VM_MD5_S41, VM_MD5_T49 );
II( d, a, b, c, X[ 7 ], VM_MD5_S42, VM_MD5_T50 );
II( c, d, a, b, X[ 14 ], VM_MD5_S43, VM_MD5_T51 );
II( b, c, d, a, X[ 5 ], VM_MD5_S44, VM_MD5_T52 );
II( a, b, c, d, X[ 12 ], VM_MD5_S41, VM_MD5_T53 );
II( d, a, b, c, X[ 3 ], VM_MD5_S42, VM_MD5_T54 );
II( c, d, a, b, X[ 10 ], VM_MD5_S43, VM_MD5_T55 );
II( b, c, d, a, X[ 1 ], VM_MD5_S44, VM_MD5_T56 );
II( a, b, c, d, X[ 8 ], VM_MD5_S41, VM_MD5_T57 );
II( d, a, b, c, X[ 15 ], VM_MD5_S42, VM_MD5_T58 );
II( c, d, a, b, X[ 6 ], VM_MD5_S43, VM_MD5_T59 );
II( b, c, d, a, X[ 13 ], VM_MD5_S44, VM_MD5_T60 );
II( a, b, c, d, X[ 4 ], VM_MD5_S41, VM_MD5_T61 );
II( d, a, b, c, X[ 11 ], VM_MD5_S42, VM_MD5_T62 );
II( c, d, a, b, X[ 2 ], VM_MD5_S43, VM_MD5_T63 );
II( b, c, d, a, X[ 9 ], VM_MD5_S44, VM_MD5_T64 );
// add the transformed values to the current checksum
//
m_aulMD5CheckSum[ 0 ] += a;
m_aulMD5CheckSum[ 1 ] += b;
m_aulMD5CheckSum[ 2 ] += c;
m_aulMD5CheckSum[ 3 ] += d;
}
/* End of function "VMMD5Checksum::Transform"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMMD5Checksum::VMMD5Checksum
DESCRIPTION: ctor. inits this
INPUT: none
OUTPUT: none
RETURNS: none
*/
VMMD5Checksum::VMMD5Checksum( void )
{
// zero members
//
memset( m_achBuffer, 0, 64 );
m_ulBitCount[ 0 ] = m_ulBitCount[ 1 ] = 0;
// Load magic state initialization constants
//
m_aulMD5CheckSum[ 0 ] = VM_MD5_INIT_STATE_0;
m_aulMD5CheckSum[ 1 ] = VM_MD5_INIT_STATE_1;
m_aulMD5CheckSum[ 2 ] = VM_MD5_INIT_STATE_2;
m_aulMD5CheckSum[ 3 ] = VM_MD5_INIT_STATE_3;
}
/* End of function "VMMD5Checksum::VMMD5Checksum"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMMD5Checksum::CharToDWord
DESCRIPTION: Transfers the data in an 8 bit array to a 32 bit array.
Four BYTES from the input array are transferred to each DWORD entry
of the output array. The first BYTE is transferred to the bits (0-7)
of the output DWORD, the second BYTE to bits 8-15 etc.
The algorithm assumes that the input array is a multiple of 4 bytes long
so that there is a perfect fit into the array of 32 bit words.
INPUT: pdwOuput : pointer to output buffer
pchInput : pointer to input buffer.
iLength : the length of the input buffer
OUTPUT: pdwOutput : contents are modified
RETURNS: void
*/
void VMMD5Checksum::CharToDWord( DWORD* pdwOutput, char* pchInput, unsigned int iLength )
{
assert( iLength %4 == 0 );
// initialisations
//
unsigned int i = 0; //index to Output array
unsigned int j = 0; //index to Input array
// transfer the data by shifting and copying
//
for ( ; j < iLength; i++, j += 4)
{
pdwOutput[ i ] = (unsigned long)pchInput[ j ] |
(unsigned long)pchInput[ j + 1 ] << 8 |
(unsigned long)pchInput[ j + 2 ] << 16 |
(unsigned long)pchInput[ j + 3 ] << 24;
}
}
/* End of function "VMMD5Checksum::CharToDWord"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMMD5Checksum::DWordToChar
DESCRIPTION: translates a hex-encoded string into the original byte
array value represented by the hex string
INPUT: pchOuput : pointer to output buffer.
pdwInput : pointer to the 32 bits of source data
iLength : the length the input buffer
OUTPUT: pchOutput : contents are modified
RETURNS: void
*/
void VMMD5Checksum::DWordToChar( char* pchOutput, DWORD* pdwInput, unsigned int iLength )
{
//transfer the data by shifting and copying
//
unsigned int i = 0;
unsigned int j = 0;
for ( ; j < iLength; i++, j += 4 )
{
pchOutput[ j ] = (UCHAR)( pdwInput[ i ] & 0xff );
pchOutput[ j + 1 ] = (UCHAR)(( pdwInput[ i ] >> 8 ) & 0xff );
pchOutput[ j + 2 ] = (UCHAR)(( pdwInput[ i ] >> 16 ) & 0xff );
pchOutput[ j + 3 ] = (UCHAR)(( pdwInput[ i ] >> 24 ) & 0xff );
}
}
/* End of function "VMMD5Checksum::DWordToChar"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMMD5Checksum::Final
DESCRIPTION: Implementation of main MD5 checksum algorithm; ends the checksum
calculation. Performs the final MD5 checksum calculation ('Update'
does most of the work, this function just finishes the calculation.)
INPUT: void
OUTPUT: none
RETURNS: VMString : the final hexadecimal MD5 checksum result
*/
VMString VMMD5Checksum::Final( void )
{
// Save number of bits
//
char achBits[ 8 ];
DWordToChar( achBits, m_ulBitCount, 8 );
// Pad out to 56 mod 64.
//
unsigned int iIndex = (unsigned int)( ( m_ulBitCount[ 0 ] >> 3 ) & 0x3f );
unsigned int iPadLength = ( iIndex < 56 ) ? ( 56 - iIndex ) : ( 120 - iIndex );
Update( (char*)VM_PADDING, iPadLength );
//Append length (before padding)
//
Update( achBits, 8 );
// Store final state in 'lpszMD5'
//
const int iMD5Size = 16;
unsigned char achMD5[ iMD5Size ];
DWordToChar( (char*)achMD5, m_aulMD5CheckSum, iMD5Size );
//Convert the hexadecimal checksum to a VMString
//
VMString oResult;
for ( int iLoop = 0; iLoop < iMD5Size; iLoop++ )
{
VMString oTemp;
if ( achMD5[ iLoop ] == 0 )
{
oTemp = VMString( "00" );
}
else
if ( achMD5[ iLoop ] <= 15 )
{
oTemp.Format( "0%x", achMD5[ iLoop ] );
}
else
{
oTemp.Format( "%x", achMD5[ iLoop ] );
}
assert( oTemp.GetLength() == 2 );
oResult += oTemp;
}
assert( oResult.GetLength() == 32 );
return( oResult );
}
/* End of function "VMMD5Checksum::Final"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMMD5Checksum::Update
DESCRIPTION: Implementation of the main MD5 check sum algorithm
INPUT: pchInput : pointer to input block
iInputLen : length of input block
OUTPUT:
RETURNS: void
*/
void VMMD5Checksum::Update( char* pchInput, ULONG iInputLength )
{
//Compute number of bytes mod 64
//
unsigned int iIndex = (unsigned int)( ( m_ulBitCount[ 0 ] >> 3 ) & 0x3F );
//Update number of bits
//
if ( ( m_ulBitCount[ 0 ] += iInputLength << 3 ) < ( iInputLength << 3 ) )
{
m_ulBitCount[ 1 ]++;
}
m_ulBitCount[ 1 ] += ( iInputLength >> 29 );
//Transform as many times as possible.
//
unsigned int iLoop = 0;
unsigned int iPartLength = 64 - iIndex;
if ( iInputLength >= iPartLength )
{
memcpy( &m_achBuffer[ iIndex ], pchInput, iPartLength );
Transform( m_achBuffer );
for ( iLoop = iPartLength; iLoop + 63 < iInputLength; iLoop += 64 )
{
Transform( &pchInput[ iLoop ] );
}
iIndex = 0;
}
else
{
iLoop = 0;
}
// Buffer remaining input
//
memcpy( &m_achBuffer[ iIndex ], &pchInput[ iLoop ], iInputLength - iLoop );
}
/* End of function "VMMD5Checksum::Update"
/*****************************************************************************/
/*****************************************************************************/
/* Check-in history */
/*
*$Log: $
*/
/*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -