CryptHashMessage

The CryptHashMessage function creates a hash of the message.

#include <wincrypt.h>
BOOL WINAPI CryptHashMessage(
  PCRYPT_HASH_MESSAGE_PARA pHashPara, // in
  BOOL fDetachedHash,                 // in
  DWORD cToBeHashed,                  // in
  const BYTE *rgpbToBeHashed[ ],      // in
  DWORD rgcbToBeHashed[ ],            // in
  BYTE *pbHashedBlob,                 // out, optional
  DWORD *pcbHashedBlob,               // in/out, optional
  BYTE *pbComputedHash,               // out, optional
  DWORD *pcbComputedHash              // in/out, optional
);
 

Parameters

pHashPara
Pointer to the hash parameters. For details, see Simplified Message Data Structures.
fDetachedHash
If this parameter is set to TRUE, only pbComputedHash is encoded in pbHashedBlob. Otherwise, both rgpbToBeHashed and pbComputedHash are encoded.
cToBeHashed
Number of array elements in rgpbToBeHashed[] and rgcbToBeHashed[]. This parameter can only be one (1) unless fDetachedHash is set to TRUE.
rgpbToBeHashed[]
Array of pointers that point to the contents to be hashed.
rgcbToBeHashed[]
Array of sizes, in bytes, for the contents pointed to by rgpbToBeHashed[].
pbHashedBlob
Optional parameter. Pointer to a buffer that receives the hashed message (the hash, and if fDetachedHash is TRUE, the content that was hashed, which has been encoded for transmission).

This parameter can be NULL if the hashed message is not needed for additional processing, or to set the size of the hashed message for memory allocation purposes. For more information, see Common In/Out Parameter Conventions.

pcbHashedBlob
Optional parameter. Pointer to a variable that specifies the size, in bytes, of the buffer pointed to by the pbHashedBlob parameter. When the function returns, this variable contains the size, in bytes, of the decrypted message copied to *pbHashedBlob. A hashed message will not be returned if this parameter 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.

pbComputedHash
Optional parameter. Pointer to a buffer that receives only the newly created, unencoded hash. This parameter can be NULL if the newly created hash is not needed for additional processing, or to set the size of the hash for memory allocation purposes. For more information, see Common In/Out Parameter Conventions.
pcbComputedHash
Optional parameter. Pointer to a variable that specifies the size, in bytes, of the buffer pointed to by the pbComputedHash parameter. When the function returns, this variable contains the size, in bytes, of the newly created hash that was copied to *pbComputedHash. A computed hash will not be returned if this parameter 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.

Return Values

TRUE if the function succeeded. FALSE if the function failed.

Call GetLastError to see the reason for any failures. Note that errors from the called functions CryptCreateHash, CryptHashData, and CryptGetHashParam may be propagated to this function. This function has the following error codes.

Error code Description
CRYPT_E_OSS_ERROR Message ASN.1 decoding error. Note, to get the OSS error subtract CRYPT_E_OSS_ERROR from the returned error and see asn1code.h for details on the error.
E_INVALIDARG Invalid message encoding type. Currently only PKCS_7_ASN_ENCODING is supported. Invalid cbSize in *pHashPara.
ERROR_MORE_DATA If the buffer specified by the pbHashedBlob 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 pcbHashedBlob.

Example

// EXAMPLE CODE FOR USING CryptHashMessage() to hash a single 
// message.
// The hash is not to be detached, and the computed hash is not
// to be returned.

// Assume that the application already knows the address of the
// message array (rgpbToBeHashed[]) and the size of each element
// of that array (rgcbToBeHashed[]).

// Set up the variables.
CRYPT_HASH_MESSAGE_PARA      HashPara; // Struct initialized elsewhere
const BYTE*                  rgpbToBeHashed[1];     
DWORD                        rgcbToBeHashed[1]; 
DWORD                        cbHashedBlob;

// Call CryptHashMessage() to get the size of the hashed and encoded message. 
BOOL        fReturn =            FALSE;

fReturn = CryptHashMessage(&HashPara, FALSE, 1, rgpbToBeHashed,    
          rgcbToBeHashed, NULL, &cbHashedBlob,
          NULL, NULL);
if(fReturn != TRUE)
    ;// Function call failed.  Handle the error.

// If the call succeeded, the size of the message, in bytes, 
// now resides in cbHashedBlob.

// Malloc memory for the size of the message.
BYTE* pbHashedBlob;

pbHashedBlob = (BYTE*)malloc(cbHashedBlob);
if(pbHashedBlob = NULL)
    ;// Handle the memory allocation error.

// Call CryptHashMessage to return the hashed and encoded message.

fReturn = CryptHashMessage(&HashPara, FALSE, 1, rgpbToBeHashed,    
          rgcbToBeHashed, pbHashedBlob, 
          &cbHashedBlob, NULL, NULL);
if(fReturn != TRUE)
    ;// Function call failed.  Handle the error.

// If the function succeeded, the hash and encoded message are now
// at the location pointed to by pbHashedBlob.

// Perform local processing on the message.
...

// Free memory.
free (pbHashedBlob);
 

QuickInfo

  Windows NT: Requires version 4.0 SP3 or later. Available also in IE 3.02 and later.
  Windows: Requires Windows 98 (or Windows 95 with IE 3.02 or later).
  Windows CE: Unsupported.
  Header: Declared in wincrypt.h.
  Import Library: Use crypt32.lib.

See Also

CryptVerifyDetachedMessageHash, CryptVerifyMessageHash