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

📄 md5keygendlg.cpp

📁 This algorithm was developed by Professor Ronald L. Rivest of MIT and can be found presented in seve
💻 CPP
字号:
// MD5KeyGenDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MD5KeyGen.h"
#include "MD5KeyGenDlg.h"
#include "..\gTools\MD5\md5.h"

#include <Windowsx.h>
#include <commctrl.h>

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

// Define our right click menu commands /////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
#define COPY_TO_CLIPBOARD	1


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMD5KeyGenDlg dialog

CMD5KeyGenDlg::CMD5KeyGenDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMD5KeyGenDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMD5KeyGenDlg)
	m_bLog = FALSE;
	m_csPhrase = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMD5KeyGenDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMD5KeyGenDlg)
	DDX_Control(pDX, IDC_EDIT_PHRASE, m_EditBox);
	DDX_Control(pDX, IDC_LIST_OUTPUT, m_Output);
	DDX_Check(pDX, IDC_CHECK_APPEND, m_bLog);
	DDX_Text(pDX, IDC_EDIT_PHRASE, m_csPhrase);
	DDV_MaxChars(pDX, m_csPhrase, 1024);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMD5KeyGenDlg, CDialog)
	//{{AFX_MSG_MAP(CMD5KeyGenDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_ABOUT, OnButtonAbout)
	ON_BN_CLICKED(IDC_BUTTON_INFO, OnButtonInfo)
	ON_BN_CLICKED(IDGENERATE, OnGenerate)
	ON_BN_CLICKED(IDC_BUTTON_CHOOSE, OnButtonChoose)
	ON_NOTIFY(NM_RCLICK, IDC_LIST_OUTPUT, OnRclickListOutput)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMD5KeyGenDlg message handlers

BOOL CMD5KeyGenDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// 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
	
	m_bLog = TRUE;

	ListView_SetExtendedListViewStyle( m_Output.m_hWnd, 
		LVS_EX_FULLROWSELECT|LVS_EX_ONECLICKACTIVATE|LVS_EX_UNDERLINEHOT );

	m_Output.InsertColumn(0, "Key");
	m_Output.InsertColumn(1, "MD5 Checksum");
	m_Output.SetColumnWidth(0, 150);
	m_Output.SetColumnWidth(1, LVSCW_AUTOSIZE_USEHEADER);
	UpdateData(FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMD5KeyGenDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

void CMD5KeyGenDlg::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();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMD5KeyGenDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

// OnButtonAbout
// Display the About dialog
void CMD5KeyGenDlg::OnButtonAbout() 
{
	CAboutDlg dlg;

	dlg.DoModal();
}

// OnButtonInfo
// Display the MD5 RFC (rfc1321)
void CMD5KeyGenDlg::OnButtonInfo() 
{
	ShellExecute(NULL, "open", "http://www.faqs.org/rfcs/rfc1321.html", NULL, NULL, SW_SHOWNORMAL);
}

// OnGenerate
// Generate an MD5 key based on the phrase given.
void CMD5KeyGenDlg::OnGenerate() 
{
	UpdateData(TRUE);

	if ( m_csPhrase.GetLength() < 1 )
		return;

	CString csMD5 = MD5String(m_csPhrase.GetBuffer(0));

	if ( m_bLog )
	{
		theApp.Report("MD5 Checksum for phrase \"%s\" = <%s>", 
					  m_csPhrase.GetBuffer(0), csMD5.GetBuffer(0));
	}

	int nItem = m_Output.InsertItem( m_Output.GetItemCount()+1, m_csPhrase );
	m_Output.SetItemText(nItem, 1, csMD5);

	m_csPhrase = CString("");
	m_EditBox.SetFocus();

	UpdateData(FALSE);
}

// FileName
// Convert a full path/filename to just the file name
CString FileName(CString szFile)
{
  CString gfnPath(szFile);

  char *szDot = strchr(gfnPath.GetBuffer(0), '.');

  if (szDot != NULL) 
    szDot[0] = 0;

  char *szPathSlash = strrchr(gfnPath.GetBuffer(0), '\\');
  
  return CString(szPathSlash+1);
}

// FileExt
// Return the extension (if any) of a given file
CString FileExt(CString szFile)
{
  char *szExtension = strchr(szFile.GetBuffer(0), '.');

  if (szExtension == NULL) 
    return CString("");

  return CString(szExtension);
}

// OnButtonChoose
// Display a file dialog to the user, and generate the MD5 keys for each file
// selected.  Prints the output to the output window.
void CMD5KeyGenDlg::OnButtonChoose() 
{
	CFileDialog fd(	TRUE, NULL, NULL, OFN_ALLOWMULTISELECT|OFN_EXPLORER|OFN_ENABLESIZING,
		            _T("All Files (*.*)|*.*|All Files (*.*)|*.*||"));

	fd.m_ofn.lpstrTitle = "Select the file(s) you want to generate keys for.";

	int nResult = fd.DoModal();

	if (nResult != IDCANCEL )
	{
		CString csPath;
		CString csFile;
		CString csMD5;

		POSITION pos = fd.GetStartPosition();

		while (pos)
		{
			csPath = fd.GetNextPathName(pos);
			csMD5  = MD5File( csPath.GetBuffer(0) ); 
			csFile = FileName(csPath) + FileExt(csPath);

			if ( m_bLog )
				theApp.Report("MD5 Checksum for file %s = <%s>", csFile.GetBuffer(0), csMD5.GetBuffer(0));

			int nItem = m_Output.InsertItem( m_Output.GetItemCount()+1, csFile );
			m_Output.SetItemText(nItem, 1, csMD5);
		}

	}

	m_EditBox.SetFocus();
}


// OnRclickListOutput
// Handle right click menu.
//
//  Menu Options:
//     - Copy to clipboard. 
//       Copy the selected MD5 key to the clipboard for easier use.
//
void CMD5KeyGenDlg::OnRclickListOutput(NMHDR* pNMHDR, LRESULT* pResult) 
{
	DWORD dwPos = GetMessagePos();
	CPoint cp( GET_X_LPARAM( dwPos ), GET_Y_LPARAM( dwPos ) );
	CPoint cpTest = cp;

	*pResult = 0;

	m_Output.ScreenToClient(&cpTest);

	UINT nItem = m_Output.HitTest( cpTest, &nItem );
	
	if ( nItem != -1 )
	{
		UINT nTrack;

		CMenu* pMenu = new CMenu;

		pMenu->CreatePopupMenu();

		pMenu->AppendMenu(MF_POPUP, COPY_TO_CLIPBOARD, "Copy to Clipboard");

		nTrack = pMenu->TrackPopupMenu(TPM_RETURNCMD | TPM_RIGHTBUTTON | TPM_LEFTALIGN, cp.x, cp.y,this,0);

		if ( nTrack == COPY_TO_CLIPBOARD )
		{
			if (!OpenClipboard())
			{
				AfxMessageBox("Unable to open the clipboard.");
				return;
			}

			CString csKey = m_Output.GetItemText(nItem, 1);

			HGLOBAL hg = GlobalAlloc(GMEM_ZEROINIT, csKey.GetLength()+1 * sizeof(TCHAR));

			if ( hg != NULL )
			{
				EmptyClipboard();

				LPTSTR lpStr = (LPSTR)GlobalLock(hg);
				memcpy(lpStr, csKey, csKey.GetLength() * sizeof(TCHAR));
				lpStr[ csKey.GetLength() ] = (TCHAR)0;

				GlobalUnlock(hg);
				SetClipboardData(CF_TEXT, hg);
				CloseClipboard();
			}

		}

		pMenu->DestroyMenu();
		delete pMenu;
	}

}

⌨️ 快捷键说明

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