tags:

views:

566

answers:

3

Hey! I am trying to do something in C with the MD5 (and latter trying to do something with the SHA1 algorithm). My main problem is that I never really did anything complex in C, just simple stuff (nothing like pointers to pointers or structs).

I got the md5 algorithm here.

I included the files md5.c and md5.h in my C project (using codeblocks) but the only problem is that I don't really understand how to use it. I have read and re-read the code and I don't understand how I use those functions to turn 'example' into a MD5 hash.

I haven't done C programming in a while (mostly php) so I am a bit lost here. Basically what I am asking is for some examples of usage. They are provided via the md5main.c file but I don't understand them.

Am I aiming high here? Should I stop all this and start reading the C book again or can anyone give me some pointers and see if I can figure this out.

Thanks.

+5  A: 

You should stop all this and start reading the C book again.

My experience is that when I am trying to learn a new programming language, it's not practical to try implementing a complex project at the same time. You should do simple exercises in C until you are comfortable with the language, and then tackle something like implementing MD5 or integrating an existing implementation.

By the way, reading code is a skill different from writing code. There are differences between these two skills, but both require that you understand the language well.

Bill Karwin
I do know C, but apparently not that well :(
AntonioCS
Well, the idea works just as well for "refreshing myself of a language I haven't used in a while." You have to walk before you run.
Bill Karwin
You are right. :)
AntonioCS
+2  A: 

I think you picked about the worst thing to look at (by no fault of your own). Encryption and hash type algorithms are going to make the strangest use of the language possible to do the type of math they need to do quickly. They are almost guaranteed to be obfuscated and difficult to understand. Plus, you will need to get bogged down in math in order to really understand them.

If you just want a hashing algorithm, get a well-known implementation and use it as a black box. Don't try and implement it yourself, you will almost certainly introduce some cryptographic weakness into the implementation.

Edit: To be fully responsive if you want great books (or resources) on encryption, look to Bruce Schneier. Applied Cryptography is a classic.

JP Alioto
+1 I agree MD5 and algorithms like it are intricate. These aren't the best code examples to study for someone coming up to speed on a programming language.
Bill Karwin
Thanks for the advice. I am really going to reread the C book and try to grasp again binary and hex stuff
AntonioCS
+5  A: 

While I agree with Bill, you should go back to the C book if you want to really understand what you're doing. But, in an effort to help, I've modified and commented some of the code from md5main.c...

const char* testData = "12345";  // this is the data you want to hash
md5_state_t state;  // this is a state object used by the MD5 lib to do "stuff"
                    // just treat it as a black box
md5_byte_t digest[16];  // this is where the MD5 hash will go

// initialize the state structure
md5_init(&state);

// add data to the hasher
md5_append(&state, (const md5_byte_t *)testData, strlen(testData));

// now compute the hash
md5_finish(&state, digest);

// digest will now contain a MD5 hash of the testData input

Hope this helps!

Bert Lamb
Thanks for this.I tried your code and I got this -> é|╦♫ÛèplL4íhæ°N{I am running this on winxp on console win. Does it have something to do with the character coding?
AntonioCS
so the digest is binary, in other words the digest array is full of 16 bytes of data (not 16 characters), if you want it in a printable format then you could use this loop to print it out as a hex string: for (int di = 0; di < 16; ++di) printf("%02x", digest[di]);
Bert Lamb
of course! Thanks it is working now :)
AntonioCS