Article View: borland.public.cppbuilder.nativeapi
Article #2838Crypto SHA1 hash
From: Bob Piskac
Date: Thu, 30 Nov 2006 18:57
Date: Thu, 30 Nov 2006 18:57
229 lines
6039 bytes
6039 bytes
I am trying to SHA1 hash the string: bob
MyCode: "5069c5f108ced9dccef0ebd4ab53ef8a418bef33"
SHA1: "48181acd22b3edaebc8a447868a7df7ce629920a"
My code is not generating the correct hash. Any ideas why? Sample
taken from Microsoft.
// SHA1.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
void MyHandleError(char *s);
void Get_And_Print_Hash(HCRYPTHASH hHash);
int main(void)
{
//--------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Declare and initialize variables.
HCRYPTPROV hCryptProv = NULL;
HCRYPTHASH hOriginalHash;
//--------------------------------------------------------------------
// Acquire a cryptographic provider context handle.
if(CryptAcquireContext(
&hCryptProv,
NULL,
NULL,
// MS_ENHANCED_PROV,
PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT))
{
printf("CryptAcquireContext succeeded. \n");
}
else
{
MyHandleError("Error during CryptAcquireContext!");
}
//--------------------------------------------------------------------
// Create a hash.
if (CryptCreateHash(
hCryptProv,
CALG_SHA1,
// CALG_MD5,
0,
0,
&hOriginalHash))
{
printf("An empty hash object has been created. \n");
}
else
{
MyHandleError("Error during CryptCreateHash.");
}
//--------------------------------------------------------------------
// Hash a BYTE string.
if (CryptHashData(
hOriginalHash,
(BYTE*)"bob",
sizeof("bob"), 0))
{
printf("An original hash has been created. \n");
}
else
{
MyHandleError("Error during CryptHashData.");
}
//--------------------------------------------------------------------
// Duplicate the hash.
/*
if (CryptDuplicateHash(
hOriginalHash,
NULL,
0,
&hDuplicateHash))
{
printf("The hash has been duplicated. \n");
}
else
{
MyHandleError("Error during CryptDuplicateHash.");
}
*/
printf("The original hash -- phase 1.\n");
Get_And_Print_Hash(hOriginalHash);
// Destroy the original hash.
if(CryptDestroyHash(hOriginalHash))
{
printf("The original hash has been destroyed. \n");
}
else
{
MyHandleError("ERROR during CryptDestroyHash");
}
//--------------------------------------------------------------------
// Release the CSP.
if(hCryptProv)
CryptReleaseContext(hCryptProv,0);
printf("The program ran to completion without error. \n");
return(0);
} // end main
//--------------------------------------------------------------------
// Define the function Get_And_Print_Hash.
void Get_And_Print_Hash(HCRYPTHASH hOHash)
{
//--------------------------------------------------------------------
// Declare and initialize local variables.
HCRYPTHASH hHash;
BYTE *pbHash;
BYTE *pbHashSize;
DWORD dwHashLen = sizeof(DWORD);
DWORD i;
//--------------------------------------------------------------------
// Duplicate the hash passed in.
// The hash is duplicated to leave the original hash intact.
if (CryptDuplicateHash(
hOHash,
NULL,
0,
&hHash))
{
// It worked. Do nothing.
}
else
{
MyHandleError("Error during CryptDuplicateHash.");
}
if(!(pbHashSize =(BYTE *) malloc(dwHashLen)))
MyHandleError("Memory allocation failed.");
if(CryptGetHashParam(
hHash,
HP_HASHSIZE,
pbHashSize,
&dwHashLen,
0))
{
// It worked. Free pbHashSize.
free(pbHashSize);
}
else
{
MyHandleError("CryptGetHashParam failed to get size.");
}
if(CryptGetHashParam(
hHash,
HP_HASHVAL,
NULL,
&dwHashLen,
0))
{
// It worked. Do nothing.
}
else
{
MyHandleError("CryptGetHashParam failed to get length.");
}
if(pbHash = (BYTE*)malloc(dwHashLen))
{
// It worked. Do nothing.
}
else
{
MyHandleError("Allocation failed.");
}
if(CryptGetHashParam(
hHash,
HP_HASHVAL,
pbHash,
&dwHashLen,
0))
{
// Print the hash value.
printf("The hash is: \"");
for(i = 0 ; i < dwHashLen ; i++)
{
printf("%2.2x",pbHash[i]);
}
printf("\"\n");
}
else
{
MyHandleError("Error during reading hash value.");
}
free(pbHash);
if (CryptDestroyHash(hHash))
{
// It worked. Do nothing.
}
else
{
MyHandleError("ERROR - CryptDestroyHash");
}
} // end Get_And_Print_Hash
//--------------------------------------------------------------------
// This example uses the function MyHandleError, a simple error
// handling function to print an error message and exit
// the program.
// For most applications, replace this function with one
// that does more extensive error reporting.
void MyHandleError(char *s)
{
fprintf(stderr,"An error occurred in running the program. \n");
fprintf(stderr,"%s\n",s);
fprintf(stderr, "Error number %x.\n", GetLastError());
fprintf(stderr, "Program terminating. \n");
exit(1);
} // end MyHandleError
Technical Details
Message-ID:
<456f2981$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news.glorb.com!multikabel.net!feed20.multikabel.net!newsfeed.freenet.de!news.albasani.net!feeder06.uucp-net.de!reader.uucp.at!feeder02.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,549f3a6d72a98b67,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Thu, 30 Nov 2006 13:57:37 -0500 From: Bob Piskac <bob@pbsoftware.com> User-Agent: Thunderbird 1.5.0.8 (Windows/20061025) MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Crypto SHA1 hash Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.154.8.58 Message-ID: <456f2981$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1164913025 24.154.8.58 (30 Nov 2006 10:57:05 -0700) Lines: 229 Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news.glorb.com!multikabel.net!feed20.multikabel.net!newsfeed.freenet.de!news.albasani.net!feeder06.uucp-net.de!reader.uucp.at!feeder02.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail