I’m using Delphi XE2 with FireMonkey.
I have read many other questions about MD5 but didn’t find an answer to the issue I have…
I’ve also tested different scripts to generate a MD5 from Delphi:
function MD5(const text: string) : string;
var
md5 : TIdHashMessageDigest5;
begin
md5 := TIdHashMessageDigest5.Create;
Result := LowerCase(md5.HashStringAsHex(text, TEncoding.ANSI));
md5.Free;
end;
or
function MD5(const text: String) : string;
var
md5: IMD5;
begin
md5:= GetMD5;
md5.Init;
md5.Update(TByteDynArray(RawByteString(AnsiString(text))), Length(AnsiString(text)));
Result := LowerCase(md5.AsString);
end;
Both give me the same results… which is often the same I have within PHP:
$md5 = md5($toencode);
$md5 = hash("md5", $toencode);
But the results are different if I use Delphi or PHP, when I request the MD5 of one of these chars: " or '
There are maybe others chars which generate different results but I just find these ones during my tests…
I have tested with many extended ASCII characters and have the same MD5…
So, I don’t understand why I have differences with few of them (" or ' ), and I’d like to know if there’s a way to always generate the PHP MD5 result from Delphi, whatever the chars are.
Any idea?
For instance:
with Delphi:
“: b15835f133ff2e27c7cb28117bfae8f4
‘: 3590cb8af0bbb9e78c343b52b93773c9
with PHP:
“: 3bd864034f446da13581129bb17f9191
‘: 024c94d6e03b6f67a86b952b914816c7
Formalizing this question.. I’ve find the answer… BTW, I post it as others may have the same issue…
" and ' are escaped in PHP… so, within Delphi, I have to encode \" and \'… if you think I have forgotten chars or if you want to add some details, do not hesitate…
The PHP
hashfunction implements MD5 perfectly well.The following PHP:
results in this output:
So, the top two values are what you get from your Delphi code, and the same values that Ignacio obtained from Python. So, there’s absolutely no reason to believe that the PHP MD5 code is at fault. And the bottom two values are what your PHP code is returning.
Which leaves us to conclude that you are feeding your Delphi code different input from that which you feed to your PHP code. If you feed them both the same input, you will get the same output.