Correct way to use MD5

Everything else that doesn't fall into one of the other PB categories.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Correct way to use MD5

Post by Killswitch »

What is the correct way to use the MD5Fingerprint() command as I get repeated results with different code, including a still repeated but varied alternative:

Code: Select all


*a=AllocateMemory(4)
PokeS(*a,"Bob")

Debug MD5Fingerprint("Bob",Len("Bob")) ;1st
Debug MD5Fingerprint(*a,Len("Bob")) ;1st
Debug MD5Fingerprint("Bob",Len("Bob")+1) ;2nd
Debug MD5Fingerprint(*a,Len("Bob")+1) ;2nd

1st = Same
2nd = Same

But:

1st<>2nd!
~I see one problem with your reasoning: the fact is thats not a chicken~
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Your code is same like this:

Code: Select all

*a=@"Bob"

Debug MD5Fingerprint("Bob",Len("Bob")) ;1st 
Debug MD5Fingerprint(*a,Len("Bob")) ;1st 
Debug MD5Fingerprint("Bob",Len("Bob")+1) ;2nd 
Debug MD5Fingerprint(*a,Len("Bob")+1) ;2nd 
As you can see *a points to the string "Bob" (null terminated), so then writing *a is the same as writing "Bob".
Where is the problem here????
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
freak
PureBasic Team
PureBasic Team
Posts: 5948
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

First, if you pass a string to a function, in reality the pointer to that string is passed.
That is, because strings are always handled through their pointers, no matter if it is a literal string or a string variable.
So when writing "MD5FingerPrint("Bob",..." you actually call the function
MD5Fingerprint with the location of the bytes 'B','o','b', followed by a 0 byte.
So technically, this is exactly the same as you did with the AllocateMemory line.
The only difference is that the one data (literal string) is statically stored at the end of the source code,
and the allocated buffer is *somewhere* in memory.
But in both ways you call the function with a pointer to a buffer with the exact same data.
That's why the results are the same.

As for the diference between the '1st' and '2nd' lines. The difference is quite simple.
"Len("Bob")" obviously returns 3. So with '1st', you are calculating the
fingerprint of the bytes 'B','o' and 'b'.
With the '2nd' way, you include one more byte, which is the terminating 0
of the string. So this is a different buffer, of course the resulting fingerprint if different.

Hope that helps...
quidquid Latine dictum sit altum videtur
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

Thanks for your help, but you haven't quite cleared up my problem. Is it correct to create a MD5Fingerprint of "Bob" or a MD5Fingerprint of "Bob" with the null terminator byte?
~I see one problem with your reasoning: the fact is thats not a chicken~
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Sorry.
I see me neither Freak understood your question.

I think it must be done without the 0 if you want to checksum just "Bob", but if you want to checksum "Bob"+Chr(0), then it must be done including the null terminator byte.
It just depends on what you want to do :wink:
Last edited by Psychophanta on Wed Feb 23, 2005 5:49 pm, edited 1 time in total.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Yes, MD5 takes an abitraty buffer, binary or not, so you can do a checksum on anything.
Post Reply