Wrong MD5 Checksum in PB 4.51 (MacOS X x86)

Everything else that doesn't fall into one of the other PB categories.
Karl-Uwe Frank
User
User
Posts: 17
Joined: Sat Sep 03, 2011 12:33 am

Wrong MD5 Checksum in PB 4.51 (MacOS X x86)

Post 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
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

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

Post 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.
BERESHEIT
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

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

Post 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
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

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

Post 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)
Last edited by STARGÅTE on Sat Sep 03, 2011 10:34 am, edited 2 times in total.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

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

Post 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
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

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

Post by STARGÅTE »

Thanks nco2k, I keep forgetting the PokeS still adds a NULL (unlike WriteString)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Karl-Uwe Frank
User
User
Posts: 17
Joined: Sat Sep 03, 2011 12:33 am

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

Post 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?
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

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

Post by nco2k »

Karl-Uwe Frank wrote:P.S.: Am I allowed to use your code in my projects?
of course.

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Karl-Uwe Frank
User
User
Posts: 17
Joined: Sat Sep 03, 2011 12:33 am

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

Post by Karl-Uwe Frank »

@nco2k

Ahh, thanks a lot.

Cheers,
Karl-Uwe
Post Reply