Improvements to TSHA256's Stream, StreamHexa and StreamBase64 routines

by 'lagprogramming'.

packages/fcl-hash/src/fpsha256.pp has the following routines:

    class procedure TSHA256.Stream(aStream: TStream; out aDigest: TSHA256Digest);
     
    const
      BUFFER_SIZE = 64*1024;
     
    var
      aLen : LongInt;
      Buffer: TBytes;
      SHA256: TSHA256;
     
    begin
      Buffer:=Nil;
      SHA256.Init;
      SetLength(Buffer,BUFFER_SIZE);
      repeat
         aLen:=aStream.Read(Buffer, BUFFER_SIZE);
         if aLen = 0 then
           Break;
         SHA256.Update(PByte(Buffer),aLen);
      until aLen=0;
      SHA256.Final;
      aDigest:=SHA256.Digest;
    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

    class function TSHA256.StreamHexa(aStream: TStream): AnsiString;
     
    begin
      Result:='';
      StreamHexa(aStream,Result);
    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

    class Function TSHA256.StreamBase64(aStream: TStream; isURL : Boolean): AnsiString;
     
    begin
      Result:='';
      StreamBase64(aStream,isURL,Result);
    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:='';

new.diff

Edited by Alexey Torgashin