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

📄 md5checksumtestdlg.cpp

📁 md5算法
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// MD5ChecksumTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MD5ChecksumTest.h"
#include "MD5ChecksumTestDlg.h"
#include "MD5Checksum.h"
#include "MD5About.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////////////
// CMD5ChecksumTestDlg dialog

/*****************************************************************************************
CONSTRUCTOR:	CMD5ChecksumTestDlg
DESCRIPTION:	Initialises member data
ARGUMENTS:		CWnd* pParent - see MFC
NOTES:			None
*****************************************************************************************/
CMD5ChecksumTestDlg::CMD5ChecksumTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMD5ChecksumTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMD5ChecksumTestDlg)
	m_strSelfTestStatus = _T("");
	m_strChecksum = _T("");
	m_strEntry = _T("");
	m_strFileChecksum = _T("");
	m_strSelectedFile = _T("");
	//}}AFX_DATA_INIT

	//an empty string has a checksum, so calculate it
	m_strChecksum = CMD5Checksum::GetMD5( (BYTE*)(const char*)m_strEntry, m_strEntry.GetLength());

	//Note that LoadIcon does not require a subsequent DestroyIcon in Win32	
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	//find the folder containing test data file
	m_bTestDataFolderFound = LoadTestDataPath();
	if ( !m_bTestDataFolderFound )
	{
		//report an error if cannot find folder
		CString strErrMsg = CString("Could not find TestData Folder: ") +
							m_strTestDataPath +
							CString("\nSelf Test not available");
		AfxMessageBox(strErrMsg);
	}
}


/*****************************************************************************************
FUNCTION:		CMD5ChecksumTestDlg::DoDataExchange
DESCRIPTION:	see MFC
*****************************************************************************************/
void CMD5ChecksumTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMD5ChecksumTestDlg)
	DDX_Text(pDX, IDC_EDIT_SELF_TEST_STATUS, m_strSelfTestStatus);
	DDX_Text(pDX, IDC_EDIT_STR_CHECKSUM, m_strChecksum);
	DDX_Text(pDX, IDC_EDIT_STR, m_strEntry);
	DDX_Text(pDX, IDC_EDIT_FILE_CHECKSUM, m_strFileChecksum);
	DDX_Text(pDX, IDC_EDIT_SEL_FILE, m_strSelectedFile);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMD5ChecksumTestDlg, CDialog)
	//{{AFX_MSG_MAP(CMD5ChecksumTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_SELFTEST, OnButtonSelfTest)
	ON_EN_CHANGE(IDC_EDIT_STR, OnChangeEditStr)
	ON_BN_CLICKED(IDC_BUTTON_SEL_FILE, OnButtonSelFile)
	ON_BN_CLICKED(IDC_BUTTON_ABOUT, OnButtonAbout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMD5ChecksumTestDlg message handlers

/*****************************************************************************************
FUNCTION:		CMD5ChecksumTestDlg::OnInitDialog
DETAILS:		overrides CDialog::OnInitDialog
DESCRIPTION:	see MFC
RETURNS:		see MFC
ARGUMENTS:		see MFC
NOTES:			see MFC
*****************************************************************************************/
BOOL CMD5ChecksumTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	//Set the icon for this dialog.  The framework does this automatically
	//when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	//initialise dialog's controls
	EnableCtrls(true);

	return TRUE;  // return TRUE  unless you set the focus to a control
}


/*****************************************************************************************
FUNCTION:		CMD5ChecksumTestDlg::OnSysCommand
DETAILS:		overrides CWnd::OnSysCommand
DESCRIPTION:	see MFC
RETURNS:		see MFC
ARGUMENTS:		see MFC
NOTES:			see MFC
*****************************************************************************************/
void CMD5ChecksumTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}


/*****************************************************************************************
FUNCTION:		CMD5ChecksumTestDlg::OnSysCommand
DETAILS:		overrides CWnd::OnSysCommand
DESCRIPTION:	see MFC
RETURNS:		see MFC
ARGUMENTS:		see MFC
NOTES:			see MFC
				If you add a minimize button to your dialog, you will need the code below
				to draw the icon.  For MFC applications using the document/view model,
				this is automatically done for you by the framework.
*****************************************************************************************/
void CMD5ChecksumTestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}


/*****************************************************************************************
FUNCTION:		CMD5ChecksumTestDlg::OnQueryDragIcon
DETAILS:		overrides CWnd::OnQueryDragIcon
DESCRIPTION:	see MFC
RETURNS:		see MFC
ARGUMENTS:		see MFC
NOTES:			see MFC
				The system calls this to obtain the cursor to display while the user drags
				the minimized window.
*****************************************************************************************/
HCURSOR CMD5ChecksumTestDlg::OnQueryDragIcon()
{
	return (HCURSOR)m_hIcon;
}



/*****************************************************************************************
FUNCTION:		CMD5ChecksumTestDlg::UpdateDataNow
DETAILS:		protected.
DESCRIPTION:	Provides the dialog box with a message pump.
RETURNS:		Non Zero if the operation is successfull, otherwise zero
ARGUMENTS:		BOOL bSaveAndValidate - see MFC
NOTES:			By providing the dialog box with its own message pump, data displayed in
				the dialog can be updated immediately from within a CMD5ChecksumTestDlg 
				function. A normal UpdataData command is not properly processed until the 
				function that makes the UpdateData call is exited and the windows message 
				pump is entered.
*****************************************************************************************/
BOOL CMD5ChecksumTestDlg::UpdateDataNow(BOOL bSaveAndValidate)
{	
	BOOL bResult = CWnd::UpdateData(bSaveAndValidate);
	MSG msg;
	while ( PeekMessage ( &msg, (HWND) NULL, 0, 0, PM_REMOVE ) )
	{
		TranslateMessage( &msg );
		DispatchMessage( &msg );
	}
	return bResult;
}


/*****************************************************************************************
FUNCTION:	 CMD5ChecksumTestDlg::SelfTest01
DETAILS:	 protected
DESCRIPTION: Checks the MD5 checksum calculation for the string "message digest"
RETURNS:	 bool : true if checksum calculated correctly, false otherwise
ARGUMENTS:	 none
NOTES:		 MD5("message digest") = f96b697d7cb7938d525a2f31aaf161d0
			 Tests function CMD5Checksum::GetMD5(BYTE* pBuf, UINT nLength)
*****************************************************************************************/
bool CMD5ChecksumTestDlg::SelfTest01()
{
	CString strMessageDigest("message digest");
	return CMD5Checksum::GetMD5(
				(BYTE*)(const char*)strMessageDigest,
				strMessageDigest.GetLength()
		   ) == "f96b697d7cb7938d525a2f31aaf161d0";																						    
}


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



/*****************************************************************************************
FUNCTION:	 CMD5ChecksumTestDlg::SelfTest03
DETAILS:	 protected
DESCRIPTION: Checks the MD5 checksum calculation for a file containing "message digest"
RETURNS:	 bool : true if checksum calculated correctly, false otherwise
ARGUMENTS:	 none
NOTES:		 MD5("message digest") = f96b697d7cb7938d525a2f31aaf161d0
			 Tests function CMD5Checksum::GetMD5(CFile& File)
*****************************************************************************************/
bool CMD5ChecksumTestDlg::SelfTest03()
{
	//test passing CFile file.
	CString strFile = m_strTestDataPath + CString("\\messagedigest.txt");
	CFile File(
		strFile,
		CFile::modeRead | CFile::shareDenyWrite | CFile::typeBinary
	);

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

	return bResult;
}


/*****************************************************************************************
FUNCTION:	 CMD5ChecksumTestDlg::SelfTest04
DETAILS:	 protected
DESCRIPTION: Checks the MD5 checksum calculation for a file containing "abc"
RETURNS:	 bool : true if checksum calculated correctly, false otherwise
ARGUMENTS:	 none
NOTES:		 MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
			 Tests function CMD5Checksum::GetMD5(CFile& File)
*****************************************************************************************/
bool CMD5ChecksumTestDlg::SelfTest04()
{
	//test passing CFile file.
	CString strFile = m_strTestDataPath + CString("\\abc.txt");
	CFile File(
		strFile,
		CFile::modeRead | CFile::shareDenyWrite | CFile::typeBinary
	);

	bool bResult = CMD5Checksum::GetMD5(File) == "900150983cd24fb0d6963f7d28e17f72";
	File.Close();

	return bResult;
}


/*****************************************************************************************
FUNCTION:	 CMD5ChecksumTestDlg::SelfTest05
DETAILS:	 protected
DESCRIPTION: Checks the MD5 checksum calculation for the string "abc"
RETURNS:	 bool : true if checksum calculated correctly, false otherwise
ARGUMENTS:	 none
NOTES:		 MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
			 Tests function CMD5Checksum::GetMD5(BYTE* pBuf, UINT nLength)
*****************************************************************************************/
bool CMD5ChecksumTestDlg::SelfTest05()
{
	CString strABC("abc");
	return CMD5Checksum::GetMD5( 
				(BYTE*)(const char*)strABC,strABC.GetLength()
		   ) == "900150983cd24fb0d6963f7d28e17f72";
}


/*****************************************************************************************
FUNCTION:	 CMD5ChecksumTestDlg::SelfTest06
DETAILS:	 protected
DESCRIPTION: Checks the MD5 checksum calculation for a file containing "abc"
RETURNS:	 bool : true if checksum calculated correctly, false otherwise
ARGUMENTS:	 none

⌨️ 快捷键说明

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