Dismiss Notice

REGISTRATION IS AFTER ADMIN ACTIVATION

DONATIONS WITH PAYPAL CLICK TO BUTTON

1 MONTH VIP - 5$; 3 MONTHS VIP - 10$; 6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$

DONATIONS WITH Bitcoin Address:3NRs3CK3fhXifrNYxHEZKpETDd9vNLMsMD

Dismiss Notice
The registration is approved by the Administrator. It takes about 1 day to approve your registration
Dismiss Notice
For open hidden message no need write thanks, thank etc. Enough is click to like button on right side of thread.

MD5 Hash Strings, Streams, And Files In Delphi XE5

Discussion in 'Delphi Android And IOS' started by N0body, Mar 14, 2014.

  1. N0body
    Online

    N0bodyN0body is a Verified Member DelphiFan Administrator Staff Member DF Staff

    If you are building any kind of login system or you need file signatures in Delphi XE5 Firemonkey you may be looking for an MD5 hash function. MD5 hash functionality ships with Delphi XE5 in the Indy component set. An MD5 hash is basically a string of unique characters that will represent the data you give it whether it is a password or an MP3 file. However, the code to access it is rather long which is where this library called hashutil comes in. The library should be cross platform and run on Windows, Android, IOS, and OSX. It provides some wrapper functions around the MD5 hash functionality which makes it easier to use. There is a function for a straight string, a stream, and a file. These three functions cover a lot of the usage and formats that you might need an MD5 hash for. You could for example get the MD5 hash for a list of MP3 files and use the hash as a fingerprint to see if there are duplicate files.. You could also create an MD5 hash of a password so that you are not storing the actual password in clear text. Here is a sample function:

    Code:
    function MD5(S: String): String;
    begin
    with TIdHashMessageDigest5.Create do
    begin
    Result := HashStringAsHex(S);
    DisposeOf;
    end;
    end;

    Until:
    Code:
    unit hashutil;
     
    interface
     
    uses SysUtils, Classes, IdGlobal, IdHash, IdHashMessageDigest, IdHashSHA, IdHashCRC;
     
    function MD5(S: String): String; overload;
    function MD5(S: TStream): String; overload;
    function MD5_Bytes(S: String): TIdBytes;
    function MD5_File(AFilename: String): String;
     
    implementation
     
     
    function MD5_File(AFilename: String): String;
    var
      fs: TFileStream;
    begin
      with TIdHashMessageDigest5.Create do
      begin
        fs := TFileStream.Create(AFilename, fmOpenRead);
        try
          Result := HashStreamAsHex(fs);
        finally
          fs.Free;
        end;
        Free;
      end;
    end;
     
    function MD5_Bytes(S: String): TIdBytes;
    begin
      with TIdHashMessageDigest5.Create do
      begin
        Result := HashString(S);
        Free;
      end;
    end;
     
    function MD5(S: String): String;
    begin
      with TIdHashMessageDigest5.Create do
      begin
        Result := HashStringAsHex(S);
        Free;
      end;
    end;
     
    function MD5(S: TStream): String;
    begin
      with TIdHashMessageDigest5.Create do
      begin
        Result := HashStreamAsHex(S);
        Free;
      end;
    end;
     
    end.
     
  2. martinflower
    Offline

    martinflower Guest

    [font='Open Sans', Arial, Tahoma, Verdana, sans-serif]Thank[/font][font='Open Sans', Arial, Tahoma, Verdana, sans-serif] [/font][font='Open Sans', Arial, Tahoma, Verdana, sans-serif]you very much[/font][font='Open Sans', Arial, Tahoma, Verdana, sans-serif]![/font]
     

Share This Page

Laws
Our website, law, laws, copyright and aims to be respectful of individual rights. Our site, as defined in the law of 5651 serves as a provider of space. According to the law, contrary to the law of site management has no obligation to monitor content. Therefore, our site has adopted the principle of fit and remove. Works are subject to copyright laws and is shared in a manner that violated their legal rights, or professional associations, rights holders who, adsdelphi@gmail.com I can reach us at e-mail address. Complaints considered to be infringing on the examination of the content will be removed from our site.
Donate Help To Us and Be VIP
DONATIONS WITH PAYPAL CLICK TO BUTTON
6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$
Social Block