locked
SHA1 hash problem RRS feed

  • Question

  • Dear all,

    I have refer to http://msdn.microsoft.com/en-us/library/aa382379(VS.85).aspx to create a SHA-1 hash function.

    if my input is "Hello", my function output is "d2efcbba102ed3339947e85f4141eb08926e40e9".
    But, I use two another program to test my function, "Hello" should be return "f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0".

    what wrong with my program?

    Thanks.

    #include <stdafx.h>
    
    #include <wincrypt.h>
    #pragma comment (lib, "advapi32.lib")
    
    //--------------------------------------------------------------------
    // Hash function using SHA1 algorithm
    //
    // plainText:       Original text to be hash
    // hashCode:		text to store hashed code, which is 40 character long string.
    // 
    bool SHA1Hash(LPCTSTR plainText, LPTSTR hashCode)
    {
    	bool		rc			= true;		// default is success
    	TCHAR		rstData[256]= {0};		// buffer to receive hashed result
    	ALG_ID		algorithmID	= CALG_SHA1; // use SHA1 algorithm
    	HCRYPTPROV  hProv       = NULL;
    	HCRYPTHASH  hHash       = NULL;
    	BYTE		pbHash[50]  = {0};
    	DWORD       dwDataLen   = 0;
    
    	//--------------------------------------------------------------------
    	// Acquire a handle to the default RSA cryptographic service provider.
    	if (!CryptAcquireContext(
    		&hProv,                   // handle of the CSP
    		NULL,                     // key container name
    		NULL,                     // CSP name
    		PROV_RSA_FULL,            // provider type
    		CRYPT_VERIFYCONTEXT))     // no key access is requested
    	{
    		_tprintf(_T(" Error in AcquireContext 0x%08x \n"), GetLastError());
    		rc = false;
    		goto ErrorExit;
    	}
    
    	if (!CryptCreateHash(
    		hProv,                    // handle of the CSP
    		algorithmID,              // hash algorithm to use
    		0,                        // hash key
    		0,                        // reserved
    		&hHash))                  // address of hash object handle
    	{
    		_tprintf(_T("Error in CryptCreateHash 0x%08x \n"), GetLastError());
    		rc = false;
    		goto ErrorExit;
    	}
    
    	if (!CryptHashData(
    		hHash,                    // handle of the hash object
    		(const BYTE *) plainText, // text to be hash
    		_tcslen(plainText)*sizeof(TCHAR), // number of bytes of data
    		0))                       // flags
    	{
    		_tprintf(_T("Error in CryptHashData 0x%08x \n"), GetLastError());
    		rc = false;
    		goto ErrorExit;
    	}
    
    	if (!CryptGetHashParam(
    		hHash,                 // handle of the HMAC hash object
    		HP_HASHVAL,                // query on the hash value
    		NULL,                    // pointer to the HMAC hash value
    		&dwDataLen,                // length,in bytes, of the hash
    		0))
    	{
    		_tprintf(_T("Error in CryptGetHashParam 0x%08x \n"), GetLastError());
    		rc = false;
    		goto ErrorExit;
    	}
    
    	if (!CryptGetHashParam(
    		hHash,                 // handle of the HMAC hash object
    		HP_HASHVAL,                // query on the hash value
    		pbHash,                    // pointer to the HMAC hash value
    		&dwDataLen,                // length,in bytes, of the hash
    		0))
    	{
    		_tprintf(_T("Error in CryptGetHashParam 0x%08x \n"), GetLastError());
    		rc = false;
    		goto ErrorExit;
    	}
    
    	TCHAR tmpBuffer[3] = {0};
    	for (DWORD i = 0 ; i < dwDataLen ; i++) 
    	{
    		tmpBuffer[0] = 0; tmpBuffer[1] = 0; tmpBuffer[2] = 0;		// clear
    		_stprintf(tmpBuffer, _T("%2.2x"),pbHash[i]);
    		_tcscat(rstData, tmpBuffer);
    	}
    	_tcscpy(hashCode, rstData);
    
    	// Free resources.
    ErrorExit:
    	if(hHash)
    		CryptDestroyHash(hHash);    
    	if(hProv)
    		CryptReleaseContext(hProv, 0);
    
    	return rc;
    }
    Thursday, November 5, 2009 4:39 AM

Answers

  • if my input is "Hello", my function output is "d2efcbba102ed3339947e85f4141eb08926e40e9".
    But, I use two another program to test my function, "Hello" should be return "f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0".

    This is because if the difference in the unicode and non unicode strings. Your application is using UNICODE charecter set and the other application that returned ""f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0" might be using multi byte character set.

    if you want to get "f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0" as output change your project to multibyte character set or change the function "SHA1Hash" to accept char*
    Nave [My Blog]
    • Proposed as answer by iamhe Thursday, November 5, 2009 4:09 PM
    • Marked as answer by Kevin Tang in Macau Friday, November 6, 2009 3:05 AM
    Thursday, November 5, 2009 4:55 AM

All replies

  • if my input is "Hello", my function output is "d2efcbba102ed3339947e85f4141eb08926e40e9".
    But, I use two another program to test my function, "Hello" should be return "f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0".

    This is because if the difference in the unicode and non unicode strings. Your application is using UNICODE charecter set and the other application that returned ""f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0" might be using multi byte character set.

    if you want to get "f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0" as output change your project to multibyte character set or change the function "SHA1Hash" to accept char*
    Nave [My Blog]
    • Proposed as answer by iamhe Thursday, November 5, 2009 4:09 PM
    • Marked as answer by Kevin Tang in Macau Friday, November 6, 2009 3:05 AM
    Thursday, November 5, 2009 4:55 AM
  • Dear Nave,

    Thank you, you are right. After modify my project to support MBSC, my function return "f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0"

    I have another question. After I modified hash algorithm to "CALG_SHA_256",  CryptCreateHash(...) function fail, the error code is "0x80090008 (Invalid algorithm specified.)".

    Why I cannot use SHA-2 (256bit) algorithm? How to solve?

    Thank you.
    Thursday, November 5, 2009 7:27 AM
  • From MSDN:

    Windows XP and Windows 2000/NT:  
    This algorithm is not supported
    Nave [My Blog]
    Thursday, November 5, 2009 8:30 AM
  • From MSDN:

    Windows XP and Windows 2000/NT:  
    This algorithm is not supported
    Nave [My Blog]

    I recompile my program and test it under Windows 7, but it still fail in CryptCreateHash().....

    My platform is "Windows 7, Visual Studio 2008 SP1".

    Do I need to install something or update? Or my program need to modify?

    Thanks.
    Friday, November 6, 2009 7:12 AM
  • I am working on windows 8, Visual Studio 2012 Ultimate, and SHA 256 is not supported for me too. Can you tell me why that is the case.
    Tuesday, May 20, 2014 1:04 AM