Recent

Author Topic: [CLOSED] Improvements to TSHA256's Stream, StreamHexa and StreamBase64 routines  (Read 816 times)

lagprogramming

  • Sr. Member
  • ****
  • Posts: 407
packages/fcl-hash/src/fpsha256.pp has the following routines:
1/3
Code: Pascal  [Select][+][-]
  1. class procedure TSHA256.Stream(aStream: TStream; out aDigest: TSHA256Digest);
  2.  
  3. const
  4.   BUFFER_SIZE = 64*1024;
  5.  
  6. var
  7.   aLen : LongInt;
  8.   Buffer: TBytes;
  9.   SHA256: TSHA256;
  10.  
  11. begin
  12.   Buffer:=Nil;
  13.   SHA256.Init;
  14.   SetLength(Buffer,BUFFER_SIZE);
  15.   repeat
  16.      aLen:=aStream.Read(Buffer, BUFFER_SIZE);
  17.      if aLen = 0 then
  18.        Break;
  19.      SHA256.Update(PByte(Buffer),aLen);
  20.   until aLen=0;
  21.   SHA256.Final;
  22.   aDigest:=SHA256.Digest;
  23. end;

The condition of the repeat loop regarding the value of aLen is always fulfilled because if the assigned value is 0 then we have an immediate break and later the declared procedure Update(PBuf: PByte; Size: UInt32); overload; receives the aLen parameter by value.
The proposed patch replaces until aLen=0; with until false;

2/3
Code: Pascal  [Select][+][-]
  1. class function TSHA256.StreamHexa(aStream: TStream): AnsiString;
  2.  
  3. begin
  4.   Result:='';
  5.   StreamHexa(aStream,Result);
  6. end;
The file contains the following declaration: class procedure StreamHexa(aStream: TStream; out Result: AnsiString); static; overload;
According to the above declaration, the proposed patch removes the line Result:='';

3/3
Code: Pascal  [Select][+][-]
  1. class Function TSHA256.StreamBase64(aStream: TStream; isURL : Boolean): AnsiString;
  2.  
  3. begin
  4.   Result:='';
  5.   StreamBase64(aStream,isURL,Result);
  6. end;
The file contains the following declaration: class procedure StreamBase64(aStream: TStream; isURL : Boolean; out Result: AnsiString); static; overload;
According to the above declaration, the proposed patch removes the line Result:='';
Here is the patch:
Code: Pascal  [Select][+][-]
  1. diff --git a/packages/fcl-hash/src/fpsha256.pp b/packages/fcl-hash/src/fpsha256.pp
  2. index 7cb3c8b2d6..80cab79351 100644
  3. --- a/packages/fcl-hash/src/fpsha256.pp
  4. +++ b/packages/fcl-hash/src/fpsha256.pp
  5. @@ -361,7 +361,7 @@ class procedure TSHA256.Stream(aStream: TStream; out aDigest: TSHA256Digest);
  6.       if aLen = 0 then
  7.         Break;
  8.       SHA256.Update(PByte(Buffer),aLen);
  9. -  until aLen=0;
  10. +  until false;
  11.    SHA256.Final;
  12.    aDigest:=SHA256.Digest;
  13.  end;
  14. @@ -388,7 +388,6 @@ class procedure TSHA256.StreamHexa(aStream: TStream; out Result: AnsiString);
  15.  class function TSHA256.StreamHexa(aStream: TStream): AnsiString;
  16.  
  17.  begin
  18. -  Result:='';
  19.    StreamHexa(aStream,Result);
  20.  end;
  21.  
  22. @@ -408,7 +407,6 @@ class procedure TSHA256.StreamBase64(aStream: TStream; isURL : Boolean; out Resu
  23.  class Function TSHA256.StreamBase64(aStream: TStream; isURL : Boolean): AnsiString;
  24.  
  25.  begin
  26. -  Result:='';
  27.    StreamBase64(aStream,isURL,Result);
  28.  end;
  29.  
« Last Edit: August 11, 2023, 03:10:28 pm by lagprogramming »


 

TinyPortal © 2005-2018