Getting an exception/error when using Win32 API to create Hash string using Sha1 Algorithm

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
void main (){
CString sHashedPwd = "TNVBalaji";<br/>
<span style="white-space:pre SHA1Hash(pwd, sHashedPwd.GetBuffer());

}
// plainText: Original text to be hash<br/>
// hashCode:<span style="white-space:pre text to store hashed code, which is 40 character long string.
bool CPwdMgrX::SHA1Hash(LPCTSTR plainText, LPTSTR hashCode)<br/>
{<br/>
<span style="white-space:pre bool<span style="white-space:pre rc<span style="white-space:pre
= true;<span style="white-space:pre // Default is success<br/>
<span style="white-space:pre TCHAR<span style="white-space:pre rstData[256]= {0};<span style="white-space:pre
// Buffer to receive hashed result<br/>
<span style="white-space:pre ALG_ID<span style="white-space:pre
algorithmID<span style="white-space:pre = CALG_SHA1; // Use SHA1 algorithm<br/>
<span style="white-space:pre HCRYPTPROV hProv = NULL; // Handle to a cryptographic service provider (CSP). <br/>
<span style="white-space:pre HCRYPTHASH hHash = NULL; // Handle to the hash object needed to create a hash.<br/>
<span style="white-space:pre PBYTE pbHash = NULL; // Pointer to the hash.<br/>
<span style="white-space:pre DWORD dwDataLen = 0; // Length, in bytes, of the hash.<br/>
<br/>
<br/>
<span style="white-space:pre //--------------------------------------------------------------------<br/>
<span style="white-space:pre // Acquire a handle to the default RSA cryptographic service provider.<br/>
<span style="white-space:pre if (!CryptAcquireContext(<br/>
<span style="white-space:pre &hProv, // handle of the CSP<br/>
<span style="white-space:pre NULL, // key container name<br/>
<span style="white-space:pre NULL, // CSP name<br/>
<span style="white-space:pre PROV_RSA_FULL, // provider type<br/>
<span style="white-space:pre CRYPT_VERIFYCONTEXT)) // no key access is requested<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre printf(" Error in AcquireContext 0x%08x n", GetLastError());<br/>
<span style="white-space:pre rc = false;<br/>
<span style="white-space:pre goto ErrorExit;<br/>
<span style="white-space:pre }<br/>
<br/>
<br/>
<span style="white-space:pre if (!CryptCreateHash(<br/>
<span style="white-space:pre hProv, // handle of the CSP<br/>
<span style="white-space:pre algorithmID, // hash algorithm to use<br/>
<span style="white-space:pre 0, // hash key<br/>
<span style="white-space:pre 0, // reserved<br/>
<span style="white-space:pre &hHash)) // address of hash object handle<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre printf("Error in CryptCreateHash 0x%08x n", GetLastError());<br/>
<span style="white-space:pre rc = false;<br/>
<span style="white-space:pre goto ErrorExit;<br/>
<span style="white-space:pre }<br/>
<br/>
<br/>
<span style="white-space:pre if (!CryptHashData(<br/>
<span style="white-space:pre hHash, // handle of the hash object<br/>
<span style="white-space:pre (const BYTE *) plainText, // text to be hash<br/>
<span style="white-space:pre _tcslen(plainText)*sizeof(TCHAR), // number of bytes of data<br/>
<span style="white-space:pre 0)) // flags<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre printf("Error in CryptHashData 0x%08x n", GetLastError());<br/>
<span style="white-space:pre rc = false;<br/>
<span style="white-space:pre goto ErrorExit;<br/>
<span style="white-space:pre }<br/>
<br/>
<br/>
<span style="white-space:pre if (!CryptGetHashParam(<br/>
<span style="white-space:pre hHash, // handle of the HMAC hash object<br/>
<span style="white-space:pre HP_HASHVAL, // query on the hash value<br/>
<span style="white-space:pre NULL, // pointer to the HMAC hash value<br/>
<span style="white-space:pre &dwDataLen, // length,in bytes, of the hash<br/>
<span style="white-space:pre 0))<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre printf("Error in CryptGetHashParam 0x%08x n", GetLastError());<br/>
<span style="white-space:pre rc = false;<br/>
<span style="white-space:pre goto ErrorExit;<br/>
<span style="white-space:pre }<br/>
<br/>
<br/>
<span style="white-space:pre pbHash = (BYTE*)malloc(dwDataLen);<br/>
<span style="white-space:pre if(NULL == pbHash) <br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre printf("unable to allocate memoryn");<br/>
<span style="white-space:pre goto ErrorExit;<br/>
<span style="white-space:pre }<br/>
<br/>
<br/>
<span style="white-space:pre if (!CryptGetHashParam(<br/>
<span style="white-space:pre hHash, // handle of the HMAC hash object<br/>
<span style="white-space:pre HP_HASHVAL, // query on the hash value<br/>
<span style="white-space:pre pbHash, // pointer to the HMAC hash value<br/>
<span style="white-space:pre &dwDataLen, // length,in bytes, of the hash<br/>
<span style="white-space:pre 0))<br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre printf("Error in CryptGetHashParam 0x%08x n", GetLastError());<br/>
<span style="white-space:pre rc = false;<br/>
<span style="white-space:pre goto ErrorExit;<br/>
<span style="white-space:pre }<br/>
<br/>
<br/>
<span style="white-space:pre //CString s = (char*)pbHash;<br/>
<span style="white-space:pre TCHAR tmpBuffer[3] = {0};<br/>
<span style="white-space:pre for (DWORD i = 0 ; i < dwDataLen ; i++) <br/>
<span style="white-space:pre {<br/>
<span style="white-space:pre tmpBuffer[0] = 0; tmpBuffer[1] = 0; tmpBuffer[2] = 0;<span style="white-space:pre
// clear<br/>
<span style="white-space:pre _stprintf_s(tmpBuffer, _T("%2.2x"),pbHash);<br/>
<span style="white-space:pre _tcscat_s(rstData, tmpBuffer);<br/>
<span style="white-space:pre }<br/>
<span style="white-space:pre _tcscpy(hashCode, rstData);<br/>
<br/>
<br/>
<span style="white-space:pre // Free resources.<br/>
ErrorExit:<br/>
<span style="white-space:pre if(hHash)<br/>
<span style="white-space:pre CryptDestroyHash(hHash); <br/>
<span style="white-space:pre if(hProv)<br/>
<span style="white-space:pre CryptReleaseContext(hProv, 0);<br/>
<span style="white-space:pre if(pbHash)<br/>
free(pbHash);<br/>
<br/>
<span style="white-space:pre return rc;<br/>
}

Error Message
=================
---------------------------<br/>
Microsoft Visual C++ Debug Library<br/>
---------------------------<br/>
Debug Assertion Failed!<br/>
<br/>
<br/>
Program: C:Program FilesInternet ExplorerIEXPLORE.EXE<br/>
File: f:ddvctoolscrt_bldself_x86crtsrcdbgheap.c<br/>
Line: 1317<br/>
<br/>
<br/>
Expression: _CrtIsValidHeapPointer(pUserData)<br/>
<br/>
<br/>
For information on how your program can cause an assertion<br/>
failure, see the Visual C++ documentation on asserts.<br/>
<br/>
<br/>
(Press Retry to debug the application)<br/>
---------------------------<br/>
Abort Retry Ignore <br/>
---------------------------<br/>

Please Help to resolve the issue.
<hr class="sig www.tnvbalaji.com

View the full article
 
Back
Top