Page 1 of 1
Correct way to use MD5
Posted: Tue Feb 22, 2005 11:26 pm
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!
Posted: Tue Feb 22, 2005 11:46 pm
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????
Posted: Wed Feb 23, 2005 1:26 am
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...
Posted: Wed Feb 23, 2005 5:25 pm
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?
Posted: Wed Feb 23, 2005 5:45 pm
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

Posted: Wed Feb 23, 2005 5:45 pm
by Fred
Yes, MD5 takes an abitraty buffer, binary or not, so you can do a checksum on anything.