-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJPL.Hash.WE_CRC64.pas
93 lines (68 loc) · 2.4 KB
/
JPL.Hash.WE_CRC64.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
unit JPL.Hash.WE_CRC64;
interface
uses
Classes, SysUtils,
JPL.Math, JPL.TimeLogger,
JPL.Hash.Common,
//WE: https://github.com/jackdp/www.wolfgang-ehrhardt.de
Hash, Mem_util, CRC64
;
const
HASH_BUFFER_SIZE_CRC32 = 1024*63; // 1024 * 100; // F000 = 61 440 bytes
function WeGetFileHash_Crc64(fName: string; var HashResult: THashResultRec; HashEnumProc: THashEnumProc = nil): Boolean;
function WeGetStreamHash_Crc64(AStream: TStream; var HashResult: THashResultRec; StartPos: Int64 = 0; HashEnumProc: THashEnumProc = nil): Boolean;
implementation
function WeGetStreamHash_Crc64(AStream: TStream; var HashResult: THashResultRec; StartPos: Int64 = 0; HashEnumProc: THashEnumProc = nil): Boolean;
var
Crc: TCRC64;
xRead, xPercent: integer;
xSize, xTotalRead: Int64;
BufNo: integer;
Buffer: array[0..HASH_BUFFER_SIZE_CRC32-1] of Byte;
Logger: TClassTimeLogger;
begin
Result := False;
Logger := TClassTimeLogger.Create;
try
Logger.StartLog;
ClearHashResultRec(HashResult);
HashResult.HashType := htCrc64;
CRC64Init(Crc);
xTotalRead := StartPos;
xSize := AStream.Size - StartPos;
HashResult.StreamSize := xSize;
BufNo := 0;
AStream.Position := StartPos;
while AStream.Position < AStream.Size do
begin
Inc(BufNo);
xRead := AStream.Read(Buffer, SizeOf(Buffer));
xTotalRead := xTotalRead + xRead;
xPercent := Round(PercentValue(xTotalRead, xSize));
if xRead > 0 then CRC64Update(Crc, @Buffer, xRead);
if Assigned(HashEnumProc) then if not HashEnumProc(xPercent, BufNo) then Exit;
end; // while
CRC64Final(Crc);
HashResult.StrValueUpper := UpperCase(string(HexStr(@CRC, DIGEST_LEN_CRC64))); // !!! musi byæ DIGEST_LEN_CRC64 zamiast HASH_LEN_CRC64
HashResult.StrValueLower := LowerCase(HashResult.StrValueUpper);
Logger.EndLog;
HashResult.ElapsedTimeMs := Logger.ElapsedTimeMs;
HashResult.SpeedMBperSec := GetSpeedValue_MB_per_sec(HashResult.StreamSize, HashResult.ElapsedTimeMs);
HashResult.ValidHash := True;
Result := True;
finally
Logger.Free;
end;
end;
function WeGetFileHash_Crc64(fName: string; var HashResult: THashResultRec; HashEnumProc: THashEnumProc = nil): Boolean;
var
fs: TFileStream;
begin
fs := TFileStream.Create(fName, fmOpenRead or fmShareDenyNone);
try
Result := WeGetStreamHash_Crc64(fs, HashResult, 0, HashEnumProc);
finally
fs.Free;
end;
end;
end.