Recent

Author Topic: Wrong byte order returned by TIdHashMessageDigest5.HashStringAsHex...  (Read 10270 times)

mvaldez

  • New Member
  • *
  • Posts: 10
Hi. This is not really a Lazarus-specific question, and not a networking-specific question, but as I'm using Lazarus and this problem is with the Indy 10 lib so I thought this was a good place to start asking if anybody have seen this problem: I am using TIdHashMessageDigest5.HashStringAsHex to hash a string with MD5, but the resulting hex string does not match the expected string; the bytes are in the wrong order.

For example, for the string "test" the expected MD5 hash (as hex) is 098F6BCD4621D373CADE4E832627B4F6, but I'm getting CD6B8F0973D32146834EDECAF6B42726, which is similar but with each 4-bytes group reversed (in this example, 098F6BCD as CD6B8F09, 4621D373 as 73D32146, and so on).

My minimal case looks is this...

a form with two fields and a button:
Code: [Select]
  TForm1 = class(TForm)                     
    BitBtn1: TBitBtn;                       
    Edit1: TEdit;                           
    Edit2: TEdit;                           
    procedure BitBtn1Click(Sender: TObject);
  private                                   
    { private declarations }               
  public                                   
    { public declarations }                 
  end;

and the implementation:

Code: [Select]
uses IdHashMessageDigest;

procedure TForm1.BitBtn1Click(Sender: TObject);
VAR
  MD5Hasher : TIdHashMessageDigest5;
begin
  MD5Hasher := TIdHashMessageDigest5.Create;
  Edit2.Text := MD5Hasher.HashStringAsHex (Edit1.Text);
  FreeAndNIL (MD5Hasher);
end;

It looks like an endianess problem, but I am guessing.

I'm using Lazarus 0.9.28.2-8ubuntu1 r22277 FPC 2.4.0 i386-linux-gtk 2 (beta) in Ubuntu 10.04. The Indy version is 10.2.0.3 (which it seems a bit old).

So, has anybody faced this problem before (with MD5 or any other hashing in Indy)?




Thanks in advance,

MV


mvaldez

  • New Member
  • *
  • Posts: 10
Well, I decided to use the MD5String and MD5Print functions from the MD5 FP unit, as I could not find a solution to this problem. I read there was a problem with endianess in the Indy hashing functions but for an old version of Indy 10... maybe that's the problem. I was tempted to try the Indy SVN nightly builds (the Tiburon branch) but I don't want more surprises doing this migration (I'm migrating this application from Delphi to Lazarus and from Indy 9 to Indy 10).

Just for documentation purposes, I used the following code to get the expected result:
Code: [Select]
UPPERCASE (MD5Print (MD5String (Str2Hash)))

As for why I was using an Indy function for a MD5 hash... well, my original Delphi app relied on Indy for networking and I needed a simple hashing without additional units or components, so it seemed like a good idea back then. Now in Lazarus, I am discovering there is already this FreePascal MP5 unit, so I'll use it. (I really need to recheck all FP packages and Lazarus components available by default.)


Regards,

MV

 

TinyPortal © 2005-2018