#1 2017-12-27 16:19:03

Mokofay
Member
Registered: 2017-12-27
Posts: 3

Calculate SHA256 hash of large file

Hi Folks,

I am new to Synopse.

I would like to calculate the SHA256 hash of some large files (up to 800MB) in order to verify their integrity so converting them into strings is not really an option.

Is there any way to perform this calculation on a stream instead?

Thanks in advance

Mokofay

Offline

#2 2017-12-27 16:28:01

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Calculate SHA256 hash of large file

You can simply use the TSHA256.Update on buffers from the file.

Offline

#3 2017-12-27 16:31:36

Mokofay
Member
Registered: 2017-12-27
Posts: 3

Re: Calculate SHA256 hash of large file

I am sorry but I do not understand.

Can you please provide some sample code?

Offline

#4 2017-12-27 17:25:25

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Calculate SHA256 hash of large file

I've just added the HashFile() function which does what you expect.
See https://synopse.info/fossil/info/906b6f74c4

Offline

#5 2017-12-27 18:31:27

Mokofay
Member
Registered: 2017-12-27
Posts: 3

Re: Calculate SHA256 hash of large file

Thank you much!

Offline

#6 2021-12-29 09:47:38

George
Member
Registered: 2016-04-05
Posts: 142

Re: Calculate SHA256 hash of large file

Hi!
HashFile method accept string type instead of rawutf8.
I found issue in case when windows encoding is set to United States and file path is rawutf8 (includes utf8 cyrillic  characters).

{$mode delphi}

var
  FilePathStr: TFileName;
  Result:        RawUtf8;
begin
  FilePathStr := SrcPath; // 'C:\Program Files (x86)\'#208#161#208#181#209#128#208#178#208#181#209#128'\test.txt'
  // FilePathStr := UTF8ToString(SrcPath);
  // FilePathStr := UTF8ToSys(SrcPath);
  Result := HashFile(FilePathStr, THashAlgo.hfMD5) // empty hash
end;

Compiler options: -FcUTF8

How to pass rawutf8 to HashFile?

Last edited by George (2021-12-29 09:56:19)

Offline

#7 2021-12-29 10:03:31

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Calculate SHA256 hash of large file

Did you try Utf8ToWinAnsi(FilePathStr) ?

But anyway, if the system code page is WinAnsi 1252 then it won't be able to open a file name with cyrilic chars, which are not available in this CP1252... sad

Offline

#8 2021-12-29 10:10:48

George
Member
Registered: 2016-04-05
Posts: 142

Re: Calculate SHA256 hash of large file

Same with Utf8ToWinAnsi, cyrillic symbols become "?".
UTF8ToString(SrcPath) works only when system encoding is russian.

Offline

#9 2021-12-29 10:43:32

George
Member
Registered: 2016-04-05
Posts: 142

Re: Calculate SHA256 hash of large file

So, the only option is to load file (using ut8 ready methods) and then calculate hash from buffer (because there is no option to pass stream)?

Last edited by George (2021-12-29 10:44:38)

Offline

#10 2021-12-29 11:08:35

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Calculate SHA256 hash of large file

I am afraid this is a FPC RTL limitation.
If your system is WinAnsi, then you can't use string=TFileName encoded outside the WinAnsi charset.

We don't have any UnicodeString file name functions in mORMot - we rely on the TFileName type... which is UnicodeString only on Delphi 2009+.

Offline

#11 2021-12-29 13:05:08

George
Member
Registered: 2016-04-05
Posts: 142

Re: Calculate SHA256 hash of large file

Code below works, but require to load entire file.

{$mode delphi}

var
  FilePathStr: RawUtf8;           // 'C:\Program Files (x86)\'#208#161#208#181#209#128#208#178#208#181#209#128'\test.txt'
  FileData:    RawByteString;
  Result:      RawUtf8;
begin
  FileData := ReadFileToString(FilePathStr);
  Result := MD5(FileData);
end;

Last edited by George (2021-12-29 14:09:04)

Offline

#12 2021-12-29 14:44:56

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Calculate SHA256 hash of large file

You may try after https://github.com/synopse/mORMot2/commit/3a3734ba

HashFile() should work as you expect with a RawUtf8 file name.

Offline

#13 2021-12-29 15:56:50

George
Member
Registered: 2016-04-05
Posts: 142

Re: Calculate SHA256 hash of large file

I still use 1.18..

Offline

#14 2021-12-29 16:15:45

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Calculate SHA256 hash of large file

Fix is sadly not available in 1.18.
And difficult to port.

Offline

#15 2021-12-29 16:27:05

George
Member
Registered: 2016-04-05
Posts: 142

Re: Calculate SHA256 hash of large file

Thanks, i will use suitable temp fix.
Would be cool if mORMot v2 will be fully Unicode aware (for FPC, including file paths and file operations).

Offline

#16 2021-12-29 17:45:55

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Calculate SHA256 hash of large file

With my commit above, in mORMot 2 you can use RawUtf8 as TFileName and it will convert any UTF-8 encoded name into UTF-16 proper buffer before calling the Windows API.
Just as with the plain FPC RTL.

So no need of UnicodeString overloaded versions of functions and methods.

Offline

#17 2021-12-29 21:59:40

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Calculate SHA256 hash of large file

There is an experimental utf8 support for file names in Windows 10 (checkbox on the language setting in control panel). If it is checked -  Windows should see file, so mORMot1 HashFile will work.

Last edited by mpv (2021-12-29 22:00:38)

Offline

Board footer

Powered by FluxBB