1. Home
  2. Computing & Technology
  3. Delphi Programming

MD5 Hashing in Delphi
Calculate MD5 Checksum for a File or String using Delphi

By , About.com Guide

The MD5 Message-Digest algorithm is a cryptographic hash function. MD5 is commonly used to check the integrity of files. An MD5 hash value is a 32 digit hexadecimal number.

MD5 for files

Using Delphi, you can easily create a function to calculate the MD5 hash for a given file. All you need is included in two units (part of Indy): IdHashMessageDigest and idHash.
uses IdHashMessageDigest, idHash;

//returns MD5 has for a file
function MD5(const fileName : string) : string;
var
  idmd5 : TIdHashMessageDigest5;
  fs : TFileStream;
  hash : T4x4LongWordRecord;
begin
  idmd5 := TIdHashMessageDigest5.Create;
  fs := TFileStream.Create(fileName, fmOpenRead OR fmShareDenyWrite) ;
  try
    result := idmd5.AsHex(idmd5.HashValue(fs)) ;
  finally
    fs.Free;
    idmd5.Free;
  end;
end;
You can use MD5 file hashing to enusure that after a (some kind of file) transfer the file has arrived intact.
Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. MD5 Hashing in Delphi - Calculate MD5 Checksum for a File or String using Delphi

©2009 About.com, a part of The New York Times Company.

All rights reserved.