EDN Admin
Well-known member
Im not sure what Im doing wrong. All of the following code is contained in a single function and there is no multithreading of any sort. CryptAcquireContext and CryptReleaseContext both report success and hProv seems to have valid values. Doesnt help
if I run as administrator (seems like some crypt APIs require admin privileges?) This works sometimes and errors out sometimes. I cant tell when it doesnt work. Any ideas as to why this would be happening? Thanks so much in advance. Im running VS2010 SP1
on a Win 7 system.
<div style="color:Black;background-color:White; <pre>
<span style="color:Green; //computes hash of each string in an array of strings
<span style="color:Blue; for(<span style="color:Blue; int i = 0; i < numEntries; i++)
{
memcpy(entry, data + i * dataEntryLen, dataEntryLen);
<span style="color:Green; //--------------------------------------------------------------------
<span style="color:Green; // Acquire a handle to the default RSA cryptographic service provider.
<span style="color:Blue; if(CryptAcquireContext(
&hProv,
NULL,
MS_ENHANCED_PROV,
PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT))
{
printf(<span style="color:#A31515; "CSP context acquired.n");
}
<span style="color:Blue; else
{
printf(<span style="color:#A31515; "calculateHMAC: Error in AcquireContext 0x%08x n",
GetLastError());
}
<span style="color:Blue; if (!CryptCreateHash(
hProv, <span style="color:Green; // handle of the CSP.
hmacHashAlg, <span style="color:Green; // HMAC hash algorithm ID
hKey, <span style="color:Green; // key for the hash (see above)
0, <span style="color:Green; // reserved
&hHmacHash)) <span style="color:Green; // address of the hash handle
{
printf(<span style="color:#A31515; "Error in CryptCreateHash 0x%08x n",
GetLastError());
}
<span style="color:Blue; if (!CryptSetHashParam(
hHmacHash, <span style="color:Green; // handle of the HMAC hash object
HP_HMAC_INFO, <span style="color:Green; // setting an HMAC_INFO object
(BYTE*)&HmacInfo, <span style="color:Green; // the HMAC_INFO object
0)) <span style="color:Green; // reserved
{
printf(<span style="color:#A31515; "Error in CryptSetHashParam 0x%08x n",
GetLastError());
}
<span style="color:Blue; if (!CryptHashData(
hHmacHash, <span style="color:Green; // handle of the HMAC hash object
entry, <span style="color:Green; // message to hash
dataEntryLen, <span style="color:Green; // number of bytes of data to add
0)) <span style="color:Green; // flags
{
printf(<span style="color:#A31515; "Error in CryptHashData 0x%08x n",
GetLastError());
<span style="color:Blue; continue;
}
<span style="color:Green; //--------------------------------------------------------------------
<span style="color:Green; // Call CryptGetHashParam twice. Call it the first time to retrieve
<span style="color:Green; // the size, in bytes, of the hash. Allocate memory. Then call
<span style="color:Green; // CryptGetHashParam again to retrieve the hash value.
<span style="color:Blue; if (!CryptGetHashParam(
hHmacHash, <span style="color:Green; // handle of the HMAC hash object
HP_HASHVAL, <span style="color:Green; // query on the hash value
NULL, <span style="color:Green; // filled on second call
&dwDataLen, <span style="color:Green; // length, in bytes, of the hash
0))
{
printf(<span style="color:#A31515; "Error in CryptGetHashParam 0x%08x n",
GetLastError());
}
pbHash = (BYTE*)malloc(dwDataLen);
<span style="color:Blue; if(NULL == pbHash)
{
printf(<span style="color:#A31515; "unable to allocate memoryn");
<span style="color:Blue; goto Cleanup;
}
<span style="color:Blue; if (!CryptGetHashParam(
hHmacHash, <span style="color:Green; // handle of the HMAC hash object
HP_HASHVAL, <span style="color:Green; // query on the hash value
pbHash, <span style="color:Green; // pointer to the HMAC hash value
&dwDataLen, <span style="color:Green; // length, in bytes, of the hash
0))
{
printf(<span style="color:#A31515; "Error in CryptGetHashParam 0x%08x n", GetLastError());
free(pbHash);
<span style="color:Blue; continue;
}
<span style="color:Green; // Print the hash to the console.
printf(<span style="color:#A31515; "The hash is: ");
<span style="color:Blue; for(DWORD j = 0 ; j < dwDataLen ; j++)
{
printf(<span style="color:#A31515; "%2.2x ",pbHash[j]);
}
printf(<span style="color:#A31515; "n");
<span style="color:Blue; if(pbHash)
free(pbHash);
<span style="color:Blue; if(hHmacHash)
CryptDestroyHash(hHmacHash);
<span style="color:Blue; if(hProv)
<span style="color:Blue; if(!CryptReleaseContext(hProv, 0))
printf(<span style="color:#A31515; "Error 0x%08x in CryptReleaseContextn", GetLastError());
}
[/code]
<br/>
<br/>
View the full article
if I run as administrator (seems like some crypt APIs require admin privileges?) This works sometimes and errors out sometimes. I cant tell when it doesnt work. Any ideas as to why this would be happening? Thanks so much in advance. Im running VS2010 SP1
on a Win 7 system.
<div style="color:Black;background-color:White; <pre>
<span style="color:Green; //computes hash of each string in an array of strings
<span style="color:Blue; for(<span style="color:Blue; int i = 0; i < numEntries; i++)
{
memcpy(entry, data + i * dataEntryLen, dataEntryLen);
<span style="color:Green; //--------------------------------------------------------------------
<span style="color:Green; // Acquire a handle to the default RSA cryptographic service provider.
<span style="color:Blue; if(CryptAcquireContext(
&hProv,
NULL,
MS_ENHANCED_PROV,
PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT))
{
printf(<span style="color:#A31515; "CSP context acquired.n");
}
<span style="color:Blue; else
{
printf(<span style="color:#A31515; "calculateHMAC: Error in AcquireContext 0x%08x n",
GetLastError());
}
<span style="color:Blue; if (!CryptCreateHash(
hProv, <span style="color:Green; // handle of the CSP.
hmacHashAlg, <span style="color:Green; // HMAC hash algorithm ID
hKey, <span style="color:Green; // key for the hash (see above)
0, <span style="color:Green; // reserved
&hHmacHash)) <span style="color:Green; // address of the hash handle
{
printf(<span style="color:#A31515; "Error in CryptCreateHash 0x%08x n",
GetLastError());
}
<span style="color:Blue; if (!CryptSetHashParam(
hHmacHash, <span style="color:Green; // handle of the HMAC hash object
HP_HMAC_INFO, <span style="color:Green; // setting an HMAC_INFO object
(BYTE*)&HmacInfo, <span style="color:Green; // the HMAC_INFO object
0)) <span style="color:Green; // reserved
{
printf(<span style="color:#A31515; "Error in CryptSetHashParam 0x%08x n",
GetLastError());
}
<span style="color:Blue; if (!CryptHashData(
hHmacHash, <span style="color:Green; // handle of the HMAC hash object
entry, <span style="color:Green; // message to hash
dataEntryLen, <span style="color:Green; // number of bytes of data to add
0)) <span style="color:Green; // flags
{
printf(<span style="color:#A31515; "Error in CryptHashData 0x%08x n",
GetLastError());
<span style="color:Blue; continue;
}
<span style="color:Green; //--------------------------------------------------------------------
<span style="color:Green; // Call CryptGetHashParam twice. Call it the first time to retrieve
<span style="color:Green; // the size, in bytes, of the hash. Allocate memory. Then call
<span style="color:Green; // CryptGetHashParam again to retrieve the hash value.
<span style="color:Blue; if (!CryptGetHashParam(
hHmacHash, <span style="color:Green; // handle of the HMAC hash object
HP_HASHVAL, <span style="color:Green; // query on the hash value
NULL, <span style="color:Green; // filled on second call
&dwDataLen, <span style="color:Green; // length, in bytes, of the hash
0))
{
printf(<span style="color:#A31515; "Error in CryptGetHashParam 0x%08x n",
GetLastError());
}
pbHash = (BYTE*)malloc(dwDataLen);
<span style="color:Blue; if(NULL == pbHash)
{
printf(<span style="color:#A31515; "unable to allocate memoryn");
<span style="color:Blue; goto Cleanup;
}
<span style="color:Blue; if (!CryptGetHashParam(
hHmacHash, <span style="color:Green; // handle of the HMAC hash object
HP_HASHVAL, <span style="color:Green; // query on the hash value
pbHash, <span style="color:Green; // pointer to the HMAC hash value
&dwDataLen, <span style="color:Green; // length, in bytes, of the hash
0))
{
printf(<span style="color:#A31515; "Error in CryptGetHashParam 0x%08x n", GetLastError());
free(pbHash);
<span style="color:Blue; continue;
}
<span style="color:Green; // Print the hash to the console.
printf(<span style="color:#A31515; "The hash is: ");
<span style="color:Blue; for(DWORD j = 0 ; j < dwDataLen ; j++)
{
printf(<span style="color:#A31515; "%2.2x ",pbHash[j]);
}
printf(<span style="color:#A31515; "n");
<span style="color:Blue; if(pbHash)
free(pbHash);
<span style="color:Blue; if(hHmacHash)
CryptDestroyHash(hHmacHash);
<span style="color:Blue; if(hProv)
<span style="color:Blue; if(!CryptReleaseContext(hProv, 0))
printf(<span style="color:#A31515; "Error 0x%08x in CryptReleaseContextn", GetLastError());
}
[/code]
<br/>
<br/>
View the full article