Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

delphi

sha

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 ?

like image 588
Charles Faiga Avatar asked Feb 16 '09 13:02

Charles Faiga


2 Answers

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;
like image 162
Davy Landman Avatar answered Oct 13 '22 02:10

Davy Landman


I would recommend Wolfgang Ehrhardt's CRC/Hash.
http://home.netsurf.de/wolfgang.ehrhardt/

It's fast and "can be compiled with most current Pascal (TP 5/5.5/6, BP 7, VP 2.1, FPC 1.0/2.0/2.2) and Delphi versions (tested with V1 up to V7/9/10)".

I've used it with D11/D12 too.

like image 24
PetriW Avatar answered Oct 13 '22 00:10

PetriW