YOUSEFUL / DELPHI / COMPONENTS
TAtlasCryptoProgress
Progress callback of the stream and file functions: an anonymous procedure with bytes done, bytes in total and a cancel flag.
Description
TAtlasCryptoProgress = reference to procedure(Done, Total: Int64; var Cancel: Boolean);
Passed to TAtlasStreamCrypt; called once per chunk, on the calling thread. Total is measured when the operation starts and is -1 for a stream that cannot report its size. 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. (The comment in the source says plain bytes in both directions; the decryption figures are as described here.) Set Cancel to True to stop: the function raises EAtlasCryptoError. The components use the method type TAtlasCryptoProgressEvent instead.
This type has no members of its own beyond those listed in its description.
Examples
Show progress and stop half-way
Encrypts 200,000 bytes in 64 KB chunks and cancels once 100,000 bytes are done. Prints 65536 of 200000, 131072 of 200000 and Encryption was cancelled.
uses System.SysUtils, System.Classes, Atlas.Crypto, Atlas.CryptoBox;
procedure CancelHalfWay;
var
Source, Dest: TBytesStream;
Progress: TAtlasCryptoProgress;
begin
Progress :=
procedure(Done, Total: Int64; var Cancel: Boolean)
begin
WriteLn(Done, ' of ', Total);
Cancel := Done >= 100000;
end;
Source := TBytesStream.Create(TAtlasRandom.Bytes(200000));
Dest := TBytesStream.Create;
try
try
TAtlasStreamCrypt.Encrypt(Source, Dest, TAtlasSecretBox.NewKey, Progress);
except
on E: EAtlasCryptoError do WriteLn(E.Message);
end;
finally
Dest.Free;
Source.Free;
end;
end;
Related
TAtlasStreamCrypt · TAtlasCryptoProgressEvent