MD5FingerPrint in Unicode

Just starting out? Need help? Post your questions and find answers here.
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: MD5FingerPrint in Unicode

Post by luis »

STARGÅTE wrote: The MD5Fingerprint() is a function for a memory buffer (not directly for strings!).
So, it is not the job of this memory function to "change" the format of the buffer.
Yep, that's the point, I agree. What you store in the memory buffer it's up to you.
STARGÅTE wrote:It's a bad habit to use strings as memory buffer.
When compiling to both unicode and ascii is more cumbersome, else can have its advantages (you can use string commands on that buffer, it auto-deallocates itself when leaving the procedure, etc.).

As long as you know what you are doing, it's not necessarily a bad habit IMO.
"Have you tried turning it off and on again ?"
A little PureBasic review
Little John
Addict
Addict
Posts: 4782
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: MD5FingerPrint in Unicode

Post by Little John »

STARGÅTE wrote:
Little John wrote:Howevr, IMHO PB's built-in MD5Fingerprint() function just should provide the option to pass a format parameter.
Then it wouldn't be necessary for us to write a wrapper in order to get this important option.
( But this is a feature request by me. I can't see a bug here. )
The MD5Fingerprint() is a function for a memory buffer (not directly for strings!).
So, it is not the job of this memory function to "change" the format of the buffer.
That's why
Little John wrote:( But this is a feature request by me. I can't see a bug here. )
My feature request was to extend the functionality of PB's MD5Fingerprint() function.
STARGÅTE wrote:This is also a rule for all other functions such as:
CRC32Fingerprint, SHA1Fingerprint, AESDecoder, AESEncoder, Base64Decoder, Base64Encoder and so on.
Yes, I know.
And we also know, that people post the same problems with these functions here on the forum again and again for years.
Why not extend those functions, in order to make them more flexible?
STARGÅTE wrote:It's a bad habit to use strings as memory buffer.
I think it depends on the context.
In some cases it's certainly a bad idea (e.g. when the buffer contains zeros), in other cases it might have its advantages.

If it is actually a bad habit, then at least examples for doing so should be removed from the PB docs. :mrgreen:
E.g.:
http://www.purebasic.com/documentation/ ... coder.html
http://www.purebasic.com/documentation/ ... coder.html
http://www.purebasic.com/documentation/ ... print.html
(These examples are showing incorrect use of strings as buffers!)

BTW: For important stuff, it's best not to use MD5 at all. :mrgreen:
http://www.purebasic.fr/english/viewtop ... 37&t=61364
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Re: MD5FingerPrint in Unicode

Post by Tranquil »

Hello Forum.

I have the same problem as the starter of this thread and I did not found a solution for this right now.
To convert the sting to an ASCII string can not be the solution. For eg I want the hash of the following character: "Æ", how do I calculate this?
It can not be stored in a one Ascii Byte, so what to do?
Tranquil
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Re: MD5FingerPrint in Unicode

Post by Tranquil »

Got it!

Converting to UTF8 in Unicode Mode gives the correct answers.
This is what I use now for further projects. It returns the always correct value, even for unicode strings.

Code: Select all

pwd$="ÝßÕýý"
*mem = AllocateMemory(StringByteLength(pwd$,#PB_UTF8))
PokeS(*mem,pwd$,-1,#PB_UTF8 | #PB_String_NoZero)
Debug SHA1Fingerprint(*mem,MemorySize(*mem))
Tranquil
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: MD5FingerPrint in Unicode

Post by Trond »

You need to call FreeMemory(*mem), else your program will take up a tiny bit more memory every time it runs that code.
Post Reply