MD5 검사

1436 단어 delphi
uses IdHashMessageDigest, IdHash; 
{         MD5  }

function MD5Stream(const Stream: TStream): string;
var
  MyMD5: TIdHashMessageDigest5;
  Digest: T4x4LongWordRecord;
begin
  Result := '';
  try
    try
      MyMD5 := TIdHashMessageDigest5.Create;
      Stream.Position := 0;
      Digest := MyMD5.HashValue(Stream);
      Stream.Position := 0;
      Result := MyMD5.AsHex(Digest);
    except
      Result := '';
    end;
  finally
    if Assigned(MyMD5) then
      FreeAndNil(MyMD5);
  end;
end;
{      MD5  }

function MD5File(const LocalFile: string): string;
var
  Stream: TFileStream;
begin
  Result := '';
  if not FileExists(LocalFile) then
    exit;
  try
    try
      Stream := TFileStream.Create(LocalFile, fmOpenRead);
      Result := MD5Stream(Stream);
    except
      Result := '';
    end;
  finally
    if Assigned(Stream) then
      FreeAndNil(Stream)
  end;
end;
{      MD5 }

function MD5String(const Src: string): string;
var
  MyMD5: TIdHashMessageDigest5;
  Digest: T4x4LongWordRecord;
begin
  Result := '';
  try
    try
      MyMD5 := TIdHashMessageDigest5.Create;
      Digest := MyMD5.HashValue(Src);
      Result := MyMD5.AsHex(Digest);
    except
      Result := '';
    end;
  finally
    if Assigned(MyMD5) then
      FreeAndNil(MyMD5);
  end;
end;

좋은 웹페이지 즐겨찾기