1. Technology

MD5 Hashing in Delphi

Calculate MD5 Checksum for a File or String using Delphi

By

Young Asian businesswoman working on laptop in board room
Steve Debenport/E+/Getty Images

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.

©2014 About.com. All rights reserved.