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

📄 md5checksumtestdlg.cpp

📁 MD5 加 密的V i s u a l C + + S o u r c e C o d e .
💻 CPP
📖 第 1 页 / 共 4 页
字号:
NOTES:		 MD5 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678") !=
			 d174ab98d277d9f5a5611c2c9f419d9f
			 Tests function CMD5Checksum::GetMD5(BYTE* pBuf, UINT nLength)
			 Checks that if the final digit in the SelfTest18 string is removed, then a
			 different checksum is calculated
*****************************************************************************************/
bool CMD5ChecksumTestDlg::SelfTest18a()
{
	CString strAlphaNumeric("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678");
	return CMD5Checksum::GetMD5( 
				(BYTE*)(const char*)strAlphaNumeric, strAlphaNumeric.GetLength()
		   ) != "d174ab98d277d9f5a5611c2c9f419d9f";
}


/*****************************************************************************************
FUNCTION:	 CMD5ChecksumTestDlg::SelfTest19
DETAILS:	 protected
DESCRIPTION: Checks the MD5 checksum calculation for a file containing  numbers
RETURNS:	 bool : true if checksum calculated correctly, false otherwise
ARGUMENTS:	 none
NOTES:		 MD5 ("123456789012345678901234567890123456789012345678901234567890123456
			 78901234567890") = 57edf4a22be3c955ac49da2e2107b67a
			 Tests the function CMD5Checksum::GetMD5(const CString& strFilePath)
*****************************************************************************************/
bool CMD5ChecksumTestDlg::SelfTest19()
{	
	CString strFile = m_strTestDataPath + CString("\\Numeric.txt");
	return CMD5Checksum::GetMD5( strFile ) == "57edf4a22be3c955ac49da2e2107b67a";
}


/*****************************************************************************************
FUNCTION:	 CMD5ChecksumTestDlg::SelfTest20
DETAILS:	 protected
DESCRIPTION: Checks the MD5 checksum calculation for a file containing  numbers
RETURNS:	 bool : true if checksum calculated correctly, false otherwise
ARGUMENTS:	 none
NOTES:		 MD5 ("123456789012345678901234567890123456789012345678901234567890123456
			 78901234567890") = 57edf4a22be3c955ac49da2e2107b67a
			 Tests function CMD5Checksum::GetMD5(CFile& File)
*****************************************************************************************/
bool CMD5ChecksumTestDlg::SelfTest20()
{
	CString strFile = m_strTestDataPath + CString("\\Numeric.txt");

	//test passing CFile file.
	CFile File(
		strFile,
		CFile::modeRead | CFile::shareDenyWrite | CFile::typeBinary
	);

	bool bResult = CMD5Checksum::GetMD5(File) == "57edf4a22be3c955ac49da2e2107b67a";
	File.Close();

	return bResult;
}

/*****************************************************************************************
FUNCTION:	 CMD5ChecksumTestDlg::SelfTest21
DETAILS:	 protected
DESCRIPTION: Checks the MD5 checksum calculation for a string containing  numbers
RETURNS:	 bool : true if checksum calculated correctly, false otherwise
ARGUMENTS:	 none
NOTES:		 MD5 ("123456789012345678901234567890123456789012345678901234567890123456
			 78901234567890") = 57edf4a22be3c955ac49da2e2107b67a
			 Tests function CMD5Checksum::GetMD5(BYTE* pBuf, UINT nLength)
*****************************************************************************************/
bool CMD5ChecksumTestDlg::SelfTest21()
{
	CString strNumeric(
		"12345678901234567890123456789012345678901234567890123456789012345678901234567890"
	);

	return CMD5Checksum::GetMD5( 
				(BYTE*)(const char*)strNumeric, strNumeric.GetLength()
		   ) == "57edf4a22be3c955ac49da2e2107b67a";

}


/*****************************************************************************************
FUNCTION:	 CMD5ChecksumTestDlg::SelfTest21a
DETAILS:	 protected
DESCRIPTION: Checks the MD5 checksum calculation for a corrupted SelfTest21 string
RETURNS:	 bool : true if checksum calculated correctly, false otherwise
ARGUMENTS:	 none
NOTES:		 MD5 ("23456789012345678901234567890123456789012345678901234567890123456
			 78901234567890") != 57edf4a22be3c955ac49da2e2107b67a
			 Tests function CMD5Checksum::GetMD5(BYTE* pBuf, UINT nLength)
			 The string is corrupted by removing the first digit.
*****************************************************************************************/
bool CMD5ChecksumTestDlg::SelfTest21a()
{
	CString strNumeric(
		"2345678901234567890123456789012345678901234567890123456789012345678901234567890"
	);

	return CMD5Checksum::GetMD5( 
				(BYTE*)(const char*)strNumeric, strNumeric.GetLength()
		   ) != "57edf4a22be3c955ac49da2e2107b67a";

}


/*****************************************************************************************
FUNCTION:	 CMD5ChecksumTestDlg::SelfTest22
DETAILS:	 protected
DESCRIPTION: Checks the MD5 checksum calculation for a 50kb file
RETURNS:	 bool : true if checksum calculated correctly, false otherwise
ARGUMENTS:	 none
NOTES:		 MD5 ("MD5 Test Data File.txt"") == eaf2d7238637e95bbf666fa0b98fa64f
			 Tests the function CMD5Checksum::GetMD5(const CString& strFilePath)
			 NB. Modifications to this implementation of the MD5 algorithm may work	
			 correctly for small files or strings, but not for larger amounts of data.
			 This test on a 50Kb file help checks correctness in this respect. 
******************************************************************************************/
bool CMD5ChecksumTestDlg::SelfTest22()
{
	CString strFile = m_strTestDataPath + CString("\\MD5 Test Data File.txt");
	return ( CMD5Checksum::GetMD5( strFile ) == "eaf2d7238637e95bbf666fa0b98fa64f" );
}


/*****************************************************************************************
FUNCTION:	 CMD5ChecksumTestDlg::LoadLargeTestFile
DETAILS:	 protected
DESCRIPTION: Loads into a buffer the data from "MD5 Test Data File 1.txt" file
RETURNS:	 Size (in bytes) of the large test file
ARGUMENTS:	 none
NOTES:		 none
******************************************************************************************/
int CMD5ChecksumTestDlg::LoadLargeTestFile()
{
	// entry invariant
	ASSERT( m_pTestFileBuffer == NULL );

	//open the file as a binary file in readonly mode, denying write access 
	CString strFilePath = m_strTestDataPath + CString("\\MD5 Test Data File.txt");
	CFile File(strFilePath, CFile::modeRead | CFile::shareDenyWrite | CFile::typeBinary);

	// read the file contents into a buffer
	const int knBufferSize = 51200;
	m_pTestFileBuffer = new BYTE[knBufferSize];
	int nLength = File.Read( m_pTestFileBuffer, knBufferSize );
	ASSERT( nLength == knBufferSize );
	return nLength;
}


/*****************************************************************************************
FUNCTION:	 CMD5ChecksumTestDlg::UnloadLargeTestFile
DETAILS:	 protected
DESCRIPTION: Frees the memory allocated for the large test file.
RETURNS:	 none
ARGUMENTS:	 none
NOTES:		 none
******************************************************************************************/
void CMD5ChecksumTestDlg::UnloadLargeTestFile()
{
	delete m_pTestFileBuffer;
	m_pTestFileBuffer = NULL;
}


/*****************************************************************************************
FUNCTION:	 CMD5ChecksumTestDlg::SelfTest23
DETAILS:	 protected
DESCRIPTION: This is similar to SelfTest22. However, the large test file is first loaded 
			 into a buffer. A pointer to the buffer is passed to the MD5 function.
RETURNS:	 bool : true if checksum calculated correctly, false otherwise
ARGUMENTS:	 none
NOTES:		 none
******************************************************************************************/
bool CMD5ChecksumTestDlg::SelfTest23()
{
	const int nTestFileSize = LoadLargeTestFile();

	bool bOK = ( CMD5Checksum::GetMD5( m_pTestFileBuffer, nTestFileSize ) == "eaf2d7238637e95bbf666fa0b98fa64f" );

	UnloadLargeTestFile();

	return bOK;
}


/*****************************************************************************************
FUNCTION:	 CMD5ChecksumTestDlg::SelfTest
DETAILS:	 private, message map function for Self Test Button
DESCRIPTION: Calls a series of test functions to check the correctness of CMD5Checksum
RETURNS:	 void
ARGUMENTS:	 none
NOTES:		 The following tests are performed:-
			 MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
			 MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661
			 MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
			 MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0
			 MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
			 MD5 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") 
					= d174ab98d277d9f5a5611c2c9f419d9f
			 MD5 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") 
					= 57edf4a22be3c955ac49da2e2107b67a
			 MD5 ( MD5 Test Data File.txt ) = 9f9fb64291dbb5088abc465badb42937
			 All three of the CMD5Checksum::GetMD5 checksums are called for each of the
			 test strings. 
*****************************************************************************************/
bool CMD5ChecksumTestDlg::SelfTest()
{
	bool bOK = true;

	try
	{
		bOK = bOK && SelfTest01();
		bOK = bOK && SelfTest02();
		bOK = bOK && SelfTest03();
		bOK = bOK && SelfTest04();
		bOK = bOK && SelfTest05();
		bOK = bOK && SelfTest06();
		bOK = bOK && SelfTest07();
		bOK = bOK && SelfTest08();
		bOK = bOK && SelfTest09();
		bOK = bOK && SelfTest10();
		bOK = bOK && SelfTest11();
		bOK = bOK && SelfTest12();
		bOK = bOK && SelfTest13();
		bOK = bOK && SelfTest14();
		bOK = bOK && SelfTest15();
		bOK = bOK && SelfTest15a();
		bOK = bOK && SelfTest16();
		bOK = bOK && SelfTest17();
		bOK = bOK && SelfTest18();
		bOK = bOK && SelfTest18a();
		bOK = bOK && SelfTest19();
		bOK = bOK && SelfTest20();
		bOK = bOK && SelfTest21();
		bOK = bOK && SelfTest21a();
		bOK = bOK && SelfTest22();
		bOK = bOK && SelfTest23();
	}
	catch(CFileException* e)
	{
		CString strErrMsg = CString("Problem with file:\n") + e->m_strFileName;
		AfxMessageBox(strErrMsg);
		bOK = false;
	}
	
	return bOK;
}



/*****************************************************************************************
FUNCTION:	 CMD5ChecksumTestDlg::OnButtonSelfTest
DETAILS:	 private, message map function for Self Test Button
DESCRIPTION: Calls the selftest function to check the correctness of CMD5Checksum. 
			 Displays the result to the user. 
RETURNS:	 void
ARGUMENTS:	 none
NOTES:		 none
*****************************************************************************************/
void CMD5ChecksumTestDlg::OnButtonSelfTest() 
{
	ASSERT( m_bTestDataFolderFound );

	//tell the user to wait for the test to complete
	m_strSelfTestStatus.LoadString(IDS_PLEASE_WAIT);
	EnableCtrls(false);
	UpdateDataNow(FALSE);

	//perform the selftest and display the result to the user
	m_strSelfTestStatus = ( SelfTest() ? CString("All Pass") : CString("Fail") );
	EnableCtrls(true);
	UpdateDataNow(FALSE);
}


/*****************************************************************************************
FUNCTION:	 CMD5ChecksumTestDlg::OnChangeEditStr
DETAILS:	 private, message map function 
DESCRIPTION: Gets and displays the MD5 checksum for a user entered string
RETURNS:	 void
ARGUMENTS:	 none
NOTES:		 This function is called whenever the user enters data in the 
			 "Enter a string and see the checksum" edit box. This function is called as
			 soon as the user enters any data, so the user will see the checksum change
			 as each individual character of the string is entered. 
*****************************************************************************************/
void CMD5ChecksumTestDlg::OnChangeEditStr() 
{
	//get the user entered data
	UpdateData();

	//calculate the checksum for the user entered string
	m_strChecksum = CMD5Checksum::GetMD5( (BYTE*)(const char*)m_strEntry, m_strEntry.GetLength() );

	//update the dialog box with the newly calculated checksum
	UpdateData(FALSE);
}


/*****************************************************************************************
FUNCTION:	 CMD5ChecksumTestDlg::OnButtonSelFile
DETAILS:	 private, message map function for File Selection Button
DESCRIPTION: Gets and displays the MD5 checksum for a user selected file
RETURNS:	 void
ARGUMENTS:	 none
NOTES:		 Displays the Windows File Open Dialog to allow the user to select a file. The
			 MD5 checksum for the file is then calculated and displayed using the 
			 ChecksumSelectedFile function. Controls on the dialog are disabled while the 
			 checksum is being calculated. The ChecksumSelectedFile function can throw the 
			 CFileException (as it calls GetMD5 which tries open the user selected file.) 
			 Typically this will occur if the user selects a file which is already open in 
			 another application, in which case a sharing violation will occur. 
*****************************************************************************************/
void CMD5ChecksumTestDlg::OnButtonSelFile() 
{
	//display the file open dialog
	CFileDialog	FileDialog( TRUE );
	if ( FileDialog.DoModal() == IDOK )

⌨️ 快捷键说明

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