CryptGetHashParam

The CryptGetHashParam function retrieves data that governs the operations of a hash object. The actual hash value can also be retrieved by using this function.

#include <wincrypt.h>
BOOL WINAPI CryptGetHashParam(
  HCRYPTHASH hHash, // in
  DWORD dwParam,    // in
  BYTE *pbData,     // out
  DWORD *pcbData,   // in/out
  DWORD dwFlags     // in
);
 

Parameters

hHash
Handle to the hash object on which to query parameters.
dwParam
Parameter number. See the "Remarks" section for a list of valid parameters.
pbData
Pointer to a buffer that receives the specified parameter data. The form of this data will vary, depending on the parameter number.

This parameter can be NULL to set the size of this information for memory allocation purposes. For more information, see Common In/Out Parameter Conventions.

pcbData
Pointer to a variable that specifies the size, in bytes, of the buffer pointed to by the pbData parameter. When the function returns, the variable pointed to by the pcbData parameter contains the number of bytes stored in the buffer. This parameter can be NULL, only if pbData is NULL.

Note that when processing the data returned in the buffer, applications need to use the actual size of the data returned. The actual size may be slightly smaller than the size of the buffer specified on input. (On input, buffer sizes are usually specified large enough to insure that the largest possible output data will fit in the buffer.) On output, the variable pointed to by this parameter is updated to reflect the actual size of the data copied to the buffer.

dwFlags
Flag values. This parameter is reserved for future use and should always be zero.

Return Values

If the function succeeds, the return value is TRUE. If it fails, the return value is FALSE. To retrieve extended error information, use the GetLastError function.

The following table lists the error codes most commonly returned by the GetLastError function. The error codes prefaced by "NTE" are generated by the particular CSP you are using.

Error code Description
ERROR_INVALID_HANDLE One of the parameters specifies an invalid handle.
ERROR_INVALID_PARAMETER One of the parameters contains an invalid value. This is most often an illegal pointer.
ERROR_MORE_DATA If the buffer specified by the pbData parameter is not large enough to hold the returned data, the function sets the ERROR_MORE_DATA code, and stores the required buffer size, in bytes, into the variable pointed to by pcbData.
NTE_BAD_FLAGS The dwFlags parameter is nonzero.
NTE_BAD_HASH The hash object specified by the hHash parameter is invalid.
NTE_BAD_TYPE The dwParam parameter specifies an unknown parameter number.
NTE_BAD_UID The CSP context that was specified when the hash was created cannot be found.

Remarks

The dwParam value can be set to one of the following hash parameter types:

Note  Some CSPs may add additional parameters that can be queried through this function.

Example

#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);
 

Another example is located in Sender Code Example.

QuickInfo

  Windows NT: Requires version 4.0 or later.
  Windows: Requires Windows 95 OSR2 or later (or Windows 95 with IE 3.02 or later).
  Windows CE: Unsupported.
  Header: Declared in wincrypt.h.
  Import Library: Use advapi32.lib.

See Also

CryptCreateHash, CryptGetKeyParam, CryptHashData, CryptHashSessionKey, CryptSetHashParam