SHA1 Hash Implementation
#1
Posted 11 January 2011 - 11:29 PM
#2
Posted 21 March 2013 - 12:46 AM
The link is not working. I'm looking for a Sha1 implementation for my websocket handshake. This could help me.
Thanks,
Rex
#3
Posted 21 March 2013 - 01:27 PM
The link is not working. I'm looking for a Sha1 implementation for my websocket handshake. This could help me.
Thanks,
Rex
funny, just yesterday I needed sha1 (and hmac), what I did was to extract the pertinent modules from bouncy castle (which is very well respected if you aren't already familiar). just download the current (v1.7 as of this writing) and pluck out what you need. Its under MIT-style license, btw, if thats important for your project.
OK to save you an hour rummaging for what you need, here is what I pulled out to get SHA1 and HMAC:
GeneralDigest.cs HMac.cs ICipherParameters.cs IDigest.cs IMac.cs KeyParameter.cs Pack.cs Sha1Digest.cs
you'll have to find exactly where they are in the source tree since I didn't write that down, sorry, but it's all under the 'crypto' folder in various directories.
Also this list has the HMAC stuff, too, which I needed, you didn't say you needed, so you'll have to figure out which of those files to discard yourself if you want to optimise a little. I think the group added a couple K to my overall executable.
BouncyCastle is not known for their C# docco, but you could use the javadocs to get an idea since it's mostly a straight port. To give you a head start, here is my snippet testing against one of the test vectors so I could verify the implementation:
obviously include those files, and you'll be 'using' stuff:
using Org.BouncyCastle.Crypto.Macs;using Org.BouncyCastle.Crypto.Digests;using Org.BouncyCastle.Crypto.Parameters;
and here I am verifying against one of the rfc 2202 test vectors:
byte[] key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19 }; byte[] data = { 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd }; HMac hmac = new HMac(new Sha1Digest()); hmac.Init(new KeyParameter(key, 0, key.Length)); hmac.BlockUpdate(data, 0, data.Length); byte[] abyDigest = new byte[hmac.GetMacSize()]; int nRet = hmac.DoFinal(abyDigest, 0); //digest = 0x4c9007f4026250c6bc8414f9bf50c86c2d7235da
so now it's time to get cryppy with it...
EDIT: oh it's customary to welcome folks on their first post, so 'welcome to the forum'!
#4
Posted 21 March 2013 - 04:15 PM
I downloaded bouncy castle and will try to investigate the classes you suggest.
I also found another SHA1 implementation https://code.google....ase/SHA.cs?r=92 and it looks like this is the one I need.
Thanks for the help and thanks for the warm welcome.
#5
Posted 21 March 2013 - 05:34 PM
oh, nice find! I might try that one out myself....
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users