YOUSEFUL / DELPHI / COMPONENTS
TAtlasCryptoProgressEvent
The method type of OnProgress on both components: bytes done, bytes in total, and a flag to cancel.
Description
TAtlasCryptoProgressEvent = procedure(Sender: TObject; Done, Total: Int64; var Cancel: Boolean) of object;
Sender is the component. Done and Total are byte counts; Total is measured once, when the operation starts. For TAtlasHashCalculator they count bytes hashed. For TAtlasFileProtector: When encrypting, Done and Total count plain bytes and Total is the size of the source. When decrypting they count the sealed bytes after the header (ciphertext plus the 16-byte tag of each chunk), so the numbers are larger than the plain size, but Done still reaches Total at the end. Set Cancel to True to stop; the component then raises EAtlasCryptoError. The event is called synchronously, on the thread that called the component. The anonymous-method equivalent used by the class functions is TAtlasCryptoProgress.
This type has no members of its own beyond those listed in its description.
Examples
Log progress from a console program
An event handler has to be a method, so a console program uses a small class. Hashing 600,000 bytes prints TAtlasHashCalculator: 43%, 87%, 100% and a final 100% for the last, empty read.
uses System.SysUtils, System.Classes, Atlas.Crypto, Atlas.CryptoComponents;
type
TProgressLog = class
procedure Report(Sender: TObject; Done, Total: Int64; var Cancel: Boolean);
end;
procedure TProgressLog.Report(Sender: TObject; Done, Total: Int64; var Cancel: Boolean);
begin
if Total > 0 then
WriteLn(Format('%s: %d%%', [Sender.ClassName, Done * 100 div Total]));
end;
procedure HashWithLog;
var
Calculator: TAtlasHashCalculator;
Log: TProgressLog;
Data: TBytesStream;
begin
Data := TBytesStream.Create(TAtlasRandom.Bytes(600000));
Log := TProgressLog.Create;
Calculator := TAtlasHashCalculator.Create(nil);
try
Calculator.OnProgress := Log.Report;
Calculator.HashStream(Data);
finally
Calculator.Free;
Log.Free;
Data.Free;
end;
end;
Related
TAtlasHashCalculator · TAtlasFileProtector · TAtlasCryptoProgress