Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions HashLib/src/Base/HlpHMACNotBuildInAdapter.pas
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ THMACNotBuildInAdapter = class sealed(THash, IHMAC, IHMACNotBuildIn, ICrypto,
var
FHash: IHash;
FOpad, FIpad, FKey: THashLibByteArray;
FPadsValid: Boolean;

constructor Create(const AUnderlyingHash: IHash;
const AHMACKey: THashLibByteArray);
Expand Down Expand Up @@ -60,6 +61,7 @@ implementation
procedure THMACNotBuildInAdapter.Clear();
begin
TArrayUtils.ZeroFill(FKey);
FPadsValid := False;
end;

function THMACNotBuildInAdapter.Clone(): IHash;
Expand All @@ -69,6 +71,7 @@ function THMACNotBuildInAdapter.Clone(): IHash;
LHmacInstance := THMACNotBuildInAdapter.Create(FHash.Clone(), FKey);
LHmacInstance.FOpad := System.Copy(FOpad);
LHmacInstance.FIpad := System.Copy(FIpad);
LHmacInstance.FPadsValid := FPadsValid;
Result := LHmacInstance;
Result.BufferSize := BufferSize;
end;
Expand Down Expand Up @@ -104,6 +107,7 @@ procedure THMACNotBuildInAdapter.SetKey(const AValue: THashLibByteArray);
begin
FKey := System.Copy(AValue);
end;
FPadsValid := False;
end;

procedure THMACNotBuildInAdapter.UpdatePads;
Expand All @@ -112,13 +116,13 @@ procedure THMACNotBuildInAdapter.UpdatePads;
LIdx, LBlockSize: Int32;
begin
LBlockSize := FHash.BlockSize;
if (System.Length(Key) > LBlockSize) then
if (System.Length(FKey) > LBlockSize) then
begin
LKey := FHash.ComputeBytes(Key).GetBytes();
LKey := FHash.ComputeBytes(FKey).GetBytes();
end
else
begin
LKey := Key;
LKey := FKey;
end;

TArrayUtils.Fill(FIpad, 0, LBlockSize, Byte($36));
Expand All @@ -137,7 +141,11 @@ procedure THMACNotBuildInAdapter.UpdatePads;
procedure THMACNotBuildInAdapter.Initialize;
begin
FHash.Initialize();
UpdatePads();
if not FPadsValid then
begin
UpdatePads();
FPadsValid := True;
end;
FHash.TransformBytes(FIpad);
end;

Expand Down
56 changes: 26 additions & 30 deletions HashLib/src/Base/HlpHash.pas
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface
uses
Classes,
SysUtils,
Math,
HlpHashLibTypes,
HlpHashLibExceptions,
HlpConverters,
Expand All @@ -30,6 +31,9 @@ THash = class abstract(TInterfacedObject, IHash)
const
DefaultBufferSize = Int32(64 * 1024); // 64Kb

strict private
class function StagingSize(AMax: Int32; ATotal: Int64): Int32; static; inline;

strict protected

function GetBlockSize: Int32; virtual;
Expand Down Expand Up @@ -100,6 +104,11 @@ function THash.GetName: String;
Result := Self.ClassName;
end;

class function THash.StagingSize(AMax: Int32; ATotal: Int64): Int32;
begin
Result := Int32(Min(Int64(AMax), Max(ATotal, Int64(0))));
end;

function THash.GetBufferSize: Int32;
begin
Result := FBufferSize;
Expand Down Expand Up @@ -158,14 +167,7 @@ procedure THash.TransformUntyped(const AData; ALength: Int64);
begin
LPtrStart := @AData;

if BufferSize > ALength then // Sanity Check
begin
LBufferSize := BufferSize;
end
else
begin
LBufferSize := BufferSize;
end;
LBufferSize := StagingSize(BufferSize, ALength);

if LPtrStart <> nil then
begin
Expand Down Expand Up @@ -253,42 +255,36 @@ procedure THash.TransformStream(const AStream: TStream; ALength: Int64);
var
LData: THashLibByteArray;
LRead, LBufferSize: Int32;
LTotal: Int64;
LTotal, LPosition, LSize: Int64;
begin
{$IFDEF DEBUG}
System.Assert((ALength = -1) or (ALength > 0));
{$ENDIF DEBUG}
LTotal := 0;
if (AStream <> nil) then
if (AStream = nil) then
begin
if (ALength > -1) then
begin
if ((AStream.Position + ALength) > AStream.Size) then
begin
raise EIndexOutOfRangeHashLibException.CreateRes(
@SReadBeyondStreamEndError);
end;
end;
raise EArgumentNilHashLibException.CreateRes(@SAStreamNilError);
end;

LPosition := AStream.Position;
LSize := AStream.Size;

if (AStream.Position >= AStream.Size) then
if (ALength > -1) then
begin
if ((LPosition + ALength) > LSize) then
begin
Exit;
raise EIndexOutOfRangeHashLibException.CreateRes(
@SReadBeyondStreamEndError);
end;
end
else
begin
raise EArgumentNilHashLibException.CreateRes(@SAStreamNilError);
end;

if BufferSize > AStream.Size then // Sanity Check
begin
LBufferSize := BufferSize;
end
else
if (LPosition >= LSize) then
begin
LBufferSize := BufferSize;
Exit;
end;

LBufferSize := StagingSize(BufferSize, LSize - LPosition);

System.SetLength(LData, LBufferSize);

if (ALength = -1) then
Expand Down
Loading