Re: Hash method

From: Manuel Meitinger (csaf2817_at_uibk.ac.at)
Date: 11/24/04


Date: Wed, 24 Nov 2004 18:38:25 +0100

Here's how you do a hashing with the Crypt* functions. (The example is taken
from Pocket PC 2003 Help)
You might, however, want to call CryptAcquireContext with
CRYPT_VERIFYCONTEXT, if you don't need private user keys.

#include <wincrypt.h>

HCRYPTPROV hProv = 0;
HCRYPTHASH hHash = 0;
BYTE *pbHash = NULL;
DWORD dwHashLen;
#define BUFFER_SIZE 256
BYTE pbBuffer[BUFFER_SIZE];
DWORD dwCount;
DWORD i;

// Get a handle to the default provider.
if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) {
 printf("Error %x during CryptAcquireContext!\n", GetLastError());
 goto done;
}
// Create a hash object.
if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
 printf("Error %x during CryptBeginHash!\n", GetLastError());
 goto done;
}

// Fill the buffer with test data.
for(i = 0 ; i < BUFFER_SIZE ; i++) {
 pbBuffer[i] = (BYTE)i;
}

// Put the hash in buffer.
if(!CryptHashData(hHash, pbBuffer, BUFFER_SIZE, 0)) {
 printf("Error %x during CryptHashData!\n", GetLastError());
 goto done;
}

// Read the hash value size and allocate memory.
dwCount = sizeof(DWORD);
if(!CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE *)&dwHashLen,
 &dwCount, 0)) {
 printf("Error %x during reading hash size!\n", GetLastError());
 goto done;
}
if((pbHash = malloc(dwHashLen)) == NULL) {
 printf("Out of memory!\n");
 goto done;
}

// Read the hash value.
if(!CryptGetHashParam(hHash, HP_HASHVAL, pbHash, &dwHashLen, 0)) {
 printf("Error %x during reading hash value!\n", GetLastError());
 goto done;
}

// Print the hash value.
for(i = 0 ; i < dwHashLen ; i++) {
 printf("%2.2x ",pbHash[i]);
}
printf("\n");

done:

// Free memory.
if(pbHash !=NULL) free(pbHash);

// Destroy the hash object.
if(hHash) CryptDestroyHash(hHash);
// Release the CSP handle.
if(hProv) CryptReleaseContext(hProv,0);

"F" <anonymous@discussions.microsoft.com> schrieb im Newsbeitrag
news:8eb001c4d229$f5180260$a601280a@phx.gbl...
>
>>> Is there a method to make a hash (MD5, SHA1 or
> whatever)
>>> from a string on Pocket PC?
>
>>>> Use the Crypt* functions (wincrypt.h)
>
> But to Crypt there is probably Decrypt. Isn't it?
> MD5 for Pocket PC: http://www.flowgroup.fr/tech_md5_us.htm
>
> Greets



Relevant Pages