Page 1 of 1

Wrong MD5 Checksum in PB 4.51 (MacOS X x86)

Posted: Sat Sep 03, 2011 12:50 am
by Karl-Uwe Frank
Hi,

I am currently testing PB and just at the beging of my "journey" I am unable to compute a correct MD5 checksum.

When I request the MD5 checksum for

Code: Select all

 "Password" it normally reads dc647eb65e6711e155375218212b3964
This is true for PHP, JavaScript, RealBasic, Linux and MacOSX console, with OpenSSL but not with PB?!

I have tried this

Code: Select all

cTEST$ = "Password"
Debug "MD5 Fingerprint = " + MD5Fingerprint(@cTEST$, Len(cTEST$))
Debug "MD5 Fingerprint = " + MD5Fingerprint(@cTEST$, StringByteLength(cTEST$))

*Buffer = AllocateMemory(500)    
PokeS(*Buffer, "Password")

MD5$ = MD5Fingerprint(*Buffer, MemorySize(*Buffer))
Debug "MD5 Fingerprint = " + MD5$

MD5$ = MD5Fingerprint(*Buffer, MemoryStringLength(*Buffer))
Debug "MD5 Fingerprint = " + MD5$
and I end up with 3 different MD5 checksum's ???!!!

Code: Select all

MD5 Fingerprint = dc76c9dda4c8ab15bd1797c6dbe1ad99
MD5 Fingerprint = fcd9449df6e7e5adaa69b9cf32860e14
MD5 Fingerprint = 02e2999807f8ca7291cc17d42cb4c8e2
MD5 Fingerprint = dc76c9dda4c8ab15bd1797c6dbe1ad99
Maybe some-one is able to push me in the right direction because I do not understand the reason for this odd behaviour.

Thanks and cheers,
Karl-Uwe

Re: Wrong MD5 Checksum in PB 4.51 (MacOS X x86)

Posted: Sat Sep 03, 2011 2:07 am
by netmaestro
Your first code:

Code: Select all

cTEST$ = "Password"
Debug "MD5 Fingerprint = " + MD5Fingerprint(@cTEST$, Len(cTEST$))
should do the trick and it works here in PB 4.6 b4 windows x86. If Mac is returning something different, there's probably a bug in the Purebasic MacOS routine for this command.

Re: Wrong MD5 Checksum in PB 4.51 (MacOS X x86)

Posted: Sat Sep 03, 2011 2:26 am
by Guimauve
With the code posted by netmaestro I get this :

Code: Select all

MD5 Fingerprint = dc647eb65e6711e155375218212b3964
Linux Mint 11 x64
PureBasic 4.60 Beta 4 x64

Best regards.
Guimauve

Re: Wrong MD5 Checksum in PB 4.51 (MacOS X x86)

Posted: Sat Sep 03, 2011 2:34 am
by STARGÅTE
1.
dc76c9dda4c8ab15bd1797c6dbe1ad99
is the MD5 from "Password" in Unicode with wrong length (only 8Byte instead of 16)

2.
fcd9449df6e7e5adaa69b9cf32860e14
is the MD5 from "Password" in Unicode with right length (16 Byte), but its wrong for the MD5 ...

3.
02e2999807f8ca7291cc17d42cb4c8e2
thats nonsense! MemorySize() gives 500 Bytes ... so MD5 from 500 Bytes


so the only way with Ascii and Unicode is:

Code: Select all

Procedure.s MD5(String.s)
	Protected *Buffer, MD5.s, Length.i = StringByteLength(String, #PB_Ascii)
	*Buffer = AllocateMemory(Length+SizeOf(Unicode))
	PokeS(*Buffer, String, #PB_Default, #PB_Ascii)
	MD5 = MD5Fingerprint(*Buffer, Length)
	FreeMemory(*Buffer)
	ProcedureReturn MD5
EndProcedure

Debug "MD5 Fingerprint = " + MD5("Password")
MD5 Fingerprint = dc647eb65e6711e155375218212b3964
Edit: Add SizeOf(Unicode)

Re: Wrong MD5 Checksum in PB 4.51 (MacOS X x86)

Posted: Sat Sep 03, 2011 6:04 am
by nco2k
@STARGÅTE
you forgot to make room for the nullbyte.
PokeS wrote:Writes a string (including the ending '0') to the specified memory address.
Error wrote:Line 4: Overflow in a dynamically allocated memory block.
but why not make it more flexible?

Code: Select all

Procedure$ MD5StringFingerprint(String$, Flag=#PB_Ascii)
  Protected Result$, *StringBuffer, StringByteSize, NullByteSize
  Select Flag
    Case #PB_Ascii, #PB_Unicode, #PB_UTF8
      If Flag = #PB_Unicode
        NullByteSize = SizeOf(Word)
      Else
        NullByteSize = SizeOf(Byte)
      EndIf
      StringByteSize = StringByteLength(String$, Flag)
      *StringBuffer = AllocateMemory(StringByteSize + NullByteSize)
      If *StringBuffer
        If PokeS(*StringBuffer, String$, #PB_Default, Flag) = StringByteSize
          Result$ = MD5Fingerprint(*StringBuffer, StringByteSize)
        EndIf
        FreeMemory(*StringBuffer)
      EndIf
  EndSelect
  ProcedureReturn Result$
EndProcedure

Debug "[Password]"
Debug "Ascii: "+MD5StringFingerprint("Password", #PB_Ascii)
Debug "Unicode: "+MD5StringFingerprint("Password", #PB_Unicode)
Debug "UTF8: "+MD5StringFingerprint("Password", #PB_UTF8)
Debug ""
Debug "[Pãsswõrd]"
Debug "Ascii: "+MD5StringFingerprint("Pãsswõrd", #PB_Ascii)
Debug "Unicode: "+MD5StringFingerprint("Pãsswõrd", #PB_Unicode)
Debug "UTF8: "+MD5StringFingerprint("Pãsswõrd", #PB_UTF8)
Debug ""
Debug "[Empty]"
Debug "Ascii: "+MD5StringFingerprint("", #PB_Ascii)
Debug "Unicode: "+MD5StringFingerprint("", #PB_Unicode)
Debug "UTF8: "+MD5StringFingerprint("", #PB_UTF8)
c ya,
nco2k

Re: Wrong MD5 Checksum in PB 4.51 (MacOS X x86)

Posted: Sat Sep 03, 2011 10:24 am
by STARGÅTE
Thanks nco2k, I keep forgetting the PokeS still adds a NULL (unlike WriteString)

Re: Wrong MD5 Checksum in PB 4.51 (MacOS X x86)

Posted: Sat Sep 03, 2011 11:47 am
by Karl-Uwe Frank
@STARGÅTE

Thanks a lot for the explanation and the sample source code, it really help me to understand how to program in PB and why I got different MD5 checksums.

Regarding 3.
I took this listing form the PB online documentation
http://www.purebasic.com/documentation/ ... print.html

My thought was that I could rely on it because I do not have any experience in programming with memory buffers and Peek and Poke :? I am an application progammer manly with X-Base, so now with your, "nco2k" and the forum help I might by getting on with PB.


@nco2k

Thanks for the good example about all the differences with character codings and the very useful procedure.

Cheers,
Karl-Uwe

P.S.: Am I allowed to use your code in my projects?

Re: Wrong MD5 Checksum in PB 4.51 (MacOS X x86)

Posted: Sat Sep 03, 2011 12:11 pm
by nco2k
Karl-Uwe Frank wrote:P.S.: Am I allowed to use your code in my projects?
of course.

c ya,
nco2k

Re: Wrong MD5 Checksum in PB 4.51 (MacOS X x86)

Posted: Sat Sep 03, 2011 2:45 pm
by Karl-Uwe Frank
@nco2k

Ahh, thanks a lot.

Cheers,
Karl-Uwe