The CryptDuplicateHash function is used to make an exact copy of a hash and the state the hash is in.
#include <wincrypt.h>
BOOL WINAPI CryptDuplicateHash(
HCRYPTHASH hHash, // in
DWORD *pdwReserved, // in
DWORD dwFlags, // in
HCRYPTHASH phHash // out
);
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_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. |
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.
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;}
Windows NT: Requires version 5.0 or later.
Windows: Unsupported.
Windows CE: Unsupported.
Header: Declared in wincrypt.h.
Import Library: Use advapi32.lib.