⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 md5.cpp

📁 md5算法的另外一种实现
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "stdafx.h"
#include "md5.h"
/*****************************************************************************************
FUNCTION:  CMD5Checksum::GetMD5
DETAILS:  static, public
DESCRIPTION: Gets the MD5 checksum for a specified file
RETURNS:  CString : the hexadecimal MD5 checksum for the specified file
ARGUMENTS:  CString& strFilePath : the full pathname of the specified file
NOTES:   Provides an interface to the CMD5Checksum class. 'strFilePath' name should
hold the full pathname of the file, eg C:\My Documents\Arcticle.txt.
NB. If any problems occur with opening or reading this file, a CFileException
will be thrown; callers of this function should be ready to catch this
exception.
*****************************************************************************************/
CString CMD5Checksum::GetMD5(const CString& strFilePath)
{
	//open the file as a binary file in readonly mode, denying write access
	CFile File(strFilePath, CFile::shareDenyNone);
	//the file has been successfully opened, so now get and return its checksum
	return GetMD5(File);
}


/*****************************************************************************************
FUNCTION:  CMD5Checksum::GetMD5
DETAILS:  static, public
DESCRIPTION: Gets the MD5 checksum for a specified file
RETURNS:  CString : the hexadecimal MD5 checksum for the specified file
ARGUMENTS:  CFile& File : the specified file
NOTES:   Provides an interface to the CMD5Checksum class. 'File' should be open in
binary readonly mode before calling this function.
NB. Callers of this function should be ready to catch any CFileException
thrown by the CFile functions
*****************************************************************************************/
CString CMD5Checksum::GetMD5(CFile& File)
{
	try
	{
		CMD5Checksum MD5Checksum;  //checksum object 
		int nLength = 0;    //number of bytes read from the file
		const int nBufferSize = 1024; //checksum the file in blocks of 1024 bytes
		BYTE Buffer[nBufferSize];  //buffer for data read from the file

		//checksum the file in blocks of 1024 bytes
		while ((nLength = File.Read( Buffer, nBufferSize )) > 0 )
		{
			MD5Checksum.Update( Buffer, nLength );
		}

		//finalise the checksum and return it
		return MD5Checksum.Final();
	}

	//report any file exceptions in debug mode only
	catch (CFileException* e )
	{
		TRACE0("CMD5Checksum::GetMD5: CFileException caught"); 
		throw e;
	}
}


/*****************************************************************************************
FUNCTION:  CMD5Checksum::GetMD5
DETAILS:  static, public
DESCRIPTION: Gets the MD5 checksum for data in a BYTE array
RETURNS:  CString : the hexadecimal MD5 checksum for the specified data
ARGUMENTS:  BYTE* pBuf  : pointer to the BYTE array
UINT nLength : number of BYTEs of data to be checksumed
NOTES:   Provides an interface to the CMD5Checksum class. Any data that can
be cast to a BYTE array of known length can be checksummed by this
function. Typically, CString and char arrays will be checksumed,
although this function can be used to check the integrity of any BYTE array.
A buffer of zero length can be checksummed; all buffers of zero length
will return the same checksum.
*****************************************************************************************/
CString CMD5Checksum::GetMD5(BYTE* pBuf, UINT nLength)
{
	//entry invariants
	AfxIsValidAddress(pBuf,nLength,FALSE);

	//calculate and return the checksum
	CMD5Checksum MD5Checksum;
	MD5Checksum.Update( pBuf, nLength );
	return MD5Checksum.Final();
}


/*****************************************************************************************
FUNCTION:  CMD5Checksum::RotateLeft
DETAILS:  private
DESCRIPTION: Rotates the bits in a 32 bit DWORD left by a specified amount
RETURNS:  The rotated DWORD
ARGUMENTS:  DWORD x : the value to be rotated
int n   : the number of bits to rotate by
*****************************************************************************************/
DWORD CMD5Checksum::RotateLeft(DWORD x, int n)
{
	//check that DWORD is 4 bytes long - true in Visual C++ 6 and 32 bit Windows
	ASSERT( sizeof(x) == 4 );

	//rotate and return x
	return (x << n) | (x >> (32-n));
}


/*****************************************************************************************
FUNCTION:  CMD5Checksum::FF
DETAILS:  protected
DESCRIPTION: Implementation of basic MD5 transformation algorithm
RETURNS:  none
ARGUMENTS:  DWORD &A, B, C, D : Current (partial) checksum
DWORD X           : Input data
DWORD S     : MD5_SXX Transformation constant
DWORD T     : MD5_TXX Transformation constant
NOTES:   None
*****************************************************************************************/
void CMD5Checksum::FF( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)
{
	DWORD F = (B & C) | (~B & D);
	A += F + X + T;
	A = RotateLeft(A, S);
	A += B;
}


/*****************************************************************************************
FUNCTION:  CMD5Checksum::GG
DETAILS:  protected
DESCRIPTION: Implementation of basic MD5 transformation algorithm
RETURNS:  none
ARGUMENTS:  DWORD &A, B, C, D : Current (partial) checksum
DWORD X           : Input data
DWORD S     : MD5_SXX Transformation constant
DWORD T     : MD5_TXX Transformation constant
NOTES:   None
*****************************************************************************************/
void CMD5Checksum::GG( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)
{
	DWORD G = (B & D) | (C & ~D);
	A += G + X + T;
	A = RotateLeft(A, S);
	A += B;
}


/*****************************************************************************************
FUNCTION:  CMD5Checksum::HH
DETAILS:  protected
DESCRIPTION: Implementation of basic MD5 transformation algorithm
RETURNS:  none
ARGUMENTS:  DWORD &A, B, C, D : Current (partial) checksum
DWORD X           : Input data
DWORD S     : MD5_SXX Transformation constant
DWORD T     : MD5_TXX Transformation constant
NOTES:   None
*****************************************************************************************/
void CMD5Checksum::HH( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)
{
	DWORD H = (B ^ C ^ D);
	A += H + X + T;
	A = RotateLeft(A, S);
	A += B;
}


/*****************************************************************************************
FUNCTION:  CMD5Checksum::II
DETAILS:  protected
DESCRIPTION: Implementation of basic MD5 transformation algorithm
RETURNS:  none
ARGUMENTS:  DWORD &A, B, C, D : Current (partial) checksum
DWORD X           : Input data
DWORD S     : MD5_SXX Transformation constant
DWORD T     : MD5_TXX Transformation constant
NOTES:   None
*****************************************************************************************/
void CMD5Checksum::II( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)
{
	DWORD I = (C ^ (B | ~D));
	A += I + X + T;
	A = RotateLeft(A, S);
	A += B;
}


/*****************************************************************************************
FUNCTION:  CMD5Checksum::ByteToDWord
DETAILS:  private
DESCRIPTION: Transfers the data in an 8 bit array to a 32 bit array
RETURNS:  void
ARGUMENTS:  DWORD* Output : the 32 bit (unsigned long) destination array
BYTE* Input   : the 8 bit (unsigned char) source array
UINT nLength  : the number of 8 bit data items in the source array
NOTES:   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.
*****************************************************************************************/
void CMD5Checksum::ByteToDWord(DWORD* Output, BYTE* Input, UINT nLength)
{
	//entry invariants
	ASSERT( nLength % 4 == 0 );
	ASSERT( AfxIsValidAddress(Output, nLength/4, TRUE) );
	ASSERT( AfxIsValidAddress(Input, nLength, FALSE) );

	//initialisations
	UINT i=0; //index to Output array
	UINT j=0; //index to Input array

	//transfer the data by shifting and copying
	for ( ; j < nLength; i++, j += 4)
	{
		Output[i] = (ULONG)Input[j]   |
			(ULONG)Input[j+1] << 8 |
			(ULONG)Input[j+2] << 16 |
			(ULONG)Input[j+3] << 24;
	}
}

/*****************************************************************************************
FUNCTION:  CMD5Checksum::Transform
DETAILS:  protected
DESCRIPTION: MD5 basic transformation algorithm;  transforms 'm_lMD5'
RETURNS:  void
ARGUMENTS:  BYTE Block[64]
NOTES:   An MD5 checksum is calculated by four rounds of 'Transformation'.
The MD5 checksum currently held in m_lMD5 is merged by the
transformation process with data passed in 'Block'. 
*****************************************************************************************/
void CMD5Checksum::Transform(BYTE Block[64])
{
	//initialise local data with current checksum
	ULONG a = m_lMD5[0];
	ULONG b = m_lMD5[1];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -