登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

沙漠里de烟雨

原创分享,禁止转载

 
 
 

日志

 
 

C++--MD5值的计算  

2012-10-16 19:21:49|  分类: C++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

.h文件如下:

#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;

typedef struct {
    unsigned char digest[16];
    unsigned long hHash;
} MD5Context;


BOOL CryptStartup();
void CryptCleanup();
void MD5Init(MD5Context *ctx);
void MD5Update(MD5Context *ctx, unsigned char const *buf, unsigned int len);
void MD5Final(MD5Context *ctx);
int CalculateMD5(CString FileName, CString &MD5);
int CalculateMD5(string FileName,string& MD5);

 

.cpp文件如下:

#include "stdafx.h"
#include "MD5.h"

HCRYPTPROV hCryptProv;

BOOL CryptStartup()
{
    if(CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == 0){
        if(GetLastError() == NTE_EXISTS){
            if(CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0) == 0)
                return FALSE;
        }else return FALSE;
    }
    return TRUE;
}

void CryptCleanup()
{
    if(hCryptProv) CryptReleaseContext(hCryptProv, 0);
    hCryptProv = 0;
}

void MD5Init(MD5Context *ctx)
{
    CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &ctx->hHash);
}


void MD5Update(MD5Context *ctx, unsigned char const *buf, unsigned int len)
{
    CryptHashData(ctx->hHash, buf, len, 0);
}

void MD5Final(MD5Context *ctx)
{
    DWORD dwCount = 16;
    CryptGetHashParam(ctx->hHash, HP_HASHVAL, ctx->digest, &dwCount, 0);
    if(ctx->hHash) CryptDestroyHash(ctx->hHash);
    ctx->hHash = 0;
}


int CalculateMD5(CString FileName, CString& MD5)
{
 const size_t StringSize = FileName.GetLength() + 1; //
 size_t CharactersConverted = 0;

 char *file = new char[StringSize];
 //char file[1024];
 wcstombs_s(&CharactersConverted, file, FileName.GetLength()+1, FileName, _TRUNCATE);//


 int i, j;
    FILE *fInput;
    MD5Context md5Hash;
    unsigned char bBuffer[4096];
    unsigned char b;
    char c;
 
 
   
    if(!CryptStartup())
 {
        MessageBoxW(0, L"Could not start crypto library", L"MD5", MB_ICONERROR);
        return 0;
    }
   
    fInput = fopen(file, "rb");
    if(!fInput)
 {
       MessageBoxW(0, L"Failed to open - Invaild File", L"MD5", MB_ICONERROR);
        CryptCleanup();
        return 0;
    }
   
    memset(&md5Hash, 0, sizeof(MD5Context));
    MD5Init(&md5Hash);
    while(!feof(fInput)){
        unsigned int nCount = fread(bBuffer, sizeof(unsigned char), 4096, fInput);
        MD5Update(&md5Hash, bBuffer, nCount);
    }
    MD5Final(&md5Hash);
   
    fclose(fInput);
    //printf("\nChecksum of '%s' is: ", argv[1]);
 char *Value = new char[1024];int k = 0;
    for(i = 0; i < 16; i++)
 {
        b = md5Hash.digest[i];
        for(j = 4; j >= 0; j -= 4)
  {
            c = ((char)(b >> j) & 0x0F);
            if(c < 10) c += '0';
            else c = ('a' + (c - 10));
            //printf("%c", c);
   Value[k] = c;
   k++;
        }
    }
 Value[k] = '\0';
    CryptCleanup();
 

 MD5 = CString(Value);////

 delete file;
 delete Value;
 return 1;

}
int CalculateMD5(string FileName,string& MD5)
{
    const size_t StringSize = FileName.length() + 1; //
 size_t CharactersConverted = 0;

 char *file = new char[StringSize];
 //char file[1024];
 wcstombs_s(&CharactersConverted, file, FileName.length()+1, CString(FileName.c_str()), _TRUNCATE);//


 int i, j;
    FILE *fInput;
    MD5Context md5Hash;
    unsigned char bBuffer[4096];
    unsigned char b;
    char c;
 
   
    if(!CryptStartup())
 {
        MessageBoxW(0, L"Could not start crypto library", L"MD5", MB_ICONERROR);
        return 0;
    }
   
    fInput = fopen(file, "rb");
    if(!fInput)
 {
       MessageBoxW(0, L"Failed to open - Invaild File", L"MD5", MB_ICONERROR);
        CryptCleanup();
        return 0;
    }
   
    memset(&md5Hash, 0, sizeof(MD5Context));
    MD5Init(&md5Hash);
    while(!feof(fInput)){
        unsigned int nCount = fread(bBuffer, sizeof(unsigned char), 4096, fInput);
        MD5Update(&md5Hash, bBuffer, nCount);
    }
    MD5Final(&md5Hash);
   
    fclose(fInput);
    //printf("\nChecksum of '%s' is: ", argv[1]);
 char *Value = new char[1024];int k = 0;
    for(i = 0; i < 16; i++)
 {
        b = md5Hash.digest[i];
        for(j = 4; j >= 0; j -= 4)
  {
            c = ((char)(b >> j) & 0x0F);
            if(c < 10) c += '0';
            else c = ('a' + (c - 10));
            //printf("%c", c);
   Value[k] = c;
   k++;
        }
    }
 Value[k] = '\0';
    CryptCleanup();
 

 MD5 = string(Value);////

 delete file;
 delete Value;
 return 1;

}

  评论这张
 
阅读(1267)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018