Click to See Complete Forum and Search --> : Storing md5 value in a byte variable in C++


karthik
05-25-2004, 01:44 AM
Hi everyone,

I am doing some cryptographic calculations and wish to store the MD5 value of a file in a variable. I am implementing the program in c++ on linux with gcc 3.3.1 on it.

I need to do a byte comparison of this value stored in the variable with the freshly computed MD5 value of the file during the execution of the program.

I am lost on how to store this value inside the program and use bcmp() on it. I could do it dynamically but i want to store the value in a variable inside the program itself.

Any pointers and help would be really helpful.

TIA

Karthik

jim mcnamara
05-25-2004, 10:24 AM
Unless you are running only on BSD systems, consider using memcmp() instead. bcmp() may go away in the future.

This help at all?


unsigned char test_value[17];
....................
if ( ! memcmp(test_value, known_value,16) ..........

karthik
05-25-2004, 12:59 PM
thanx for u'r reply jim.

The problem that i am facing is not the comparison part but storing the known_value from your example inside the program itself. For comparing with the fresh test_value that is generated during execution i need to store known_value inside the program text itself permanently. That is my problem now.. how to store the value ?

Thanks

karthik

jim mcnamara
05-25-2004, 03:37 PM
since I hate typing assume your known_value is an md5 hash result 16 bytes long: 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d ff 11 11
Try:

#include <stdio.h>

int main(int argc, char *argv[]) {
unsigned char known_value[17]=
{ 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c,
0x0d, 0xff, 0x11, 0x11, 0x00 };

int i;
// proof the array is correct --
for (i=0;i<16;i++){
printf( "%x ", known_value[i]);
}
printf("\n");

return 0;
}

karthik
05-26-2004, 12:57 AM
that worked like a charm ..thanx a lot jim

karthik