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

📄 verifileview.cpp

📁 HMAC即带密钥的HASH函数
💻 CPP
字号:
// VeriFileView.cpp : implementation of the CVeriFileView class
// Programmed by Gamsn 2005.10.22

#include "stdafx.h"
#include "VeriFile.h"

#include "VeriFileDoc.h"
#include "VeriFileView.h"

#include "InputKey.h"

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

bool g_bIsStop=0;
bool g_bIsPro=0;
/////////////////////////////////////////////////////////////////////////////
// CVeriFileView

IMPLEMENT_DYNCREATE(CVeriFileView, CFormView)

BEGIN_MESSAGE_MAP(CVeriFileView, CFormView)
	//{{AFX_MSG_MAP(CVeriFileView)
	ON_COMMAND(ID_M_OPEN, OnMOpen)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_CHECK_READONLY, OnCheckReadonly)
	ON_BN_CLICKED(IDC_CHECK_HIDDEN, OnCheckHidden)
	ON_BN_CLICKED(IDC_CHECK_SYS, OnCheckSys)
	ON_BN_CLICKED(IDC_CHECK_ARCHIVE, OnCheckArchive)
	ON_COMMAND(ID_M_VERI, OnMVeri)
	ON_WM_DROPFILES()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVeriFileView construction/destruction

CVeriFileView::CVeriFileView()
	: CFormView(CVeriFileView::IDD)
{
	//{{AFX_DATA_INIT(CVeriFileView)
	m_strFileName = _T("");
	m_nFileSize = 0;
	m_strCreatDate = _T("");
	m_strLastModiDate = _T("");
	m_strLastAccessDate = _T("");
	m_strHMac = _T("");
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CVeriFileView::~CVeriFileView()
{
}

void CVeriFileView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVeriFileView)
	DDX_Control(pDX, IDC_CHECK_ARCHIVE, m_chk_archive);
	DDX_Control(pDX, IDC_CHECK_HIDDEN, m_chk_hidden);
	DDX_Control(pDX, IDC_CHECK_READONLY, m_chk_readonly);
	DDX_Control(pDX, IDC_CHECK_SYS, m_chk_sys);
	DDX_Text(pDX, IDC_EDIT_FILENAME, m_strFileName);
	DDX_Text(pDX, IDC_EDIT_FILESIZE, m_nFileSize);
	DDX_Text(pDX, IDC_EDIT_CREATDATE, m_strCreatDate);
	DDX_Text(pDX, IDC_EDIT_LASTMODIDATE, m_strLastModiDate);
	DDX_Text(pDX, IDC_EDIT_LASTACCESSDATE, m_strLastAccessDate);
	DDX_Text(pDX, IDC_EDIT_HMAC, m_strHMac);
	//}}AFX_DATA_MAP
}

BOOL CVeriFileView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CVeriFileView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	pDoc=GetDocument();
	m_Mac.m_nArchive=pDoc->m_nArchive;
	m_Mac.m_nFileSize=pDoc->m_nFileSize;
	m_Mac.m_nHidden=pDoc->m_nHidden;
	m_Mac.m_nReadOnly=pDoc->m_nReadOnly;
	m_Mac.m_nSys=pDoc->m_nSys;
	m_Mac.m_strFileCreateDate=pDoc->m_strFileCreateDate;
	m_Mac.m_strFileLastModiDate=pDoc->m_strFileLastModiDate;
	m_Mac.m_strFileLastAccessDate=pDoc->m_strFileLastAccessDate;
	m_Mac.m_strFileName=pDoc->m_strFileName;
	m_Mac.m_strMac=pDoc->m_strMac;
	pDoc->m_pMac=&m_Mac;
	ShowMsg();
}

/////////////////////////////////////////////////////////////////////////////
// CVeriFileView diagnostics

#ifdef _DEBUG
void CVeriFileView::AssertValid() const
{
	CFormView::AssertValid();
}

void CVeriFileView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CVeriFileDoc* CVeriFileView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CVeriFileDoc)));
	return (CVeriFileDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CVeriFileView message handlers
UINT ThreadsProc(LPVOID pParam) 
{
	CWnd *pMainWnd=AfxGetMainWnd();
	if(pParam==NULL)
	{
		::MessageBox(pMainWnd->m_hWnd,"系统参数传递错误!","AESer",MB_OK|MB_ICONEXCLAMATION);
		AfxEndThread(0);
	}
	CVeriFileView *pDlg=(CVeriFileView *)pParam;
	pDlg->m_HMac.PreCalcu(pDlg->m_strKey);
	pDlg->m_HMac.GenMac(pDlg->m_Mac.m_strFileName,&(pDlg->m_Mac.m_strMac));
	g_bIsStop=1;
	g_bIsPro=0;
	return 0;
}

void CVeriFileView::OnMOpen() 
{
	if(g_bIsPro) return;
	CFileDialog dlg(1,NULL,NULL,OFN_ENABLESIZING|OFN_EXPLORER,"全部 (*.*)|*.*||",this);
	dlg.m_ofn.lpstrTitle="请选择要被校验的文件";
	if(dlg.DoModal()==IDOK)
	{
		CString strSrcPath=dlg.GetPathName();
		CreateVeri(strSrcPath);
	}
}

void CVeriFileView::ShowMsg()
{
	if(m_Mac.m_nSys==1) m_chk_sys.SetCheck(1);
	else m_chk_sys.SetCheck(0);
	if(m_Mac.m_nReadOnly==1) m_chk_readonly.SetCheck(1);
	else m_chk_readonly.SetCheck(0);
	if(m_Mac.m_nHidden==1) m_chk_hidden.SetCheck(1);
	else m_chk_hidden.SetCheck(0);
	if(m_Mac.m_nArchive==1) m_chk_archive.SetCheck(1);
	else m_chk_archive.SetCheck(0);
	m_nFileSize=m_Mac.m_nFileSize;
	m_strLastModiDate=m_Mac.m_strFileLastModiDate;
	m_strCreatDate=m_Mac.m_strFileCreateDate;
	m_strLastAccessDate=m_Mac.m_strFileLastAccessDate;
	m_strFileName=m_Mac.m_strFileName;
	m_strHMac=m_Mac.m_strMac;
	UpdateData(0);
}

void CVeriFileView::OnTimer(UINT nIDEvent) 
{
	if(nIDEvent==0)
	{
		if(g_bIsStop)
		{
			KillTimer(0);
			ShowMsg();
		}
	}
	else if(nIDEvent==1)
	{
		if(g_bIsStop)
		{
			KillTimer(1);
			UpdateData(1);
			if(m_Mac.m_strMac!=m_strHMac)
			{
				MessageBox("您的文件没有通过校验,\n\n该文件很可能被非法修改,或是您输入的校验密钥不对,\n\n或是您的校验文件被非法修改过。","VERIFILE",MB_OK|MB_ICONINFORMATION);
				Restore();
			}
			else MessageBox("恭喜!您的文件通过校验,它未经非法修改过。","VERIFILE",MB_OK|MB_ICONINFORMATION);
		}
	}
	if(nIDEvent==2) 
	{
		if(!g_bIsPro) 
		{
			m_WaitDlg->DestroyWindow();
			KillTimer(2);
		}
	}
	CFormView::OnTimer(nIDEvent);
}

void CVeriFileView::OnCheckReadonly() 
{
	if(m_chk_readonly.GetCheck()==1) m_chk_readonly.SetCheck(0);
	else m_chk_readonly.SetCheck(1);
}

void CVeriFileView::OnCheckHidden() 
{
	if(m_chk_hidden.GetCheck()==1) m_chk_hidden.SetCheck(0);
	else m_chk_hidden.SetCheck(1);		
}

void CVeriFileView::OnCheckSys() 
{
	if(m_chk_sys.GetCheck()==1) m_chk_sys.SetCheck(0);
	else m_chk_sys.SetCheck(1);	
}

void CVeriFileView::OnCheckArchive() 
{
	if(m_chk_archive.GetCheck()==1) m_chk_archive.SetCheck(0);
	else m_chk_archive.SetCheck(1);	
}

void CVeriFileView::OnMVeri() 
{
	if(g_bIsPro) return;
	if(m_Mac.m_strMac.IsEmpty())
	{
		MessageBox("当前没有有效的校验文件,请先打开校验文件,再选择相应的源文件进行校验",
			"VERIFILE",MB_OK|MB_ICONEXCLAMATION);
	}
	else
	{
		CFileDialog dlg(1,NULL,NULL,OFN_ENABLESIZING|OFN_EXPLORER,"全部 (*.*)|*.*||",this);
		dlg.m_ofn.lpstrTitle="请选择要被校验的文件";
		if(dlg.DoModal()==IDOK)
		{
			CString strSrcPath=dlg.GetPathName();
			m_Mac.m_strFileName=strSrcPath;
			CInputKey Dlg;
			if(Dlg.DoModal()==IDOK)
			{
				m_strKey=Dlg.m_strKey;
				m_Mac.m_strMac.Empty();
				g_bIsStop=0;
				g_bIsPro=1;
				SetTimer(1,1000,NULL);
				SetTimer(2,1000,NULL);

				CRect rect;
				GetWindowRect(rect);
				m_WaitDlg=new CWaitDlg;
				m_WaitDlg->Create(IDD_DIALOG_WAIT);
				m_WaitDlg->SetWindowPos(NULL,(rect.left+rect.right)/2-100,(rect.top+rect.bottom)/2-50,143,70,SWP_SHOWWINDOW);
				m_WaitDlg->ShowWindow(SW_SHOW);

				AfxBeginThread(&ThreadsProc,this);
			}
		}
	}	
}

void CVeriFileView::Restore()
{
	UpdateData(1);
	m_Mac.m_strFileName=m_strFileName;
	m_Mac.m_strMac=m_strHMac;
}

void CVeriFileView::OnDropFiles(HDROP hDropInfo) 
{
	CString strFileName;
	char buffer[255];
	int nRes=0,nAll=-1;
	nAll=DragQueryFile(hDropInfo,0xFFFFFFFF,buffer,10);
	if(nAll>=0)
	{
		nRes=DragQueryFile(hDropInfo,0,buffer,255);
		strFileName.Insert(0,buffer);
		if(strFileName.IsEmpty()) return;
		if(strFileName.Right(4)==".vri")
		{
			CFile f;
			CFileException e;
			f.Open(strFileName,CFile::modeRead|CFile::typeBinary,&e);
			if(e.m_cause!=CFileException::none) 
			{
				MessageBox("打开文件失败!","HMac",MB_OK|MB_ICONEXCLAMATION);
				return;
			}
			CArchive ar(&f,CArchive::load);
			ar>>m_Mac.m_nArchive;
			ar>>m_Mac.m_nHidden;
			ar>>m_Mac.m_nReadOnly;
			ar>>m_Mac.m_nSys;
			ar>>m_Mac.m_nFileSize;
			ar>>m_Mac.m_strFileCreateDate;
			ar>>m_Mac.m_strFileLastModiDate;
			ar>>m_Mac.m_strFileLastAccessDate;
			ar>>m_Mac.m_strFileName;
			ar>>m_Mac.m_strMac;
			ShowMsg();
		}
		else 
		{
			int nRes=MessageBox("请选择:\n\n是:生成该文件的校验文件;\n\n否:校验该文件;\n\n取消:什么也不做","VERIFILE",MB_YESNOCANCEL);
			if(nRes==IDYES) CreateVeri(strFileName);
			else if(nRes==IDNO) 
			{
				m_Mac.m_strFileName=strFileName;
				CInputKey Dlg;
				if(Dlg.DoModal()==IDOK)
				{
					m_strKey=Dlg.m_strKey;
					m_Mac.m_strMac.Empty();
					g_bIsStop=0;
					g_bIsPro=1;
					SetTimer(1,1000,NULL);
					SetTimer(2,1000,NULL);
					
					CRect rect;
					GetWindowRect(rect);
					m_WaitDlg=new CWaitDlg;
					m_WaitDlg->Create(IDD_DIALOG_WAIT);
					m_WaitDlg->SetWindowPos(NULL,(rect.left+rect.right)/2-100,(rect.top+rect.bottom)/2-50,143,70,SWP_SHOWWINDOW);
					m_WaitDlg->ShowWindow(SW_SHOW);
					
					AfxBeginThread(&ThreadsProc,this);
				}
			}
		}
	}	
	CFormView::OnDropFiles(hDropInfo);
}

void CVeriFileView::CreateVeri(CString strSrcPath)
{
	CFileStatus rStatus;
	if(CFile::GetStatus(strSrcPath,rStatus))
	{
		m_Mac.m_nFileSize=rStatus.m_size;
		m_Mac.m_strFileName=strSrcPath;
		m_Mac.m_strFileCreateDate=rStatus.m_ctime.Format("%Y-%m-%d %H:%M:%S");
		m_Mac.m_strFileLastModiDate=rStatus.m_mtime.Format("%Y-%m-%d %H:%M:%S");
		m_Mac.m_strFileLastAccessDate=rStatus.m_atime.Format("%Y-%m-%d %H:%M:%S");
		if((rStatus.m_attribute & 0x01)==0x01) m_Mac.m_nReadOnly=1;
		else m_Mac.m_nReadOnly=0;
		if((rStatus.m_attribute & 0x02)==0x02) m_Mac.m_nHidden=1;
		else m_Mac.m_nHidden=0;
		if((rStatus.m_attribute & 0x20)==0x20) m_Mac.m_nArchive=1;
		else m_Mac.m_nArchive=0;
		if((rStatus.m_attribute & 0x04)==0x04) m_Mac.m_nSys=1;
		else m_Mac.m_nSys=0;
	}
	else
	{
		MessageBox("无法得到文相关属性,一定要生成校验文件吗?","VERIFILE",MB_YESNO|MB_ICONQUESTION);
	}
	CInputKey Dlg;
	if(Dlg.DoModal()==IDOK)
	{
		m_strKey=Dlg.m_strKey;
		g_bIsStop=0;
		g_bIsPro=1;
		SetTimer(0,1000,NULL);
		SetTimer(2,1000,NULL);
		m_Mac.m_strMac.Empty();
		pDoc->SetModifiedFlag(1);
		
		CRect rect;
		GetWindowRect(rect);
		m_WaitDlg=new CWaitDlg;
		m_WaitDlg->Create(IDD_DIALOG_WAIT);
		m_WaitDlg->SetWindowPos(NULL,(rect.left+rect.right)/2-100,(rect.top+rect.bottom)/2-50,143,70,SWP_SHOWWINDOW);
		m_WaitDlg->ShowWindow(SW_SHOW);
		
		AfxBeginThread(&ThreadsProc,this);
	}
}

⌨️ 快捷键说明

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