You are not logged in.
Pages: 1
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
I am sorry but I do not understand.
Can you please provide some sample code?
Offline
I've just added the HashFile() function which does what you expect.
See https://synopse.info/fossil/info/906b6f74c4
Offline
Thank you much!
Offline
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
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...
Offline
Same with Utf8ToWinAnsi, cyrillic symbols become "?".
UTF8ToString(SrcPath) works only when system encoding is russian.
Offline
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
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
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
You may try after https://github.com/synopse/mORMot2/commit/3a3734ba
HashFile() should work as you expect with a RawUtf8 file name.
Offline
I still use 1.18..
Offline
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
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
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
Pages: 1