-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJPL.Hash.WE_SHA2_384.pas
100 lines (74 loc) · 2.55 KB
/
JPL.Hash.WE_SHA2_384.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
94
95
96
97
98
99
100
unit JPL.Hash.WE_SHA2_384;
interface
uses
Classes, SysUtils,
JPL.Math, JPL.TimeLogger,
JPL.Hash.Common,
//WE: https://github.com/jackdp/www.wolfgang-ehrhardt.de
Hash, Mem_util, sha384
;
const
HASH_BUFFER_SIZE_SHA384 = 1024*63; // 1024 * 100; // F000 = 61 440 bytes
function WeGetFileHash_SHA2_384(fName: string; var HashResult: THashResultRec; HashEnumProc: THashEnumProc = nil): Boolean;
function WeGetStreamHash_SHA2_384(AStream: TStream; var HashResult: THashResultRec; StartPos: Int64 = 0; HashEnumProc: THashEnumProc = nil): Boolean;
implementation
function HexString(const x: array of byte): AnsiString; {-HEX string from memory}
begin
Result := HexStr(@x, sizeof(x));
end;
function WeGetStreamHash_SHA2_384(AStream: TStream; var HashResult: THashResultRec; StartPos: Int64 = 0; HashEnumProc: THashEnumProc = nil): Boolean;
var
//Crc: integer;
Context: THashContext;
Digest: TSHA384Digest;
xRead, xPercent: integer;
xSize, xTotalRead: Int64;
BufNo: integer;
Buffer: array[0..HASH_BUFFER_SIZE_SHA384-1] of Byte;
Logger: TClassTimeLogger;
begin
Result := False;
Logger := TClassTimeLogger.Create;
try
Logger.StartLog;
ClearHashResultRec(HashResult);
HashResult.HashType := htSha2_384;
SHA384Init(Context);
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 SHA384Update(Context, @Buffer, xRead);
if Assigned(HashEnumProc) then if not HashEnumProc(xPercent, BufNo) then Exit;
end; // while
SHA384Final(Context, Digest);
HashResult.StrValueUpper := UpperCase(string(HexString(Digest)));
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_SHA2_384(fName: string; var HashResult: THashResultRec; HashEnumProc: THashEnumProc = nil): Boolean;
var
fs: TFileStream;
begin
fs := TFileStream.Create(fName, fmOpenRead or fmShareDenyNone);
try
Result := WeGetStreamHash_SHA2_384(fs, HashResult, 0, HashEnumProc);
finally
fs.Free;
end;
end;
end.