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

📄 shaprojdlg.cpp

📁 以前发的那个只实现了SHA-1, 现在补全了, sha-160, sha-224, sha-256, sha-384, sha-512
💻 CPP
字号:
// SHAProjDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SHAProj.h"
#include "SHAProjDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CSHAProjDlg dialog

CSHAProjDlg::CSHAProjDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSHAProjDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSHAProjDlg)
	m_nSHAType = -1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSHAProjDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSHAProjDlg)
	DDX_Control(pDX, IDC_PROGRESS, m_Pos);
	DDX_Radio(pDX, IDC_RADIO_SHA160, m_nSHAType);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSHAProjDlg, CDialog)
	//{{AFX_MSG_MAP(CSHAProjDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_ABOUT, OnBtnAbout)
	ON_CBN_SELCHANGE(IDC_COMBO_TYPE, OnSelchangeComboType)
	ON_EN_CHANGE(IDC_EDIT_DATA, OnChangeEditData)
	ON_BN_CLICKED(IDC_RADIO_SHA160, OnRadioSha160)
	ON_BN_CLICKED(IDC_RADIO_SHA224, OnRadioSha224)
	ON_BN_CLICKED(IDC_RADIO_SHA256, OnRadioSha256)
	ON_BN_CLICKED(IDC_RADIO_SHA384, OnRadioSha384)
	ON_BN_CLICKED(IDC_RADIO_SHA512, OnRadioSha512)
	ON_WM_DROPFILES()
	ON_BN_CLICKED(IDC_CHECK_UPPER, OnCheckUpper)
	ON_BN_CLICKED(IDC_BTN_CALCULATION, OnBtnCalculation)
	ON_BN_CLICKED(IDC_BTN_BROWSE, OnBtnBrowse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSHAProjDlg message handlers

BOOL CSHAProjDlg::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
	
	// TODO: Add extra initialization here
	
	((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->AddString("文件");
	((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->AddString("字符串");
	((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->SetCurSel(0);

	AdjustWindow(true);

	GetDlgItem(IDC_PROGRESS)->ShowWindow(SW_HIDE);

	((CButton*)GetDlgItem(IDC_CHECK_UPPER))->SetCheck(1);

	m_nSHAType = 0;
	UpdateData(FALSE);

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

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

// 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 CSHAProjDlg::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 CSHAProjDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSHAProjDlg::AdjustWindow(bool bFlag)
{
	RECT	rcEdit;
	RECT	rcButton;
	::GetWindowRect(GetDlgItem(IDC_EDIT_RESULT)->m_hWnd, &rcEdit);
	::GetWindowRect(GetDlgItem(IDC_BTN_CALCULATION)->m_hWnd, &rcButton);

	if(!bFlag)
	{
		GetDlgItem(IDC_EDIT_DATA)->SetWindowPos(NULL, 0, 0, 
												(rcEdit.right - rcEdit.left) + (rcButton.right - rcButton.left), 
												rcEdit.bottom - rcEdit.top, 
												SWP_NOMOVE | SWP_NOOWNERZORDER);
		GetDlgItem(IDC_BTN_BROWSE)->ShowWindow(SW_HIDE);
		((CEdit*)GetDlgItem(IDC_EDIT_DATA))->SetReadOnly(false);
		GetDlgItem(IDC_BTN_CALCULATION)->EnableWindow(false);
		::DragAcceptFiles(m_hWnd, false);
	}
	else
	{
		GetDlgItem(IDC_EDIT_DATA)->SetWindowPos(NULL, 0, 0, 
													rcEdit.right - rcEdit.left, 
													rcEdit.bottom - rcEdit.top, 
													SWP_NOMOVE | SWP_NOOWNERZORDER);
		GetDlgItem(IDC_BTN_BROWSE)->ShowWindow(SW_SHOW);
		((CEdit*)GetDlgItem(IDC_EDIT_DATA))->SetReadOnly(true);
		GetDlgItem(IDC_BTN_CALCULATION)->EnableWindow(true);
		::DragAcceptFiles(m_hWnd, true);
	}

	GetDlgItem(IDC_COMBO_TYPE)->SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOSIZE );
}

void CSHAProjDlg::OnBtnAbout() 
{
	CAboutDlg dlg;
	dlg.DoModal();
}

void CSHAProjDlg::OnSelchangeComboType() 
{
	SetDlgItemText(IDC_EDIT_DATA, "");
	SetDlgItemText(IDC_EDIT_RESULT, "");

	if(0 == ((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->GetCurSel())
	{
		AdjustWindow(true); 
	}
	else if(1 == ((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->GetCurSel())
	{
		AdjustWindow(false);	
	}
}

void CSHAProjDlg::OnChangeEditData() 
{
	if(0 == ((CComboBox*)GetDlgItem(IDC_COMBO_TYPE))->GetCurSel())
		return ;

	CString strInput;

	GetDlgItemText(IDC_EDIT_DATA, strInput);

	if(strInput.IsEmpty())
	{
		SetDlgItemText(IDC_EDIT_RESULT, "");
		return ;
	}

	UpdateData();

	switch(m_nSHAType)
	{
	case 0:
		m_SHA.Init(SHAEx::BITS160);
		break;
	case 1:
		m_SHA.Init(SHAEx::BITS224);
		break;
	case 2:
		m_SHA.Init(SHAEx::BITS256);
		break;
	case 3:
		m_SHA.Init(SHAEx::BITS384);
		break;
	case 4:
		m_SHA.Init(SHAEx::BITS512);
		break;
	}

	CString strTmp, strResult;

	BOOL bUpper = ((CButton*)GetDlgItem(IDC_CHECK_UPPER))->GetCheck();

	unsigned char result[64] = { 0 };

	m_SHA.Update((unsigned char*)(LPCTSTR)strInput, strInput.GetLength());

	m_SHA.Finish(result);

	switch(m_nSHAType)
	{
		case 0:
		{
			for(int i = 0; i < 20; i++)
			{
				strTmp.Format(bUpper ? "%02X" : "%02x", result[i]);
				strResult += strTmp;
			}
			break;
		}
		case 1:
		{
			for(int i = 0; i < 28; i++)
			{
				strTmp.Format(bUpper ? "%02X" : "%02x", result[i]);
				strResult += strTmp;
			}
			break;
		}
		case 2:
		{
			for(int i = 0; i < 32; i++)
			{
				strTmp.Format(bUpper ? "%02X" : "%02x", result[i]);
				strResult += strTmp;
			}
			break;
		}
		case 3:
		{
			for(int i = 0; i < 48; i++)
			{
				strTmp.Format(bUpper ? "%02X" : "%02x", result[i]);
				strResult += strTmp;
			}
			break;
		}
		case 4:
		{
			for(int i = 0; i < 64; i++)
			{
				strTmp.Format(bUpper ? "%02X" : "%02x", result[i]);
				strResult += strTmp;
			}
			break;
		}
	}

	SetDlgItemText(IDC_EDIT_RESULT, strResult);
}

void CSHAProjDlg::OnRadioSha160() 
{
	OnChangeEditData() ;
}

void CSHAProjDlg::OnRadioSha224() 
{
	OnChangeEditData() ;
}

void CSHAProjDlg::OnRadioSha256() 
{
	OnChangeEditData() ;
}

void CSHAProjDlg::OnRadioSha384() 
{
	OnChangeEditData() ;
}

void CSHAProjDlg::OnRadioSha512() 
{
	OnChangeEditData() ;
}

void CSHAProjDlg::OnDropFiles(HDROP hDropInfo) 
{
	// TODO: Add your message handler code here and/or call default
	
	int nFiles = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, NULL);

	if(nFiles > 0)
	{
		char szFileName[MAX_PATH] = { 0 };
		DragQueryFile(hDropInfo, 0, szFileName, MAX_PATH);
		if(!(GetFileAttributes(szFileName) & FILE_ATTRIBUTE_DIRECTORY))
		{
			SetDlgItemText(IDC_EDIT_DATA, szFileName);
			SetDlgItemText(IDC_EDIT_RESULT, "");
		}
	}

	CDialog::OnDropFiles(hDropInfo);
}

void CSHAProjDlg::OnCheckUpper() 
{
	CString strInput;

	GetDlgItemText(IDC_EDIT_RESULT, strInput);

	if(strInput.IsEmpty())
		return ;

	if(((CButton*)GetDlgItem(IDC_CHECK_UPPER))->GetCheck())
		SetDlgItemText(IDC_EDIT_RESULT, CharUpper((LPSTR)(LPCTSTR)strInput));
	else
		SetDlgItemText(IDC_EDIT_RESULT, CharLower((LPSTR)(LPCTSTR)strInput));
}

void CSHAProjDlg::OnBtnBrowse() 
{
	vector<string> Files = m_FileDlg.GetOpenFileName(m_hWnd);

	if(Files.size() > 0)
	{
		SetDlgItemText(IDC_EDIT_DATA, Files[0].c_str());

		SetDlgItemText(IDC_EDIT_RESULT, "");
	}
}

void CSHAProjDlg::OnBtnCalculation() 
{
	CString strInput;

	GetDlgItemText(IDC_EDIT_DATA, strInput);

	if(strInput.IsEmpty())
	{
		AfxMessageBox("请选择待计算文件!");

		return ;
	}

	SetDlgItemText(IDC_EDIT_RESULT, "");

	void* p = m_Thunk.Stdcall(this, Thunk::GetMemberFxnAddr(&CSHAProjDlg::ThreadProc));

	HANDLE hThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)p, 0, 0, 0);
	
	CloseHandle(hThread);
}

void CSHAProjDlg::ShowError()
{
	LPVOID lpMsgBuf;
	FormatMessage(	FORMAT_MESSAGE_ALLOCATE_BUFFER | 
					FORMAT_MESSAGE_FROM_SYSTEM | 
					FORMAT_MESSAGE_IGNORE_INSERTS,
					NULL,
					GetLastError(),
					MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
					(LPTSTR) &lpMsgBuf,
					0,
					NULL );
	AfxMessageBox((char*)lpMsgBuf);
	LocalFree( lpMsgBuf );
}

DWORD CSHAProjDlg::ThreadProc(LPVOID lpParameter)
{
	// 取输入文件名

	CString strInput;

	GetDlgItemText(IDC_EDIT_DATA, strInput);

	if(strInput.IsEmpty())
	{
		AfxMessageBox("请选择待计算文件!");

		return FALSE;
	}

	m_Pos.SetPos(0);

	::EnableWindow(GetDlgItem(IDC_BTN_CALCULATION)->m_hWnd, FALSE);
	
	GetDlgItem(IDC_PROGRESS)->ShowWindow(SW_SHOW);
	
	GetDlgItem(IDC_CHECK_UPPER)->ShowWindow(SW_HIDE);

	// 打开输入文件

	HANDLE hFile = CreateFile((LPSTR)(LPCTSTR)strInput, GENERIC_READ,
		FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);

	if(NULL == hFile)
	{
		ShowError();

		m_Pos.SetPos(0);

		::EnableWindow(GetDlgItem(IDC_BTN_CALCULATION)->m_hWnd, TRUE);
		
		GetDlgItem(IDC_PROGRESS)->ShowWindow(SW_HIDE);
		
		GetDlgItem(IDC_CHECK_UPPER)->ShowWindow(SW_SHOW);

		return FALSE;
	}

	// 创建映射对像

	HANDLE hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);

	if(NULL == hFileMapping)
	{
		ShowError();

		m_Pos.SetPos(0);

		::EnableWindow(GetDlgItem(IDC_BTN_CALCULATION)->m_hWnd, TRUE);
		
		GetDlgItem(IDC_PROGRESS)->ShowWindow(SW_HIDE);
		
		GetDlgItem(IDC_CHECK_UPPER)->ShowWindow(SW_SHOW);

		return FALSE;
	}

	// 取文件大小

	DWORD dwFileSizeHigh;
	__int64 qwFileSize = GetFileSize(hFile, &dwFileSizeHigh);
	qwFileSize += (((__int64) dwFileSizeHigh) << 32);

	// 文件句柄不再需要, 关闭之

	CloseHandle(hFile);

	int nBits = 0;
	if(((CButton*)GetDlgItem(IDC_RADIO_SHA160))->GetCheck())
	{
		m_SHA.Init(SHAEx::BITS160);
		nBits = 160;
	}
	else if(((CButton*)GetDlgItem(IDC_RADIO_SHA224))->GetCheck())
	{
		m_SHA.Init(SHAEx::BITS224);
		nBits = 224;
	}
	else if(((CButton*)GetDlgItem(IDC_RADIO_SHA256))->GetCheck())
	{
		m_SHA.Init(SHAEx::BITS256);
		nBits = 256;
	}
	else if(((CButton*)GetDlgItem(IDC_RADIO_SHA384))->GetCheck())
	{
		m_SHA.Init(SHAEx::BITS384);
		nBits = 384;
	}
	else if(((CButton*)GetDlgItem(IDC_RADIO_SHA512))->GetCheck())
	{
		m_SHA.Init(SHAEx::BITS512);
		nBits = 512;
	}

	// 系统粒度

	SYSTEM_INFO sinf;
	GetSystemInfo(&sinf);

	// 文件偏移

	__int64 qwFileOffset = 0;

	while (qwFileSize > 0) 
	{
		// Determine the number of bytes to be mapped in this view
		DWORD dwBytesInBlock = sinf.dwAllocationGranularity;
		
		if(qwFileSize < sinf.dwAllocationGranularity)
			dwBytesInBlock =(DWORD) qwFileSize;
		
		PBYTE pbFile = (PBYTE) MapViewOfFile(hFileMapping, FILE_MAP_READ,
			(DWORD)(qwFileOffset >> 32),          // Starting byte
			(DWORD)(qwFileOffset & 0xFFFFFFFF),   // in file
			dwBytesInBlock);                      // # of bytes to map

		m_SHA.Update(pbFile, dwBytesInBlock);

		if((int)(qwFileSize / 100) > 0)
			m_Pos.SetPos((int)(qwFileOffset / (qwFileSize / 100)));

		// Unmap the view; we don't want multiple views
		// in our address space.
		UnmapViewOfFile(pbFile);
		
		// Skip to the next set of bytes in the file.
		qwFileOffset += dwBytesInBlock;
		qwFileSize -= dwBytesInBlock;
	}

	// 关闭映射对像

	CloseHandle(hFileMapping);

	// 进度百分百

	m_Pos.SetPos(100);

	// 取结果

	unsigned char result[64] = { 0 };

	m_SHA.Finish(result);

	BOOL bUpper = ((CButton*)GetDlgItem(IDC_CHECK_UPPER))->GetCheck();

	CString strTmp, strResult;

	for(int i = 0; i < nBits / 8; i++)
	{
		strTmp.Format(bUpper ? "%02X" : "%02x", result[i]);
		strResult += strTmp;
	}

	// 显示结果

	SetDlgItemText(IDC_EDIT_RESULT, strResult);

	// 提示用户

	AfxMessageBox("计算完成!");

	::EnableWindow(GetDlgItem(IDC_BTN_CALCULATION)->m_hWnd, TRUE);
	
	GetDlgItem(IDC_PROGRESS)->ShowWindow(SW_HIDE);
	
	GetDlgItem(IDC_CHECK_UPPER)->ShowWindow(SW_SHOW);

	return TRUE;
}

⌨️ 快捷键说明

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