Click here to Skip to main content
15,895,142 members
Articles / Desktop Programming / MFC

Exile 1.8 - The Password Manager

Rate me:
Please Sign up or sign in to vote.
4.57/5 (51 votes)
6 Mar 20058 min read 257K   7.4K   111  
Yet another password manager.
// MD5HashGeneratorDialog.cpp : implementation file
//

#include "stdafx.h"
#include "exile.h"
#include "MD5HashGeneratorDialog.h"
#include "Security.h"
#include "../MD5/MD5.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMD5HashGeneratorDialog dialog


CMD5HashGeneratorDialog::CMD5HashGeneratorDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CMD5HashGeneratorDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMD5HashGeneratorDialog)
	m_nHashOptions = 0;
	m_strMd5Hash = _T("");
	m_strFileName = _T("");
	//}}AFX_DATA_INIT
}


void CMD5HashGeneratorDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMD5HashGeneratorDialog)
	DDX_Radio(pDX, IDC_HASHFILE, m_nHashOptions);
	DDX_Text(pDX, IDC_MD5HASH, m_strMd5Hash);
	DDX_Text(pDX, IDC_FILENAME, m_strFileName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMD5HashGeneratorDialog, CDialog)
	//{{AFX_MSG_MAP(CMD5HashGeneratorDialog)
	ON_BN_CLICKED(IDC_HASHFILE, OnHashfile)
	ON_BN_CLICKED(IDC_HASHMESSAGE, OnHashmessage)
	ON_BN_CLICKED(IDC_GENERATE, OnGenerate)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMD5HashGeneratorDialog message handlers

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

	m_gcControls.AddControl(FILEHASH, GetDlgItem(IDC_FILENAME)->GetSafeHwnd());
	m_gcControls.AddControl(FILEHASH, GetDlgItem(IDC_BROWSE)->GetSafeHwnd());

	m_gcControls.AddControl(MESSAGEHASH, GetDlgItem(IDC_MESSAGE)->GetSafeHwnd());
	
	//
	// Controls
	//
	m_gcControls.SwitchControls(m_nHashOptions ? FILEHASH : MESSAGEHASH, CGroupedControls::SWS_DISABLE, 
		CGroupedControls::SWS_ENABLE);

	m_ctrlGenerate.SubclassDlgItem(IDC_GENERATE, this);

	UpdateData(FALSE);
		
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CMD5HashGeneratorDialog::OnHashfile() 
{
	UpdateData();

	m_gcControls.SwitchControls(m_nHashOptions ? FILEHASH : MESSAGEHASH, CGroupedControls::SWS_DISABLE, 
		CGroupedControls::SWS_ENABLE);
}

void CMD5HashGeneratorDialog::OnHashmessage() 
{
	UpdateData();

	m_gcControls.SwitchControls(m_nHashOptions ? FILEHASH : MESSAGEHASH, CGroupedControls::SWS_DISABLE, 
		CGroupedControls::SWS_ENABLE);
}

void CMD5HashGeneratorDialog::OnGenerate() 
{
	UpdateData();
	BeginWaitCursor();

	::Md5Initialize();

	MD5DIGEST dMd5;
	HMD5CONTEXT hMd5;

	if(!::Md5AcquireContext(hMd5))
	{
		EndWaitCursor();
		return;
	} // if
	
	// If we're hashing a file
	if(0 == m_nHashOptions) 
	{
		if(m_strFileName.IsEmpty())
		{
			EndWaitCursor();
			return;
		} // if

		CFile fData;
		CFileException eFe;

		if(!fData.Open(m_strFileName, CFile::modeRead | CFile::shareExclusive, &eFe)) 
		{
			// TODO: Meaningful error message
			EndWaitCursor();
			return;
		} // if
		
		MD5_BYTE *pBuffer = new MD5_BYTE[BUFFER_SIZE]; // IO buffer
		UINT unSize = sizeof(BYTE) * BUFFER_SIZE;
		UINT unBytes = fData.Read((void *)pBuffer, unSize);		

		while(unBytes)
		{
			if(!::Md5UpdateHash(hMd5, pBuffer, unBytes)) 
			{
				delete [] pBuffer;
				EndWaitCursor();
				return;
			} // if

			unBytes = fData.Read((void *)pBuffer, unSize);
		} // while
		

		delete [] pBuffer;

		::Md5FinalHash(hMd5, dMd5);
		::Md5ReleaseContext(hMd5);
	} // if
	else
	{
		CEdit *pMsg = ((CEdit *)GetDlgItem(IDC_MESSAGE));
		if(!pMsg)
		{
			EndWaitCursor();
			return;
		} // if

		// To avoid input or something
		pMsg->EnableWindow(FALSE);

		int nLength = pMsg->GetWindowTextLength();
		size_t sData = (nLength + 1) * sizeof(TCHAR);		
		
		// Allocating required buffer
		BYTE *pBuffer = new BYTE[sData];
		pMsg->GetWindowText((LPTSTR)pBuffer, sData);

		// Hashing
		::Md5UpdateHash(hMd5, pBuffer, sData - sizeof(TCHAR));
		::Md5FinalHash(hMd5, dMd5);

		FreeBuffer(pBuffer, sData);

		pMsg->EnableWindow(TRUE);
	} // else
	
	m_strMd5Hash = _T("0x");

	for(int n = 0; n < 16; ++n)
	{
		CString s;
		s.Format("%02X", dMd5[n]);

		m_strMd5Hash += s;
	} // for
	
	UpdateData(FALSE);

	::Md5ReleaseContext(hMd5);

	::Md5Uninitialize();

	EndWaitCursor();
}

void CMD5HashGeneratorDialog::OnDestroy() 
{
	UpdateData();
	CDialog::OnDestroy();	
}

void CMD5HashGeneratorDialog::OnBrowse() 
{
	CString strFilter;
	strFilter.LoadString(IDS_FILTER);

	CFileDialog dlgOpen(TRUE, 0, 0, OFN_HIDEREADONLY | OFN_NONETWORKBUTTON, strFilter, this);

	if(IDCANCEL == dlgOpen.DoModal())
	{
		EndWaitCursor();
		return;
	} // if	
	
	m_strFileName = dlgOpen.GetPathName();
	UpdateData(FALSE);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Russian Federation Russian Federation
I'll think about it later on...

Comments and Discussions