Browse Source

add progress callback event for buffered cipher stream processing.

Ugochukwu Mmaduekwe 7 years ago
parent
commit
6bbc2901cd

+ 30 - 1
CryptoLib/src/Crypto/ClpBufferedCipherBase.pas

@@ -55,15 +55,20 @@ type
   var
 
     FBufferSize: Int32;
+    FOnProgress: TBufferedCipherProgressEvent;
 
   const
     BUFFER_SIZE = Int32(64 * 1024); // 64Kb
 
     function GetBufferSize: Int32; inline;
     procedure SetBufferSize(value: Int32); inline;
+    function GetOnProgress: TBufferedCipherProgressEvent; inline;
+    procedure SetOnProgress(const value: TBufferedCipherProgressEvent); inline;
 
   strict protected
 
+    procedure DoProgress(AProcessed, ATotal: Int64); virtual;
+
     class property EmptyBuffer: TCryptoLibByteArray read GetEmptyBuffer;
 
   public
@@ -132,6 +137,8 @@ type
     /// encryption/decryption.
     /// </summary>
     property BufferSize: Int32 read GetBufferSize write SetBufferSize;
+    property OnProgress: TBufferedCipherProgressEvent read GetOnProgress
+      write SetOnProgress;
 
   end;
 
@@ -144,6 +151,25 @@ begin
   System.SetLength(FEmptyBuffer, 0);
 end;
 
+procedure TBufferedCipherBase.DoProgress(AProcessed, ATotal: Int64);
+begin
+  if System.Assigned(FOnProgress) then
+  begin
+    FOnProgress(AProcessed, ATotal);
+  end;
+end;
+
+function TBufferedCipherBase.GetOnProgress: TBufferedCipherProgressEvent;
+begin
+  result := FOnProgress;
+end;
+
+procedure TBufferedCipherBase.SetOnProgress(const value
+  : TBufferedCipherProgressEvent);
+begin
+  FOnProgress := value;
+end;
+
 constructor TBufferedCipherBase.Create;
 begin
   Inherited Create();
@@ -307,14 +333,16 @@ begin
   inputStream.Position := inPos;
   outputStream.Position := outPos;
 
+  DoProgress(0, length);
+
   while true do
   begin
-
     readed := inputStream.Read(data[0], LBufferSize);
 
     if ((total + Int64(readed)) >= length) then
     begin
       tempRes := ProcessBytes(data, 0, Int32(length - total));
+      DoProgress(total + readed, length);
       if (tempRes <> Nil) then
       begin
         outputStream.Write(tempRes[0], System.length(tempRes));
@@ -330,6 +358,7 @@ begin
       end;
       total := total + readed;
     end;
+    DoProgress(total, length);
   end;
 
   tempRes := DoFinal();

+ 9 - 0
CryptoLib/src/Interfaces/ClpIBufferedCipher.pas

@@ -26,6 +26,9 @@ uses
   ClpICipherParameters,
   ClpCryptoLibTypes;
 
+type
+  TBufferedCipherProgressEvent = procedure(AProcessed, ATotal: Int64);
+
 type
   /// <remarks>Block cipher engines are expected to conform to this interface.</remarks>
   IBufferedCipher = interface(IInterface)
@@ -42,6 +45,12 @@ type
     /// </summary>
     property BufferSize: Int32 read GetBufferSize write SetBufferSize;
 
+    function GetOnProgress: TBufferedCipherProgressEvent;
+    procedure SetOnProgress(const value: TBufferedCipherProgressEvent);
+
+    property OnProgress: TBufferedCipherProgressEvent read GetOnProgress
+      write SetOnProgress;
+
     /// <summary>Initialise the cipher.</summary>
     /// <param name="forEncryption">If true the cipher is initialised for encryption,
     /// if false for decryption.</param>