Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Delphi MD5File mit System.Hash? (https://www.delphipraxis.net/186661-md5file-mit-system-hash.html)

PeterPanino 21. Sep 2015 11:34

MD5File mit System.Hash?
 
Hallo!

What's New in Delphi and C++Builder XE8:

Zitat:

The RTL has a new unit, System.Hash. This unit includes classes and methods that allow you to use the following hash functions in your applications
Kann man mit dieser Unit indirekt den MD5-Hash einer Datei berechnen? Der THashMD5 Record publiziert ja selbst explizit keine MD5File-Methode.

Wenn nicht: Was ist die derzeit optimale (und schnellste) Methode, um mit Delphi einen MD5File Hash zu berechnen?

Der schöne Günther 21. Sep 2015 11:42

AW: MD5File mit System.Hash?
 
Noch nie damit gearbeitet (bin noch auf XE7), aber ich dachte die "Update"-Methode wäre dafür?

Delphi-Quellcode:
procedure justHashThings();
const
  filePath = 'x:\teil 1.txt';
var
  hash: System.Hash.THashMD5;
  fileBytes: TBytes;
begin
  fileBytes := TFile.ReadAllBytes(filePath);

  hash.Reset();
  hash.Update(fileBytes);

  WriteLn('The hash is ', hash.HashAsString() );
end;

PeterPanino 21. Sep 2015 12:21

AW: MD5File mit System.Hash?
 
VIELEN Dank für den Hinweis!

hab gleich eine schöne Funktion (für Copy&Paste) daraus gemacht:
Delphi-Quellcode:
function SystemHashFileMD5(const AFilePath: string; const AUpper: Boolean = True): string;
// VORHER prüfen, ob Datei existiert!
var
  hash: System.Hash.THashMD5;
  fileBytes: TBytes;
begin
  Result := '';

  try
    fileBytes := TFile.ReadAllBytes(AFilePath);
  except
    Result := 'FileError';
    EXIT;
  end;

  hash.Reset();
  hash.Update(fileBytes);

  Result := hash.HashAsString;

  if AUpper then
    Result := UpperCase(Result);
end;
Verbesserungsvorschläge?

alda 21. Sep 2015 16:12

AW: MD5File mit System.Hash?
 
Spring4D bietet eine Möglichkeit Hashes (komfortabel) generieren zu lassen: Klick mich

Delphi-Quellcode:
procedure Sample;
var
  LMD5Hasher: IMD5;
  LFileMD5Hash: String;
begin
  LMD5Hasher:= CreateMD5;
  LFileMD5Hash := LMD5Hasher.ComputeHashOfFile('Dateipfad').ToString;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:04 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz