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 13, 2020
1 parent 4bf2e8f commit 8fc77f6
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 @@ -97,6 +97,11 @@ public function seek($offset, $whence = SEEK_SET): void
}
}

public function eof(): bool
{
return $this->stream->eof() && $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 8fc77f6

Please sign in to comment.