views:

1240

answers:

9
+2  Q: 

File MD5 checksum

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!

A: 

Most programming languages have libraries available for computing the MD5 of a string or byte array. In some languages, they're even part of the standard library. I'm not at all familiar with Delphi, so I don't know if it exists for Delphi, but I'd say it's likely that it does. Search for hashing and/or crypto libraries for Delphi.

rmeador
+3  A: 

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.

dummzeuch
+1  A: 

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.

Rob Kennedy
+1  A: 

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.

eleven81
A: 

MessageDigest_5 would work for this as well.

Jim McKeeth
Is that available in Delphi 7? I don't have that version, hence the uncertainty in the answer I gave.
Rob Kennedy
@Rob: MessageDigest_5 has been available since Delphi 2005, but the IdHashMessageDigest.pas has been included since Delphi 7: http://wiert.wordpress.com/2009/12/11/delphi-md5-the-messagedigest_5-unit-has-been-there-since-delphi-2007/
Jeroen Pluimers
+4  A: 

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;
Ricardo Acras
To convert the hash to a hexstring you can also use: TIdHashMessageDigest5.AsHex(hash);
The_Fox
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);
Sebastian
@Sebastian - HashStreamAsHex returns a string - and a typecast is not possible. How the code above should be ported to Indy 10?
Ronaldo Junior
A: 

The easiest (and possible only way) to calculate a file MD5 hash without having to save it, is using this API: http://www.filemd5.net/API

JohnnieWalker
A: 

In the later (st?) indy the md5 for a stream can be obtained by the method HashStreamAsHex(s), wheres is stream.

mazjac
A: 

Hi there,

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

Oszkar Olah