实验:大文件MD5执行时间

// OpenSslAppFrame.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "OpenSslAppFrame.h" #include //OpenSSL版本0.9.8a, 换成别的版本需要更换 / //<../../../bin中的Dll> / //<../../../inc中的.h> / //<../../../lib中的.lib> //头缺的加在这里 #include #include #include #include //库全了,就这2个 #pragma comment(lib, "libeay32.lib") #pragma comment(lib, "ssleay32.lib") #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // The one and only application object CWinApp theApp; using namespace std; void OpenSslBegin();//OpenSSL初始化 void OpenSslEnd();//OpenSSL反初始化 int fnOpenSSLProcess();//OpenSSL的框架函数 int fnMyTestOnOpenSSL();//我们具体的OpenSSL代码写在这里 int fnSha1Demo();//Sha1演示 int fnBigFileMd5Test();//测试文件Md5的时间性能 //tools int FnPrintBuffer(PCHAR pcPrefix, PBYTE pbcBuffer, DWORD dwLenBuffer); static DWORD dwTimeBegin = 0; static DWORD dwTimeEnd = 0; void TimeLogBegin(); void TimeLogEnd(); void showTimeLog(); int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs cerr << _T("Fatal Error: MFC initialization failed") << endl; nRetCode = 1; } else { // TODO: code your application's behavior here. CString strHello; //strHello.LoadString(IDS_HELLO); strHello = "程序开始执行/n"; cout << (LPCTSTR)strHello << endl; fnOpenSSLProcess(); } printf("/n程序执行完毕, 按下任意键结束程序/n"); getchar(); return nRetCode; } void OpenSslBegin() { CRYPTO_malloc_init(); ERR_load_crypto_strings(); OpenSSL_add_all_algorithms(); ENGINE_load_builtin_engines(); } void OpenSslEnd() { CONF_modules_unload(1); EVP_cleanup(); ENGINE_cleanup(); CRYPTO_cleanup_all_ex_data(); ERR_remove_state(0); ERR_free_strings(); } int fnOpenSSLProcess() { OpenSslBegin(); fnMyTestOnOpenSSL(); OpenSslEnd(); return S_OK; } int fnMyTestOnOpenSSL() { //return fnSha1Demo();//假设任务是做sha1摘要 return fnBigFileMd5Test(); } int fnBigFileMd5Test() {//测试文件Md5的时间性能 PBYTE pBufFile = NULL; DWORD dwLenFile = 0; //把大文件读入Buffer printf(">>读入'f://BigFile.dat'/n"); TimeLogBegin(); if(1) { CFile file; if(!file.Open("f://BigFile.dat", CFile::modeReadWrite | CFile::typeBinary)) return S_FALSE; dwLenFile = file.GetLength(); printf("文件size = %d/n", dwLenFile); pBufFile = new BYTE[dwLenFile]; _ASSERT(pBufFile); UINT uNeedRdBack = dwLenFile; UINT uRdBack = 1; DWORD dwOffsetFileRd = 0; while(uRdBack > 0) { //开发机配置, 2G内存, XpSp3 //1G的文件, file.Read能一次搞定 uRdBack = file.Read(pBufFile + dwOffsetFileRd, uNeedRdBack); if(uRdBack == uNeedRdBack) break;//一次搞定, 读了几秒的样子 else { dwOffsetFileRd = uRdBack; uNeedRdBack -= uRdBack; } } file.Close(); } TimeLogEnd(); showTimeLog(); //计算整个文件做MD5的时间 printf(">>计算MD5'f://BigFile.dat/n'"); TimeLogBegin(); if(1) { BYTE Md5Out[MD5_LBLOCK]; memset(Md5Out, 0, MD5_LBLOCK); PBYTE pBufMd5Out = NULL; pBufMd5Out = MD5(pBufFile, dwLenFile, Md5Out); if(pBufMd5Out) { FnPrintBuffer("Md5计算成功", Md5Out, MD5_LBLOCK); } else { printf("Md5失败/n"); } } TimeLogEnd(); showTimeLog(); //计算文件分成1M一块做MD5的时间 if(pBufFile) { delete pBufFile; pBufFile = NULL; } return S_OK; } int fnSha1Demo() { int n = 0; const int const nLenMd = 20;//sha1摘要长度总是20 PBYTE md = new BYTE[nLenMd]; memset(md, 0, nLenMd); PCHAR const pMsgIn = "/'做摘要的数据可以是二进制的流, 这里用明文代替./'"; DWORD dwSizeSha1Src = strlen(pMsgIn); PBYTE pSha1Src = new BYTE[dwSizeSha1Src + 1]; _ASSERT(pSha1Src); pSha1Src[dwSizeSha1Src] = '/0'; memcpy(pSha1Src, pMsgIn, dwSizeSha1Src); //unsigned char *SHA1(const unsigned char *d, size_t n, unsigned char *md); SHA1(pSha1Src, dwSizeSha1Src, md); printf("原文:[%s]/n", pSha1Src); printf("原文Sha1摘要:["); for(n = 0; n < nLenMd; n++) { printf("%.2x", md[n]); if(n != (nLenMd - 1)) printf(" "); } printf("]/n"); delete pSha1Src; pSha1Src = NULL; return S_OK; } int FnPrintBuffer(PCHAR pcPrefix, PBYTE pbcBuffer, DWORD dwLenBuffer) { printf("//----------------------------------------/n"); printf("//提示信息:%s/n", pcPrefix); printf("//字节流长度:%d/n", dwLenBuffer); printf("//----------------------------------------/n"); ULONG i,j,m; char buffer[0x100] = "/0"; char cBufTmp[8] = "/0"; strcpy(buffer,""); m = dwLenBuffer%16; //打印整行字符 for(i = 0; i < (dwLenBuffer -m); i++) { //格式化16进制显示部分 sprintf(cBufTmp, "%.2x", pbcBuffer[i]); _strupr(cBufTmp); strcat(buffer, cBufTmp); //每16个字符后面, 进行文本显示 if (((i + 1) % 16) == 0) { strcat(buffer, " ");//隔断(16进制显示和文本显示) for(j = (i - 15); j < (i + 1); j++) { sprintf(cBufTmp, "%c", isprint(pbcBuffer[j]) ? pbcBuffer[j] : '.' ); strcat(buffer,cBufTmp); } printf("%s/n", buffer); strcpy(buffer,""); } } if(m == 0) { return S_OK; } //打印最后一行(不满16个字符) strcpy(buffer, ""); for(i = dwLenBuffer - m; i < dwLenBuffer; i++) { sprintf(cBufTmp, "%.2x", pbcBuffer[i]); _strupr(cBufTmp); strcat(buffer, cBufTmp); } //正文补齐 for(i = 0; i < (16 - m); i++) { strcat(buffer," "); } strcat(buffer, " ");//隔断 for(j = (dwLenBuffer - m); j < dwLenBuffer; j++) { sprintf(cBufTmp, "%c", isprint(pbcBuffer[j]) ? pbcBuffer[j] : '.' ); strcat(buffer, cBufTmp); } strcat(buffer,"/n"); printf(buffer); return S_OK; } void TimeLogBegin() { dwTimeBegin = ::GetTickCount(); } void TimeLogEnd() { dwTimeEnd = ::GetTickCount(); } void showTimeLog() { /** The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days. */ printf("showTimeUsed: [%d]~[ %d]=[%d mS]/n", dwTimeBegin, dwTimeEnd, (dwTimeEnd - dwTimeBegin)); }

你可能感兴趣的:(buffer,application,initialization,byte,mfc,delete)