获取文件md5值

        前面做系统自动更新时,想通过MD5值来检查文件版本差异,在网上看到了很多代码,都能计算出MD5值来。今天偶然看到indy的demo时发现indy有几个类是实现这个功能的,那就没必要自己去写代码了,直接利用起来就可以了。  下面是borland c++  和  delphi的代码:


DELPHI:

uses IdHash,IdHashMessageDigest;


function GetFileMd5Code(AFileName: string): string;
var
  LMd5: TIdHashMessageDigest5;
  S, S1: string;
  i: Integer;
  LFileStream: TMemoryStream;
  Digest: T4x4LongWordRecord;
begin
  SetLength(S, 16);

  LFileStream := TMemoryStream.Create;
  LFileStream.LoadFromFile(AFileName);

  LMd5 := TIdHashMessageDigest5.Create;
  Digest :=  LMd5.HashValue(LFileStream);
  Move(Digest, S[1], 20);

  for i := 1 to Length(S) do begin
    S1 := S1 + Format('%02x', [Byte(S[i])]);
  end;
  while Pos(' ', S1) > 0 do S1[Pos(' ', S1)] := '0';
  Result := S1;
end;



BORLAND C++:


AnsiString TForm1::GetFileMd5Code(AnsiString AFileName)
{
TIdHashMessageDigest5 *LMd5= new TIdHashMessageDigest5();
AnsiString S[16], S1;
int i;
TMemoryStream *LFileStream =  new TMemoryStream();
LFileStream->LoadFromFile(AFileName);
Sysutils::TBytes LBytes;
LBytes = LMd5->HashStream(LFileStream);


delete LMd5;
delete LFileStream;
return StringOf(LBytes);
}

你可能感兴趣的:(C++,BCB)