From 5acd6d8b93e92a6655ffe7a1e8397e474f7817a9 Mon Sep 17 00:00:00 2001 From: Alexander Smirnov Date: Sat, 15 May 2021 07:00:18 +0300 Subject: [PATCH] Fix warning and empty hash Sometimes stream is read even after reaching eof. In this case hash_final is called on obsolete hashResource producing warning and destroys hash result. Checking that hash is null prevents this behavior. User still can seek(0) and set hash to null in initializeHash function. Use case: AesDecryptingStream:113 calls read on empty stream (in our case its HashingStream) in order to prefetch data. Results: * Warning is produced. * Hash is spoiled. --- src/HashingStream.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/HashingStream.php b/src/HashingStream.php index 6974460..52e9adb 100644 --- a/src/HashingStream.php +++ b/src/HashingStream.php @@ -64,9 +64,11 @@ public function read($length): string hash_update($this->hashResource, $read); } if ($this->stream->eof()) { - $this->hash = hash_final($this->hashResource, true); - if ($this->onComplete) { - call_user_func($this->onComplete, $this->hash); + if (!$this->hash) { + $this->hash = hash_final($this->hashResource, true); + if ($this->onComplete) { + call_user_func($this->onComplete, $this->hash); + } } } @@ -93,4 +95,4 @@ private function initializeHash(): void $this->key ); } -} \ No newline at end of file +}