Delphi: How to calculate the SHA hash of a large file

delphisha

Hi I need to generate a SHA over a 5 Gig file

Do you know of a non string based Delphi library that can do this ?

Best Solution

You should use DCPcrypt v2 and read your file buffered and feed the SHA hasher with the buffer until you've read the complete 5GB file.

If you want to know how to read a large file buffered, see my answer about a file copy using custom buffering.

so in concept (no real delphi code!):

function GetShaHash(const AFilename: String)
begin
  sha := TSHAHasher.Create;
  SetLength(Result, sha.Size);
  file := OpenFile(AFilename, GENERIC_READ);
  while not eof file do
  begin
     BytesRead := ReadFile(file, buffer[0], 0, 1024 * 1024);
     sha.Update(buffer[0], BytesRead);
  end;
  sha.Final(Result[0]); 
  CloseFile(file);
end;