Using SHA256 with JADE

Forums for specific tips, techniques and example code
M45HY
Posts: 63
Joined: Wed Jul 11, 2012 7:32 am
Location: Mansfield, Nottinghamshire, UK

Using SHA256 with JADE

Postby M45HY » Wed Feb 06, 2013 3:43 am

Hi Guys,

I've come across a problem where I'm unable to successfully hash a piece of data using the SHA256 algorithm. Down below, I have illustrated the coding where an error message is being received.

Code: Select all

constants CALG_SHA_256 : Integer = #800c; HP_HASHVAL : Integer = Gc_2; PROV_RSA_FULL : Integer = Gc_1; CRYPT_NEWKEYSET : Integer = Gc_8; MS_DEF_PROV : String = "Microsoft Base Cryptographic Provider v1.0"; vars int_createHash : Integer; int_phProv : Integer; // A pointer to a handle of a CSP int_phHash : Integer; // The address of the handle to the new hash object begin /* * I've skipped this part of the coding, but initially, all I do is assign the data to be * hashed and then use the following method: cryptAcquireContext */ /* * Initiate the hashing function */ int_createHash := call cryptCreateHash(int_phProv,CALG_SHA_256,Gc_0,Gc_0,int_phHash); if (int_createHash = Gc_0) then write "cryptCreateHash Error : Unable to initiate the hashing function"; return false; endif;
I have tried understanding and applying knowledge from the other links on the forum (such as https://forums.jadeworld.com/viewtopic. ... hilit=SHA1). But it seems this is where the error comes from; any suggestions?

Thanks everyone,
Omash
"If you can't explain it simply, you don't understand it well enough." - Albert Einstein

User avatar
ghosttie
Posts: 181
Joined: Sat Aug 15, 2009 1:25 am
Location: Atlanta, GA, USA
Contact:

Re: Using SHA256 with JADE

Postby ghosttie » Wed Feb 06, 2013 5:14 am

Before you call createHash you need to acquire a context:

Code: Select all

setup(pS_Container : String; pS_Provider : String; pI_ProviderType : Integer) : Integer updating; vars begin // make sure we clean up anything we need to before we get new handles (in case this is called twice) destructor; if call cryptAcquireContext(hProv, pS_Container, pS_Provider, pI_ProviderType, 0) = 0 then // try to connect to an existing container with this name if call cryptAcquireContext(hProv, pS_Container, pS_Provider, pI_ProviderType, CRYPT_NEWKEYSET) = 0 then // try to create a container with this name return FVE_CryptAcquireContextFailed; endif; endif; return FVE_Success; end;
then you hash your data (this version hashes a String but you could also do a Binary)

Code: Select all

hashString(pI_HashAlgorithm : Integer; pS_Source : String; pS_Destination : String output) : Integer; vars hHash : Integer; sHash : String; iHashLen : Integer; begin if call cryptCreateHash(hProv, pI_HashAlgorithm, 0, 0, hHash) = 0 then return FVE_CryptCreateHashFailed; endif; if call cryptHashDataString(hHash, pS_Source, pS_Source.length, 0) = 0 then return FVE_CryptHashDataFailed; endif; call cryptGetHashParamString(hHash, HP_HASHVAL, null, iHashLen, 0); sHash := sHash.padBlanks(iHashLen); if call cryptGetHashParamString(hHash, HP_HASHVAL, sHash, iHashLen, 0) = 0 then return FVE_CryptGetHashParamFailed; endif; pS_Destination := sHash[1 : iHashLen]; return FVE_Success; epilog if hHash <> 0 then call cryptDestroyHash(hHash); endif; end;
finally you release the context:

Code: Select all

destructor() updating, protected; vars begin // disconnect from CSP if hProv <> 0 then call cryptReleaseContext(hProv, 0); endif; end;
I have a catapult. Give me all the money or I will fling an enormous rock at your head.

M45HY
Posts: 63
Joined: Wed Jul 11, 2012 7:32 am
Location: Mansfield, Nottinghamshire, UK

Re: Using SHA256 with JADE

Postby M45HY » Wed Feb 06, 2013 9:57 pm

Hi ghosttie,

Thanks for getting back to me so quickly; it's most appreciated :D . In regards to your help, I have already coded everything that you have entered, it was just that I was getting an error when it came to using the cryptCreateHash method. The only difference that I could find between what I have done and what you have illustrated, is that I have used cryptHashData rather than cryptHashDataString. Even though I am trying to hash a string, does it matter which method I use?

Here's what I have:

Code: Select all

/* * Connect to Cryptographic Service Provider (CSP) and return a handle to it * If you cannot connect to an existing key container with this name, * Try to create a key container with a default name */ str_pszProvider := MS_DEF_PROV; int_acquireContext := call cryptAcquireContext(int_phProv,str_pszContainer,str_pszProvider,PROV_RSA_FULL,Gc_0); if (int_acquireContext = Gc_0) then int_acquireContext := call cryptAcquireContext(int_phProv,str_pszContainer,str_pszProvider,PROV_RSA_FULL,CRYPT_NEWKEYSET); if (int_acquireContext = Gc_0) then write "cryptAcquireContext Error : Container unavailable; Handle not created"; return false; endif; endif; /* * Initiate the hashing function */ int_createHash := call cryptCreateHash(int_phProv,CALG_SHA_256,Gc_0,Gc_0,int_phHash); if (int_createHash = Gc_0) then write "cryptCreateHash Error : Unable to initiate the hashing function"; return false; endif; /* * Hash the data */ int_hashData := call cryptHashData(int_phHash,str_toHash,str_toHash.length,Gc_0); if (int_hashData = Gc_0) then write "cryptHashData Error : Unable to hash the data"; return false; endif; /* * Retrieve the data that runs the operations of a hash object */ int_toHashLen := str_toHash.length; int_getHash := call cryptGetHashParam(int_phHash,HP_HASHVAL,str_toHash,int_toHashLen,Gc_0); if (int_getHash = Gc_0) then write "cryptGetHashParam Error : Unable to retrieve data"; return false; endif; str_hashOutput := str_toHash[1:int_toHashLen]; bin_hashBinary := str_hashOutput.Binary; /* * Destroy the hash object */ call cryptDestroyHash(int_phHash); /* * Release the CSP handle */ call cryptReleaseContext(int_phProv, Gc_0);
When I ran this (alongside some other coding specific to my use, which was just collecting data and putting it into a String), I seemed to have got the error in regards to the cryptCreateHash method. I'm not sure if it's because of the way I have defined the SHA-256 Algorithm:

Code: Select all

constants CALG_SHA_256 : Integer = #800C; // I'm not sure whether the #800C is right. I think it's meant to be the Integer value of the SHA hex representation of 0x000800c HP_HASHVAL : Integer = Gc_2; PROV_RSA_FULL : Integer = Gc_1; CRYPT_NEWKEYSET : Integer = Gc_8; MS_DEF_PROV : String = "Microsoft Base Cryptographic Provider v1.0";
Any opinions?

Thanks,
Omash
"If you can't explain it simply, you don't understand it well enough." - Albert Einstein

User avatar
ghosttie
Posts: 181
Joined: Sat Aug 15, 2009 1:25 am
Location: Atlanta, GA, USA
Contact:

Re: Using SHA256 with JADE

Postby ghosttie » Thu Feb 07, 2013 3:03 am

I think the problem is your provider - try using MS_ENH_RSA_AES_PROV and PROV_RSA_AES. See this, this and this.
I have a catapult. Give me all the money or I will fling an enormous rock at your head.

M45HY
Posts: 63
Joined: Wed Jul 11, 2012 7:32 am
Location: Mansfield, Nottinghamshire, UK

Re: Using SHA256 with JADE

Postby M45HY » Fri Feb 08, 2013 10:02 pm

Hi ghosttie,

When I try using PROV_RSA_AES and MS_ENH_RSA_AES_PROV, the system fails at the cryptAcquireContext method where before it use to fail at the cryptCreateHash method. I think that the initial way that I had coded it was correct but it's the way I have defined the constant CALG_SHA_256 which is causing the problem. Have you (or anyone else reading this page) have an example of how the CALG_SHA_256 has been defined as a constant and/or how it was used?

Actually...
Just tested my coding with SHA-1 and it worked, which means that I'm having a problem using SHA-256. Any ideas on how to define and use it?

Omash
"If you can't explain it simply, you don't understand it well enough." - Albert Einstein

User avatar
ghosttie
Posts: 181
Joined: Sat Aug 15, 2009 1:25 am
Location: Atlanta, GA, USA
Contact:

Re: Using SHA256 with JADE

Postby ghosttie » Sat Feb 09, 2013 2:25 am

Code: Select all

MS_ENH_RSA_AES_PROV : String = "Microsoft Enhanced RSA and AES Cryptographic Provider" PROV_RSA_AES : Integer = 24 CALG_SHA_256 : Integer = 32780 // (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA_256)
I'm pretty sure you have to use MS_ENH_RSA_AES_PROV and PROV_RSA_AES in order to use CALG_SHA_256. CALG_SHA works with MS_DEF_PROV and PROV_RSA_FULL but different providers support different algorithms - if you look at the documentation for PROV_RSA_FULL it says it supports SHA but not SHA-2, and if you look at the documentation for PROV_RSA_AES it says it supports SHA-2 which includes SHA-256.

If you had an error when acquiring a context with MS_ENH_RSA_AES_PROV then maybe that provider not supported by your operating system. According to the documentation in Windows XP the provider name is "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)" - are you using XP?
I have a catapult. Give me all the money or I will fling an enormous rock at your head.

M45HY
Posts: 63
Joined: Wed Jul 11, 2012 7:32 am
Location: Mansfield, Nottinghamshire, UK

Re: Using SHA256 with JADE

Postby M45HY » Tue Feb 12, 2013 12:15 am

Hi ghosttie,

Thanks for you help matey. Most of it works now; I think all I required was the definitions of the constants to be correct, which they weren't. In regards to your question, I am currently using Windows 7 whilst our server in which the coding will run on, is on 2008 Enterprise. I'm just stuck on the last part now, which is actually retrieving the data. I'm using the cryptGetHashParam() method, however, I'm not getting any errors but I believe that the data is not being hashed and/or I'm not retrieving it correctly.

Code: Select all

vars toHash : String // An empty String int_phHash : Integer // Handle of the Hash object lCryptLength : Intger // Which I believe is a length of a line in an XML file begin int_getHash := call cryptGetHashParam(int_phHash,HP_HASHVAL,toHash,int_toHashLen,Gc_0); if (int_getHash = Gc_0) then write "cryptGetHashParam Error : Unable to retrieve data"; return false; endif;
Omash
"If you can't explain it simply, you don't understand it well enough." - Albert Einstein

User avatar
ghosttie
Posts: 181
Joined: Sat Aug 15, 2009 1:25 am
Location: Atlanta, GA, USA
Contact:

Re: Using SHA256 with JADE

Postby ghosttie » Tue Feb 12, 2013 1:09 am

If you look at my previous example hashString, you'll see that in order to get the hashed result you have to call cryptGetHashParam with null pbData and dwDataLen which will put the length of the hash in dwDataLen. Then you create a string with a length of dwDataLen and call cryptGetHashParam again, passing the string and dwDataLen back in. Finally make sure to trim the string to the new value of dwDataLen because it might be different.

This is explained in the documentation for cryptGetHashParam.
I have a catapult. Give me all the money or I will fling an enormous rock at your head.

M45HY
Posts: 63
Joined: Wed Jul 11, 2012 7:32 am
Location: Mansfield, Nottinghamshire, UK

Re: Using SHA256 with JADE

Postby M45HY » Tue Feb 12, 2013 3:13 am

Hi ghosttie,

Thank you very much for the help mate, with a bit of messing around and playing with the coding, I got the hashed data that I required. However, if it wasn't for you, it wouldn't have been possible. Cheers mate! :D

Omash
"If you can't explain it simply, you don't understand it well enough." - Albert Einstein

MJones
Posts: 10
Joined: Fri Jan 28, 2011 12:07 pm

Re: Using SHA256 with JADE

Postby MJones » Mon Oct 21, 2013 10:30 am

I appreciate this is all working now but for anyone else interested in a .NET framework .dll that can be imported into JADE that hides all the handles and complexity, presenting just simple SHA & MD5 functions (pass in a string recieve hash back) you are welcome to use the attached file that I built a few years back. It implements the same standard Microsoft Crypto functions that you are using above.

It can be imported into Jade using Browse-> External Component Libraries via the .NET Framework tab.

Personally I always keep this and other similar files in the bin directory for your jade install.

Please note this is supplied on a 'as is basis', etc etc..


Regards,
Michael Jones
Attachments
HashHelper.zip
(2.58 KiB) Downloaded 179 times


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 1 guest

cron