File MD5 checksum
Asked Answered
V

9

5

In this question is mentioned the wcrypt2.

What I need is simply calculate the MD5 of a file. It would be perfect if I could calculate it without having to save it because it is a downloaded file in stream format.

I would like to have the most straightforward way to do that.

Thanks!

Verleneverlie answered 15/1, 2009 at 18:55 Comment(1)
Very similar to this one: #1953429Cryolite
W
16

Here is a working code for Indy 10:

function MD5File(const FileName: string): string;
var
  IdMD5: TIdHashMessageDigest5;
  FS: TFileStream;
begin
 IdMD5 := TIdHashMessageDigest5.Create;
 FS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
 try
   Result := IdMD5.HashStreamAsHex(FS)
 finally
   FS.Free;
   IdMD5.Free;
 end;
end;

Regards, OscaR1

Worrell answered 20/10, 2010 at 11:28 Comment(1)
+1 for the fmOpenRead or fmShareDenyWrite. Too many developers forget about that.Cryolite
V
5

Based on @dummzeuch answere I wrote this function:

function getMD5checksum(s: TStream): string;
 var
  md5: TIdHashMessageDigest5;
  hash : T4x4LongWordRecord;
 begin
  md5 := TIdHashMessageDigest5.Create;
  s.Seek(0,0);
  hash := md5.HashValue(s);
  result := IntToHex(Integer(hash[0]), 4) +
            IntToHex(Integer(hash[1]), 4) +
            IntToHex(Integer(hash[2]), 4) +
            IntToHex(Integer(hash[3]), 4);
 end;
Verleneverlie answered 16/1, 2009 at 14:39 Comment(3)
To convert the hash to a hexstring you can also use: TIdHashMessageDigest5.AsHex(hash);Poulterer
IntToHex(Integer(hash[Index]), 4) will get the byte order wrong, the alternative suggested by The_Fox works correctly. For newer versions of Indy use: result := md5.HashStreamAsHex(s);Lacker
@Sebastian - HashStreamAsHex returns a string - and a typecast is not possible. How the code above should be ported to Indy 10?Circumrotate
G
3

Indy comes with functions for calculating several hashes, MD5 is one of them. Indy is included in all versions of Delphi since at least Delphi 2006 and available as a free download for older versions.

Gambill answered 15/1, 2009 at 19:42 Comment(0)
W
2

What about:

function GetFileMD5(const Stream: TStream): String; overload;
var MD5: TIdHashMessageDigest5;
begin
    MD5 := TIdHashMessageDigest5.Create;
    try
       Result := MD5.HashStreamAsHex(Stream);
    finally
       MD5.Free;
    end;
end;

function GetFileMD5(const Filename: String): String; overload;
var FileStream: TFileStream;
begin
    FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
    try
      Result := GetFileMD5(FileStream);
    finally
      FileStream.Free;
    end;
end;
Wageworker answered 27/3, 2013 at 12:42 Comment(0)
O
1

As you mentioned, the post you linked to talks about wcrypt2, which is a library of cryptographic routines, including MD5. The post you linked to also seems to indicate that it is available for Delphi 7 since the asker includes output labeled "Delphi 7." You have tagged this question delphi7, so I assume that's the version you're using, too. So what's stopping you from using wcrypt2?

The question links to a copy of wcrypt2.pas, and the copyright dates in that file appear to indicate that the unit was available by the time Delphi 7 was released. Check your installation; you might already have it. If not, then the unit also says that it was obtained via Project Jedi, so you could try looking there for the unit as well.

The answers to your referenced question include example Delphi code and the names of units that come with Delphi for doing MD5. They come with Delphi 2009, so you should check whether they're also available for your version.

Otranto answered 15/1, 2009 at 19:43 Comment(0)
P
1

Take a look at this implementation of MD5SUM in Delphi. It requires a string for input, but I imagine you can easily make it work with a stream.

Planish answered 15/1, 2009 at 19:48 Comment(0)
H
1

MessageDigest_5 would work for this as well.

Hannahhannan answered 15/1, 2009 at 21:14 Comment(2)
Is that available in Delphi 7? I don't have that version, hence the uncertainty in the answer I gave.Otranto
@Rob: MessageDigest_5 has been available since Delphi 2005, but the IdHashMessageDigest.pas has been included since Delphi 7: wiert.wordpress.com/2009/12/11/…Cryolite
L
1

I use the following function in Delphi 7 with Indy 10.1.5

uses IdHashMessageDigest, idHash, Classes;  

...

function cc_MD5File(const p_fileName : string) : string;
//returns MD5 has for a file
var
  v_idmd5 : TIdHashMessageDigest5;
  v_fs : TFileStream;
  v_hash : T4x4LongWordRecord;
begin
  v_idmd5 := TIdHashMessageDigest5.Create;
  v_fs := TFileStream.Create(p_fileName, fmOpenRead OR fmShareDenyWrite) ;
  try
    v_hash := v_idmd5.HashValue(v_fs);
    result := v_idmd5.AsHex(v_hash);
  finally
    v_fs.Free;
    v_idmd5.Free;
  end;
end;
Landgrave answered 26/8, 2017 at 12:12 Comment(1)
works: strtofile('The quick brown fox jumped over the lazy dog''s back', exepath+'foxmd5.txt'); sleep(500) writeln(cc_MD5File(exepath+'foxmd5.txt')); >>> E38CA1D920C4B8B8D3946B2C72F01680Rolf
G
0

If you use Overbyte http://www.overbyte.eu/frame_index.html just add unit and call function FileMD5 with name of file

uses OverbyteIcsMd5;
....
function GetMd5File:String; 
begin
 Result := FileMD5(FileName);
end;
Gorham answered 28/8, 2017 at 23:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.