Skip to content

Commit

Permalink
Make eof() consider padding when reading less than block size
Browse files Browse the repository at this point in the history
  • Loading branch information
valga committed Apr 12, 2020
1 parent 432b7b9 commit ef67d7b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/AesEncryptingStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public function seek($offset, $whence = SEEK_SET): void
}
}

public function eof(): bool
{
return $this->stream->eof() && !strlen($this->buffer);
}

private function encryptBlock(int $length): string
{
if ($this->stream->eof()) {
Expand Down
20 changes: 20 additions & 0 deletions tests/AesEncryptingStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,24 @@ public function seek(int $offset, int $whence = SEEK_SET): void {}

$this->assertRegExp("/EncryptionFailedException: Unable to encrypt/", $error);
}

/**
* @dataProvider cipherMethodProvider
*
* @param CipherMethod $cipherMethod
*/
public function testEofShouldConsiderPaddingWhenReadSizeIsLessThenBlockSize(CipherMethod $cipherMethod)
{
$stream = new AesEncryptingStream(
new RandomByteStream(100),
self::KEY,
$cipherMethod
);
$expectedSize = $stream->getSize();
$actualSize = 0;
while (!$stream->eof()) {
$actualSize += strlen($stream->read(15));
}
$this->assertSame($expectedSize, $actualSize);
}
}

0 comments on commit ef67d7b

Please sign in to comment.