Implementing SHA1 hash using Windows Cryptography API and C++
Question
Friday, December 21, 2012 6:44 AM
looking for a guidance to Implement SHA1 hash using Windows Cryptography API and C++. Does cryptography API support SHA1 hash
All replies (3)
Friday, December 21, 2012 8:23 AM ✅Answered | 1 vote
>looking for a guidance to Implement SHA1 hash using Windows Cryptography API and C++. Does cryptography API support SHA1 hash
Yes, it does - along with other hashing algorithms. CryptHashData is
the core API you need to use.
Dave
Friday, December 21, 2012 9:22 PM ✅Answered | 2 votes
On 12/21/2012 1:44 AM, Deepthi Kalyan wrote:
looking for a guidance to Implement SHA1 hash using Windows Cryptography API and C++. Does cryptography API support SHA1 hash
http://msdn.microsoft.com/en-us/library/windows/desktop/aa382380.aspx
Replace CALG_MD5 with CALG_SHA1, and change the buffer size (MD5LEN) from 16 to 20.
Igor Tandetnik
Monday, December 24, 2012 12:22 PM ✅Answered
Modified the changes as suggested. The modified code is
#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#include <tchar.h>
#define BUFSIZE 1024
#define SHA1LEN 20
DWORD main()
{
DWORD dwStatus = 0;
BOOL bResult = FALSE;
HCRYPTPROV hProv = 0;
HCRYPTHASH hHash = 0;
HANDLE hFile = NULL;
BYTE rgbFile[BUFSIZE];
DWORD cbRead = 0;
BYTE rgbHash[SHA1LEN];
DWORD cbHash = 0;
CHAR rgbDigits[] = "0123456789abcdef";
LPCTSTR filename=_T("D:\file1.txt");
// Logic to check usage goes here.
hFile = CreateFile(filename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
dwStatus = GetLastError();
printf("Error opening file %s\nError: %d\n", filename,
dwStatus);
return dwStatus;
}
// Get handle to the crypto provider
if (!CryptAcquireContext(&hProv,
NULL,
NULL,
PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT))
{
dwStatus = GetLastError();
printf("CryptAcquireContext failed: %d\n", dwStatus);
CloseHandle(hFile);
return dwStatus;
}
if (!CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
{
dwStatus = GetLastError();
printf("CryptAcquireContext failed: %d\n", dwStatus);
CloseHandle(hFile);
CryptReleaseContext(hProv, 0);
return dwStatus;
}
while (bResult = ReadFile(hFile, rgbFile, BUFSIZE,
&cbRead, NULL))
{
if (0 == cbRead)
{
break;
}
if (!CryptHashData(hHash, rgbFile, cbRead, 0))
{
dwStatus = GetLastError();
printf("CryptHashData failed: %d\n", dwStatus);
CryptReleaseContext(hProv, 0);
CryptDestroyHash(hHash);
CloseHandle(hFile);
return dwStatus;
}
}
if (!bResult)
{
dwStatus = GetLastError();
printf("ReadFile failed: %d\n", dwStatus);
CryptReleaseContext(hProv, 0);
CryptDestroyHash(hHash);
CloseHandle(hFile);
return dwStatus;
}
cbHash = SHA1LEN;
if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0))
{
printf("MD5 hash of file %s is: \n", filename);
for (DWORD i = 0; i < cbHash; i++)
{
printf("%c%c", rgbDigits[rgbHash[i] >> 4],
rgbDigits[rgbHash[i] & 0xf]);
}
printf("\n");
}
else
{
dwStatus = GetLastError();
printf("CryptGetHashParam failed: %d\n", dwStatus);
}
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
CloseHandle(hFile);
return dwStatus;
}