Thread View: borland.public.cppbuilder.nativeapi
2 messages
2 total messages
Started by Bob Piskac
Thu, 30 Nov 2006 18:57
Crypto SHA1 hash
Author: 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
Re: Crypto SHA1 hash
Author: Bob Piskac
Date: Sat, 02 Dec 2006 21:12
Date: Sat, 02 Dec 2006 21:12
18 lines
1326 bytes
1326 bytes
Here is my error. Never mind.
sizeof("bob") - 1, 0))
Bob Piskac wrote:
> I am trying to SHA1 hash the string: bob
> //--------------------------------------------------------------------
> // Hash a BYTE string.
>
> if (CryptHashData(
> hOriginalHash,
> (BYTE*)"bob",
> sizeof("bob"), 0))
> {
> printf("An original hash has been created. \n");
> }
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads