CryptDuplicateHash

This function is used to make an exact copy of a hash and the state the hash is in.

At a Glance

Header file: Wincrypt.h
Windows CE versions: 2.10 and later

Syntax

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

Parameters

hHash

[in] Handle to the hash to be duplicated.

pdwReserved

[in] Reserved for future use; set to NULL.

dwFlags

[in] Specifies a bitmask of flags. This parameter is reserved for future use; set to 0.

phHash

[out] Pointer to the handle to the duplicated hash.

Return Values

TRUE indicates success. FALSE indicates failure. To get extended error information, call GetLastError. Common values for GetLastError are described in the following table. The error values prefaced by "NTE" are generated by the particular CSP you are using.

Value Description
ERROR_CALL_NOT_IMPLEMENTED Since 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

CryptDuplicateHash is used to make copies of hashes and the exact state of the hash. An example of how this function might be used is that a caller may want to generate two hashes, but both hashes have to start with some common data hashed. 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.

CryptDestroyHash must be called to destroy any hashes that are created with CryptDuplicateHash. 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

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;}

See Also

CryptDestroyHash