CryptDuplicateHash (Windows Embedded CE 6.0)

1/6/2010

This function makes an exact copy of a hash and the state the hash is in.

A hash can be created in a piece-by-piece way. This function can create separate hashes of two different contents that begin with the same content.

Syntax

BOOL WINAPI CryptDuplicateHash( 
  HCRYPTHASH hHash,
  DWORD* pdwReserved, 
  DWORD dwFlags, 
  HCRYPTHASH* phHash
);

Parameters

  • hHash
    [in] HCRYPTHASH handle to the hash to be duplicated.
  • pdwReserved
    [in] Reserved for future use and must be set to NULL.
  • dwFlags
    [in] Reserved for future use and must be set to zero.
  • phHash
    [out] Pointer to the HCRYPTHASH handle to the duplicated hash.

Return Value

TRUE indicates success. FALSE indicates failure. To get extended error information, call the GetLastError function.

The following table shows the common values for the GetLastError function. The error values prefaced by NTE are generated by the particular cryptographic service provider (CSP) you are using.

Value Description

ERROR_CALL_NOT_IMPLEMENTED

Because this is a new function, existing CSPs may not implement it. This error is returned if the CSP does not support this function.

ERROR_INVALID_PARAMETER

One of the parameters contains an invalid value. This is most often an illegal pointer.

NTE_BAD_HASH

The handle to the original hash is not valid.

Remarks

This function makes copies of hashes and the exact state of the hash. For example, a caller may want to generate two hashes, but both hashes have to start with common hashed data. A hash could be created, the common data hashed, a duplicate made with the CryptDuplicateHash function, and then the data unique to each hash would be hashed.

The CryptDestroyHash function must be called to destroy any hashes that are created with the CryptDuplicateHash function. Destroying the original hash does not cause the duplicate hash to be destroyed. Once a duplicate hash is made it is separate from the original hash. There is no shared state between the two hashes.

Example Code

HCRYPTPROV hProv = 0;
HCRYPTHASH hOriginalHash = 0;
HCRYPTHASH hDuplicateHash = 0;
DWORD dwErr;
// Create a hash.
if (!CryptCreateHash(hProv, CALG_SHA1, 0, &hOriginalHash))
 {printf("ERROR - CryptCreateHash: %X\n", GetLastError());
 return;}
// Hash common data.
if (!CryptHashData(hOriginalHash, (BYTE*)"Common Data", sizeof("Common Data"), 0))
 {printf("ERROR - CryptHashData: %X\n", GetLastError());
 return;}
// Duplicate the hash.
if (!CryptDuplicateHash(hOriginalHash, NULL, 0, &hDuplicateHash))
 {printf("ERROR - CryptDuplicateHash: %X\n", GetLastError());
 return;}
// Hash some data with the original hash.
if (!CryptHashData(hOriginalHash, (BYTE*)"Some Data", sizeof("Some Data"), 0))
 {printf("ERROR - CryptHashData: %X\n", GetLastError());
 return;}
// Hash other data with the duplicate hash.
if (!CryptHashData(hDuplicateHash, (BYTE*)"Other Data", sizeof("Other Data"), 0))
 {printf("ERROR - CryptHashData: %X\n", GetLastError());
 return;}
...
// Destroy the original hash.
if (!CryptDestroyHash (hOriginalHash))
 {printf("ERROR - CryptDestroyHash: %X\n", GetLastError());
 return;}
// Destroy the duplicate hash.
if (!CryptDestroyHash(hDuplicateHash))
 {printf("ERROR - CryptDestroyHash: %X\n", GetLastError());
 return;}

Requirements

Header wincrypt.h
Library coredll.lib
Windows Embedded CE Windows CE 2.10 and later

See Also

Reference

Cryptography Functions
CryptDestroyHash
HCRYPTHASH