Community Forum

Create the md5 Hash Value with C++

Forum Overview >> Farming Simulator 2011

CategoryFarming Simulator 2011
Created30.12.2010 01:26


Dirk Buck (Unknown) 30.12.2010 01:30
Hello everybody,
i have tried to generate the equal MD5Hash for my Modfiles as LS11 it does for better organisate my files.

I use MD5 algorithm in c++.
I have found the article on ls-mods.de for generating this in php, but when i add the filename without extension in c++ i cant get the same hash.

Php is not a good solution if there are big files like modded maps with 100 and more MB in size.

I have tried everything
- The filename
- the filename lowercase
- the filename uppercase
- the filename w/o extension

If there is everyone who can help me ?

Greetings and hope for help.
DBuck


Christian Ammann - GIANTS Software 30.12.2010 12:40
Hi Dirk,

Can you post the code you used to generate the MD5 hash.

Cheers,
Chris

Dirk Buck (Unknown) 31.12.2010 00:36
OK Thx for Answering.

Here my Code

int md5filefn (char *fn , unsigned long seed , MD5_CTX_1 *mdContext, char *fnshort )
{
unsigned long size = 0;
FILE *f = fopen ( fn , "rb" ) ;
if ( f == NULL ) return 0 ;

MD5Init ( mdContext , seed ) ;

char buf[2048] ;
unsigned int trb = 0 ;
for (;;) {
int rb = fread ( buf , 1 , 2048 , f ) ;
if ( size > 0 && rb + trb > size ) rb = size - trb ;
trb += rb ;
MD5Update ( mdContext , (unsigned char *) buf , rb ) ;
if ( rb < 2048 || ( size > 0 && trb >= size ) ) break ;
}
MD5Update ( mdContext , (unsigned char *) fnshort , sizeof(fnshort ) ) ;
fclose ( f ) ;
MD5Final ( mdContext ) ;

return trb ;
}

After the For Loop ein add the filename without extension.
The MD5Update function updates the diggest.
mdcontext is an struct
typedef struct {
UINT4 i[2]; /* Number of _bits_ handled mod 2^64 */
UINT4 buf[4]; /* Scratch buffer */
unsigned char in[64]; /* Input buffer */
unsigned char digest[16]; /* Actual digest after MD5Final call */
} MD5_CTX_1;

The function is right for normal MD5 Hashes. I have compared the hash with other programms, Total Commander and md5sum


Christian Ammann - GIANTS Software 31.12.2010 13:12
Hi Dirk,

I can't see any obvious bugs in your code snippet. Please post your full code so that we can run it and find the bug.

Cheers,
Chris

Dirk Buck (Unknown) 31.12.2010 16:51
OK here is my hole code

first my for md5 creation
http://pastebin.de/13446

And here is the part i call the function to create the md5 for my files
http://pastebin.de/13447

For Example
Here is the Hash LS11 created ingame
2a2afce351edf0c19016e7a85998197e

This is the Hash without modification
bb8f3496801f2e62142bf773a96fa26e

This is the hash i became if i add the filename (mapViewerMvp)
9bb50c34bc6deac56f109448e85ab485


I hope thats enough information.

Greets
DBuck

Dirk Buck (Unknown) 31.12.2010 17:16
Ok,
i have found the fault.

I have used
MD5Update ( mdContext , (unsigned char *) fnshort , sizeof(fnshort ) ) ;

Here is the fault in sizeof(fnshort)
it return 4 but the string is 12.

Now i have used strlen(fnshort)
and it works.

Greetings
DBuck


Note: Log in to post. Create a new account here.