Skip to content
This repository was archived by the owner on Mar 20, 2026. It is now read-only.
Open
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
7 changes: 3 additions & 4 deletions src/AesDecryptingStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ public function read($length): string
);
}

$data = substr($this->plainBuffer, 0, $length);
$this->plainBuffer = substr($this->plainBuffer, $length);

return $data ? $data : '';
$data = (string)substr($this->plainBuffer, 0, $length);
$this->plainBuffer = (string)substr($this->plainBuffer, $length);
return $data;
}

public function seek($offset, $whence = SEEK_SET): void
Expand Down
7 changes: 3 additions & 4 deletions src/AesEncryptingStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ public function read($length): string
);
}

$data = substr($this->buffer, 0, $length);
$this->buffer = substr($this->buffer, $length);

return $data ? $data : '';
$data = (string)substr($this->buffer, 0, $length);
$this->buffer = (string)substr($this->buffer, $length);
return $data;
}

public function seek($offset, $whence = SEEK_SET): void
Expand Down
20 changes: 20 additions & 0 deletions tests/AesDecryptingStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,24 @@ public function testEmitsErrorWhenDecryptionFails()

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

/**
* @dataProvider cipherMethodProvider
*
* @param CipherMethod $iv
*/
public function testSupportsReadLength1(
CipherMethod $iv
) {
$plain = str_repeat("0", 100);
$cipherStream = new AesEncryptingStream(Psr7\stream_for($plain), self::KEY, clone $iv);
$stream = new AesDecryptingStream($cipherStream, self::KEY, clone $iv);

$result = "";
for ($i = 0; $i < 100; $i++) {
$result .= $stream->read(1);
}

$this->assertEquals($plain, $result);
}
}
17 changes: 17 additions & 0 deletions tests/AesEncryptingStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,21 @@ public function seek(int $offset, int $whence = SEEK_SET): void {}

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

public function testSupportsReadLength1()
{
$key = "keyy";
$plain = str_repeat("a", 49);
$iv = hex2bin("5dfe91624ede1efc6bc1c90e1932c398");

$cipherMethod = new Cbc($iv, $keySize=128);
$e = new AesEncryptingStream(Psr7\stream_for($plain), $key, $cipherMethod);

$result = "";
for ($i = 0; $i < 100; $i++) {
$result .= $e->read(1);
}

$this->assertEquals(64, strlen($result));
}
}