Expand Minimize

CryptHashData function

The CryptHashData function adds data to a specified hash object. This function and CryptHashSessionKey can be called multiple times to compute the hash of long or discontinuous data streams.

Before calling this function, CryptCreateHash must be called to create a handle of a hash object.

Syntax


BOOL WINAPI CryptHashData(
  _In_  HCRYPTHASH hHash,
  _In_  BYTE *pbData,
  _In_  DWORD dwDataLen,
  _In_  DWORD dwFlags
);

Parameters

hHash [in]

Handle of the hash object.

pbData [in]

A pointer to a buffer that contains the data to be added to the hash object.

dwDataLen [in]

Number of bytes of data to be added. This must be zero if the CRYPT_USERDATA flag is set.

dwFlags [in]

The following flag values are defined.

ValueMeaning
CRYPT_OWF_REPL_LM_HASH
0x00000001

This flag is not used.

CRYPT_USERDATA
1 (0x1)

All Microsoft Cryptographic Providers ignore this parameter. For any CSP that does not ignore this parameter, if this flag is set, the CSP prompts the user to input data directly. This data is added to the hash. The application is not allowed access to the data. This flag can be used to allow the user to enter a PIN into the system.

 

Return value

If the function succeeds, the return value is TRUE.

If the function fails, the return value is FALSE. For extended error information, call GetLastError.

The error codes prefaced by "NTE" are generated by the particular CSP you are using. Some possible error codes follow.

Return codeDescription
ERROR_INVALID_HANDLE

One of the parameters specifies a handle that is not valid.

ERROR_INVALID_PARAMETER

One of the parameters contains a value that is not valid. This is most often a pointer that is not valid.

NTE_BAD_ALGID

The hHash handle specifies an algorithm that this CSP does not support.

NTE_BAD_FLAGS

The dwFlags parameter contains a value that is not valid.

NTE_BAD_HASH

The hash object specified by the hHash parameter is not valid.

NTE_BAD_HASH_STATE

An attempt was made to add data to a hash object that is already marked "finished."

NTE_BAD_KEY

A keyed hash algorithm is being used, but the session key is no longer valid. This error is generated if the session key is destroyed before the hashing operation is complete.

NTE_BAD_LEN

The CSP does not ignore the CRYPT_USERDATA flag, the flag is set, and the dwDataLen parameter has a nonzero value.

NTE_BAD_UID

The CSP context that was specified when the hash object was created cannot be found.

NTE_FAIL

The function failed in some unexpected way.

NTE_NO_MEMORY

The CSP ran out of memory during the operation.

 

Examples

The following example shows adding data to a specified hash object already created by CryptCreateHash. For an example that includes the complete context for this example, see Example C Program: Signing a Hash and Verifying the Hash Signature. For another example that uses this function, see Example C Program: Deriving a Session Key from a Password.


//-------------------------------------------------------------
// Declare and initialize variables.

BYTE *pbBuffer= (BYTE *)"The data that is to be hashed and signed.";
DWORD dwBufferLen = strlen((char *)pbBuffer)+1;

//--------------------------------------------------------------------
//  This code assumes that the handle of a cryptographic context 
//  has been acquired and that a hash object has been created 
//  and its handle (hHash) is available.


if(CryptHashData(
   hHash, 
   pbBuffer, 
   dwBufferLen, 
   0)) 
{
     printf("The data buffer has been added to the hash.\n");
}
else
{
     printf("Error during CryptHashData.\n");
     exit(1);
}
//--------------------------------------------------------------------
//  At this point, the content of pbBuffer has been added to the hash.
//  Additional data can be added by repeatedly calling CryptHashData.
//  When the hash object is no longer needed, it should be destroyed.

if(hHash) 
  CryptDestroyHash(hHash);

Requirements

Minimum supported client

Windows XP [desktop apps only]

Minimum supported server

Windows Server 2003 [desktop apps only]

Header

Wincrypt.h

Library

Advapi32.lib

DLL

Advapi32.dll

See also

Hash and Digital Signature Functions
CryptCreateHash
CryptHashSessionKey
CryptSignHash

 

 

Community Additions

ADD
Show:
© 2014 Microsoft