VBCorLib for VB6

Sunday, August 27, 2006

First Hash Algorithm

Well after finishing all of the symmetrical cipher algorithms I decided to begin the journey into the hash algorithms. My first stop was the SHA-1 (Secure Hash Algorithm). The first class representation was the SHACryptoServiceProvider class which is just a wrapper around the CryptoAPI calls that support the hashing within Windows.

I began my journey by learning how the CryptoAPI worked when hashing data. After a bit of digging in the MSDN I found what I was looking for and a short time later I was succeeding with generating SHA-1 hashes. Once I was comfortable with that, it was a fairly easy class to implement. I used .NET to generate a series of plain text and hashed values that I would test the class with. It went pretty smoothly. Then I noticed there was the SHAManaged class.

I realized that I was going to need to actually implement the SHA-1 algorithm, so I went searching for the rfc and sure enough there it was; RFC 3174. After reading through the workings a couple of times I noticed it came with an implementation in C. I studied the code for a bit and decided it shouldn't be an overwhelming implementation.

I coded up the implementation in VB, checking the example and re-reading the RFC. I ran it against one test, a 1-byte array with the value zero. Overflow! Uhhhh... well after going around I realized that the example used unsigned integers. Now I'm not the smartest guy, but I'm pretty sure VB6 doesn't have those. I needed to do unsigned addition. It's very complicated when you try to fake it in VB6, so I resorted to some ASM from Matt Curland and expanded the Helper class to do the unsigned addition. Once I got all that working and the Endian swapping correct, it worked like a charm.

The SHA-1 algorithm is pretty straigh forward, though I'm pretty sure the remaining algorithms will be reminding how easy I had it this time.

-Kelly

0 Comments:

Post a Comment

<< Home