Anyone implemented a sha1 hash function?

Forums for specific tips, techniques and example code
concord
Posts: 47
Joined: Wed Mar 23, 2011 2:07 pm

Anyone implemented a sha1 hash function?

Postby concord » Mon Aug 13, 2012 2:10 pm

Recently I played around with the various advapi32 functions for creating a sha1 hash. It seemed awfully complicated, which is not too surprising.

I needed to get this up and running ASAP for the TradeMe api authentication so ended up taking a huge short-cut, I created a tiny php page that does the job in a couple of lines of code. Thinking, I'll come back to this and solve it properly when I have time.

Well I'm back to it and I'm still scratching my head.

I need to be able to create a sha1 hash with a shared secret and have the output in raw format http://php.net/manual/en/function.hash-hmac.php
So simple in php...

Code: Select all

hash_hmac('sha1', 'The quick brown fox jumped over the lazy dog.', 'secret', true);
In the past I managed to goggle up some Delphi code for a 'ripemd160' hash and pretty much just cut and paste it into a dll and was done.

I guess it was so easy with php, and easy with the Delphi cut and paste and so I find myself being equally as lazy and thinking surely this has already been done?

User avatar
suzuki1100
Posts: 29
Joined: Tue Nov 24, 2009 12:00 pm
Location: Auckland

Re: Anyone implemented a sha1 hash function?

Postby suzuki1100 » Mon Aug 13, 2012 8:05 pm


murray
Posts: 144
Joined: Fri Aug 14, 2009 6:58 pm
Location: New Plymouth, New Zealand

Re: Anyone implemented a sha1 hash function?

Postby murray » Mon Aug 13, 2012 8:09 pm

SHA1 and HMAC are not the same thing. SHA1 is the core hash function which is used as a building-block in other algorithms such as HMAC. Once you have a working hash function you can use it to build a HMAC function.

I use the SHA1 hash function that's available from the open source libeay32 library that is distributed with Jade.
It is a simple matter to define an external function that you call from a method on the Binary type.

Here is a link to an old post that has the basic details: https://forums.jadeworld.com/viewtopic. ... 6433#p6429. Fortunately the output of a hash digest function is a known fixed length, so you can avoid the problem sometimes encountered with defining external functions from Jade where a variable length result is returned.

Actually, I'll paste the definitions from the other posting here, for completeness...

Code: Select all

sha1(data: Binary; length: Integer; digest: Binary[20] output) is SHA1 in libeay32; md5(data: Binary; length: Integer; digest: Binary[16] output) is MD5 in libeay32;
Murray (N.Z.)

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

Re: Anyone implemented a sha1 hash function?

Postby ghosttie » Tue Aug 14, 2012 2:46 am

We use advapi, but if all you want to do is SHA1 then murray's solution looks simpler.

Both MD5 and SHA1 have known weaknesses, so depending on what you want to use the hash for, you might want to use an algorithm from the SHA-2 family (SHA-256 and SHA-512).

If you're storing passwords then you should use a slow algorithm like PBKDF2 rather than a fast one like SHA1, because if your database is compromised you want it to take the bad guy a long time to try to work out what password corresponds with each hash. You also want to salt the hash and use a different (random) salt for each password so that the bad guy can't just get the salt from your code and use it on all the hashes.
I have a catapult. Give me all the money or I will fling an enormous rock at your head.

murray
Posts: 144
Joined: Fri Aug 14, 2009 6:58 pm
Location: New Plymouth, New Zealand

Re: Anyone implemented a sha1 hash function?

Postby murray » Tue Aug 14, 2012 8:18 am

By all means use the latest hash algorithm, as they are better, however in many cases SHA1 still has to be used for compatibility with standards. Here is the corresponding Jade external function definition for SHA256 from libeay32 that I use:

Code: Select all

sha256(data: Binary; length: Integer; digest: Binary[32] output) is SHA256 in libeay32;
I agree with Ghosttie that you must follow best practice when developing cryptographic solutions. There are many pitfalls if you are designing it yourself. It pays to choose a tried and tested algorithm and not take any shortcuts. More often than not it will be the weaknesses in the implementation that will catch you out.
Murray (N.Z.)

concord
Posts: 47
Joined: Wed Mar 23, 2011 2:07 pm

Re: Anyone implemented a sha1 hash function?

Postby concord » Tue Aug 14, 2012 10:00 am

Hi guys, thanks for all the tips I'm going to look into libeay32 right away.

I have to conform to the strict OAuth requirements implemented by TradeMe, I have zero wriggle room.

At present the php function gives me exactly what I need... sha1 hash with a "shared secret key" in raw output (NOT hexits).

The shared secret key is the 3rd parameter in php's hash_hmac function, the 4th param ensures I get raw output.

Any ideas if I'm going to get raw output, with a shared secret key using "SHA1 in libeay32;"?

concord
Posts: 47
Joined: Wed Mar 23, 2011 2:07 pm

Re: Anyone implemented a sha1 hash function?

Postby concord » Tue Aug 14, 2012 10:14 am

Brilliant, just defined an md5 function (which I also need for sending photos to TradeMe) appears the output is raw binary by default which means I'm halfway there for the sha1 function.

All I need now is to workout how to handle the shared key (hmac?) part for my sha1 hash, and how to convert my raw binary into (I think) hex bits for the md5.

Thanks again for your help.

concord
Posts: 47
Joined: Wed Mar 23, 2011 2:07 pm

Re: Anyone implemented a sha1 hash function?

Postby concord » Tue Aug 14, 2012 10:54 am

OK, so I have a basic toHexDigits() method on Binary:

Code: Select all

i := 1; while i <= self.length do if self[i] <> null then s := s & self[i].Character.toHex; endif; i := i + 1; endwhile;
All I need now is to sort out the hmac external function call signature.
I can't seem to find any documentation on libeay32, trial and error indicates that libeay32 has a function called hmac. I hope I just need to get the signature right and I'm done.

murray
Posts: 144
Joined: Fri Aug 14, 2009 6:58 pm
Location: New Plymouth, New Zealand

Re: Anyone implemented a sha1 hash function?

Postby murray » Tue Aug 14, 2012 11:35 am

HMAC is an algorithm that can be implemented in different ways. The standard is documented in RFC 2104 (have a look at the Wikipedia page).
It's really just a couple of XORs and a couple of hashes, done in the right sequence.

Here's one that I have done in the past (slightly edited). It requires Binary::bitXor() and Binary::hash() to be provided:

Code: Select all

hmac( pKey : Binary ) : Binary; constants BLOCK_SIZE = 64; INNER_PAD = #[36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36]; OUTER_PAD = #[5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C]; vars key : Binary; begin if pKey.length > BLOCK_SIZE then key[1:BLOCK_SIZE] := pKey.hash; else key[1:BLOCK_SIZE] := pKey; // key, inner pad & outer pad are all = block size endif; return ( key.bitXor(OUTER_PAD) & ( key.bitXor(INNER_PAD) & self ).hash ).hash; end;
Murray (N.Z.)

murray
Posts: 144
Joined: Fri Aug 14, 2009 6:58 pm
Location: New Plymouth, New Zealand

Re: Anyone implemented a sha1 hash function?

Postby murray » Tue Aug 14, 2012 11:43 am

All I need now is to sort out the hmac external function call signature.
I can't seem to find any documentation on libeay32, trial and error indicates that libeay32 has a function called hmac. I hope I just need to get the signature right and I'm done.
Yes, Concord, you're right, I see that HMAC is supported in libeay32. However, I have not used that function.
I can see it defined in the source code (freely available) in crypto/hmac/hmac.h, and it is documented in this page online http://www.openssl.org/docs/crypto/hmac.html#.
That may be of some help to you.
Murray (N.Z.)


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 8 guests