Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 05-19-2017, 04:54 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 254
Default Get same MD5 with Delphi5 and dotNet

Dear Expert,

I am working in Delphi5 and DotNet 2015 projects and I need to get same MD5 for a string in both delphi5 and dotnet . Kindly suggest how shall i achieve this.

Thanks in advance.
__________________
Rmt
  #2  
Old 05-19-2017, 08:58 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,218
Default

This is what I use in an old Delphi project to render a MD5 from string:
Code:
uses
  Wcrypt2;

function StrToMd5(AValue: String): String;
var
  provider: HCRYPTPROV;
  hash: HCRYPTHASH;
  bytehash: array[0..$7f] of Byte;
  count: Cardinal;
  content: PByte;
  i: Integer;
begin
  count := 16;
  content := Pointer(PChar(AValue));
  Result := '';

  if CryptAcquireContext(@provider, nil, nil, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT or CRYPT_MACHINE_KEYSET) then
  begin
    if CryptCreateHash(provider, CALG_MD5, 0, 0, @hash) then
    begin
      if CryptHashData(hash, content, Length(AValue) * SizeOf(Char), 0) then
      begin
        if CryptGetHashParam(hash, HP_HASHVAL, @bytehash[0], @count, 0) then
        begin
          for i := 0 to count - 1 do
            Result := Result + Format('%.2x', [bytehash[i]]);
        end;
      end;
      CryptDestroyHash(hash);
    end;
    CryptReleaseContext(provider, 0);
  end;

  Result := AnsiLowerCase(Result);
end;
wcrypt2 is part of project JEDI, you only need this .pas since it doesn't reference to other JEDI stuff.
My best guess is that I use this version

As for the .NET part, that's easy (just a copy/paste of a random md5 google reply):
Code:
        public static string CreateMD5(string input)
        {
            using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
            {
                byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
                byte[] hashBytes = md5.ComputeHash(inputBytes);

                // Convert the byte array to hexadecimal string
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    sb.Append(hashBytes[i].ToString("X2"));
                }
                return sb.ToString();
            }
        }
  #3  
Old 05-19-2017, 10:22 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 254
Default

Dear Norrit,

Thank you so much for the code. Its working as desired. Your help is highly appreciated. Thanks once again
__________________
Rmt

Last edited by rmt; 05-19-2017 at 11:06 AM.
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 03:50 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.