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

前端 未结 5 698
囚心锁ツ
囚心锁ツ 2021-02-08 05:18

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 ?

5条回答
  •  终归单人心
    2021-02-08 05:25

    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;
    

提交回复
热议问题