http://blog.csdn.net/wangxijie/archive/2009/09/04/4520562.aspx
- #include <iostream>
- #include <vector>
- using namespace std;
- #include <windows.h>
- #include <wincrypt.h>
- #include <stdlib.h>
- #include <share.h>
- #include <tchar.h>
- #include <io.h>
- #define BOLCKSIZE 184320
- DWORD ComputeHash(CONST BYTE *pbData, DWORD dwDataLen, ALG_ID algId,
- BYTE *& pbOutHash, DWORD & dwHashLen, BOOL showDBInfo=FALSE);
- int main()
- {
- vector<BYTE *> hashVec;
- FILE *file=_tfsopen(_T( "protocol_4f_54.txt" ),_T( "rb" ),_SH_DENYNO);
- if (!file)
- {
- printf("open file fail!/n" );
- return -1;
- }
- int fileSize=_filelengthi64(file->_file);
- printf("file size:%d/n" ,fileSize);
- //设置缓冲区大小
- //setvbuf(file,NULL,_IOFBF,1024*8*2);
- BYTE * pbOutHash;
- DWORD dwHashLen;
- //char pbData[9728000]="wangxijie/0";
- char * pbData= new char [BOLCKSIZE];
- int dwDataLen=strlen(pbData);
- int togo=0;
- size_t realLen;
- while (togo<fileSize)
- {
- realLen=fread(pbData,1,BOLCKSIZE,file);
- ComputeHash((BYTE *)&pbData[0],realLen,CALG_MD5,pbOutHash,dwHashLen);
- hashVec.push_back(pbOutHash);
- for ( int i=0;i<dwHashLen;i++)
- printf("%02x " ,pbOutHash[i]);
- printf("/n%d/n" ,realLen);
- togo+=realLen;
- }
- BYTE * resHash= new BYTE [hashVec.size()*16];
- for ( int i=0;i<hashVec.size();i++)
- {
- memcpy(resHash+i*16,hashVec[i],16);
- delete [] hashVec[i];
- // for(int j=0;j<16;j++)
- // printf("%02x ",(resHash+i*16)[j]);
- // printf("/n");
- }
- ComputeHash((BYTE *)&resHash[0],hashVec.size()*16,CALG_MD5,pbOutHash,dwHashLen);
- for (i=0;i<dwHashLen;i++)
- printf("%x " ,pbOutHash[i]);
- printf("/n" );
- delete [] pbData;
- fclose(file);
- delete pbOutHash;
- return 0;
- }
- DWORD ComputeHash(CONST BYTE *pbData, DWORD dwDataLen, ALG_ID algId,
- BYTE *& pbOutHash, DWORD & dwHashLen, BOOL showDBInfo)
- {
- char UserName[10]= "wangxijie" ;
- HCRYPTHASH hHash={0};
- HCRYPTPROV hCryptProv;
- if (CryptAcquireContext(
- &hCryptProv, // 返回CSP句柄
- UserName, // 密码容器名
- NULL, // NULL时使用默认CSP名(微软RSA Base Provider)
- PROV_RSA_FULL, // CSP类型
- 0)) // Flag values
- {
- //以UserName为名的密钥容器存在,那么我们已经得到了CSP的句柄
- if (showDBInfo)
- {
- printf("A crypto context with the %s key container /n" , UserName);
- printf("has been acquired./n" );
- }
- }
- else //如果密钥容器不存在,我们需要创建这个密钥容器
- {
- if (CryptAcquireContext(
- &hCryptProv,
- UserName,
- NULL,
- PROV_RSA_FULL,
- CRYPT_NEWKEYSET)) //创建以UserName为名的密钥容器
- {
- //创建密钥容器成功,并得到CSP句柄
- if (showDBInfo)
- printf("A new key container has been created.%s/n" ,UserName);
- }
- else
- {
- if (showDBInfo)
- printf("Could not create a new key container./n" );
- }
- } // End of else
- if (CryptCreateHash(
- hCryptProv,
- algId,
- 0,
- 0,
- &hHash))
- {
- if (showDBInfo)
- printf("A hash object has been created. /n" );
- }
- else
- {
- if (showDBInfo)
- printf("Error during CryptCreateHash!/n" );
- }
- DWORD dwReturn;
- // char pbData[100]="wangxijie/0";
- // int dwDataLen=strlen(pbData);
- if (!CryptHashData(hHash, (byte*)&pbData[0], dwDataLen, 0))
- {
- dwReturn = GetLastError();
- CryptDestroyHash(hHash);
- CryptReleaseContext(hCryptProv, 0);
- return dwReturn;
- }
- //DWORD dwHashLen;
- DWORD dwLen = sizeof (dwHashLen);
- CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE *)(&dwHashLen), &dwLen, 0);
- //BYTE* pbOutHash;
- pbOutHash = new BYTE [dwHashLen];
- dwLen = dwHashLen;
- CryptGetHashParam(hHash, HP_HASHVAL, pbOutHash, &dwLen, 0);
- // for(int i=0;i<dwHashLen;i++)
- // printf("%x ",pbOutHash[i]);
- // printf("/n");
- //destory
- CryptDestroyHash(hHash);
- if (hCryptProv)
- CryptReleaseContext(hCryptProv,0);
- return 0;
- }