📄 md5checksumtestdlg.cpp
字号:
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::OnButtonSelfTest
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
All three of the CMD5Checksum::GetMD5 checksums are called for each of the
test strings.
*****************************************************************************************/
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 all the tests
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();
}
catch(CFileException* e)
{
CString strErrMsg = CString("Problem with file:\n") + e->m_strFileName;
AfxMessageBox(strErrMsg);
bOK = false;
}
//display the result to the user
m_strSelfTestStatus = ( bOK ? 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 )
{
//get the full path and name of the file and checksum it
m_strSelectedFile = FileDialog.GetPathName();
ChecksumSelectedFile();
}
}
/*****************************************************************************************
FUNCTION: CMD5ChecksumTestDlg::OnButtonAbout
DETAILS: public
DESCRIPTION: Displays the Help-About dialog
RETURNS: void
ARGUMENTS: none
NOTES: none
*****************************************************************************************/
void CMD5ChecksumTestDlg::OnButtonAbout()
{
CAboutDlg AboutDlg;
AboutDlg.DoModal();
}
/*****************************************************************************************
FUNCTION: CMD5ChecksumTestDlg::EnableCtrls
DETAILS: protected
DESCRIPTION: Enable or disables user interface ccontrols
RETURNS: void
ARGUMENTS: bool bEnable - enable or disable the controls
NOTES: Controls need to be disabled when a checksum calculation is being performed
(because they will not respond while the calculation is being performed -
the program is not multi-threaded!)
*****************************************************************************************/
void CMD5ChecksumTestDlg::EnableCtrls(bool bEnable /*= true */)
{
GetDlgItem(IDC_BUTTON_SELFTEST)->EnableWindow(bEnable && m_bTestDataFolderFound);
GetDlgItem(IDC_BUTTON_SEL_FILE)->EnableWindow(bEnable);
GetDlgItem(IDC_BUTTON_ABOUT)->EnableWindow(bEnable);
GetDlgItem(IDC_EDIT_STR)->EnableWindow(bEnable);
GetDlgItem(IDC_EDIT_SEL_FILE)->EnableWindow(bEnable);
}
/*****************************************************************************************
FUNCTION: CMD5ChecksumTestDlg::OnOK
DETAILS: protected, message map function for IDOK
DESCRIPTION: Checksums the entry in the "Select a File.." edit box when "Enter" is pressed.
RETURNS: void
ARGUMENTS: none
NOTES: Called whenever the Return (or Enter) key is pressed when the
"IDC_EDIT_SEL_FILE" edit box currently has focus. This function allows the
user to enter a file name manually and then initiate the checksum on the file
by pressing 'Enter'.
*****************************************************************************************/
void CMD5ChecksumTestDlg::OnOK()
{
//if there is an entry in the "Select a file..." edit box and it is the selected control
UpdateData();
if ( m_strSelectedFile.GetLength() > 0 && GetFocus() == GetDlgItem(IDC_EDIT_SEL_FILE) )
{
//get its checksum
ChecksumSelectedFile();
}
}
/*****************************************************************************************
FUNCTION: CMD5ChecksumTestDlg::OnCancel
DETAILS: protected, virtual, overrides CDialog::OnCancel
DESCRIPTION: Exits the application after user confirmation
RETURNS: void
ARGUMENTS: none
NOTES: This function is called when the user attempts to exit the application.
Its main purpose is to prevent accidental termination should the user
mistakenly press the 'Esc' key.
*****************************************************************************************/
void CMD5ChecksumTestDlg::OnCancel()
{
if ( AfxMessageBox("Do you want to exit?", MB_YESNO ) == IDYES )
{
CDialog::OnCancel();
}
}
/*****************************************************************************************
FUNCTION: CMD5ChecksumTestDlg::ChecksumSelectedFile
DETAILS: protected
DESCRIPTION: Calculates and displays the checksum for the file named in 'm_strSelectedFile'.
RETURNS: void
ARGUMENTS: none
NOTES: The function does not check that m_strSelectedFile represents a valid file
name before attempting to get the checksum. If the filename is not valid,
GetMD5 throws a CFileException that this function catches.
*****************************************************************************************/
void CMD5ChecksumTestDlg::ChecksumSelectedFile()
{
//check that a file has been selected
ASSERT( m_strSelectedFile.GetLength() > 0 );
//display a wait message while the checksum is calculated
m_strFileChecksum.LoadString(IDS_PLEASE_WAIT);
EnableCtrls(false);
UpdateDataNow(FALSE);
//calculate the checksum
try
{
m_strFileChecksum = CMD5Checksum::GetMD5( m_strSelectedFile );
}
//tell the user the calculation failed if a CFileException is thrown
catch(CFileException* e)
{
m_strFileChecksum = CString("Fail");
EnableCtrls(true);
UpdateData(FALSE);
throw e;
}
//no exception was thrown - display the checksum result
EnableCtrls(true);
UpdateData(FALSE);
}
/*****************************************************************************************
FUNCTION: CMD5ChecksumTestDlg::LoadTestDataPath
DETAILS: protected
DESCRIPTION: Loads 'm_strTestDataPath' with the pathname of the test data folder.
RETURNS: bool : true if the folder is found, false otherwise
ARGUMENTS: none
NOTES: The data files used in the Self Test function are held in a folder "TestData".
In the target environment (ie, a non-development environment), this folder
should be located in the same folder as the executable. However, in a
development environment, the executable will be held in either the "debug" or
"release" folder, while the "TestData' folder will be held one level higher, ie,
with the programs source code. This function determines the path of the TestData
folder, taking account of three scenarios;
1) the release version of the program is being run standalone from the
"Release" folder
2) the debug version of the program is being run standalone from the
"Debug" folder
3) the release or debug program is being run from within the development
environment
*****************************************************************************************/
bool CMD5ChecksumTestDlg::LoadTestDataPath()
{
const CString strTestDataFolder("\\TestData");
//get the current folder
char szCurDir[512];
GetCurrentDirectory(512,szCurDir);
CString strCurDir(szCurDir);
//if the release version of the program is being run standalone from the "Release" folder
if (strCurDir.Right(7) == "Release")
{
m_strTestDataPath = strCurDir.Left( strCurDir.GetLength() - 7 ) + strTestDataFolder;
}
else
//if the debug version of the program is being run standalone from the "Debug" folder
if (strCurDir.Right(5) == "Debug")
{
m_strTestDataPath = strCurDir.Left( strCurDir.GetLength() - 5 ) + strTestDataFolder;
}
else
{
//program is being run from same folder as \\TestData
m_strTestDataPath = strCurDir + CString("\\TestData");
}
//check the folder exists (nb, no need to check it is an actual folder.)
CFileFind FileFind;
return ( FileFind.FindFile(m_strTestDataPath) ? true : false );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -