From c81120691a486f0b1ab5c57bcdf9cd2a3a3cad89 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Tue, 20 Dec 2022 09:32:11 +0100 Subject: [PATCH] Use xxh128 hash algorithm for PHP ^8.1 (#44) --- src/Psr6Store.php | 7 +++++-- tests/Psr6StoreTest.php | 34 +++++++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/Psr6Store.php b/src/Psr6Store.php index dbcf014..949f24d 100644 --- a/src/Psr6Store.php +++ b/src/Psr6Store.php @@ -57,6 +57,8 @@ class Psr6Store implements Psr6StoreInterface, ClearableInterface */ private array $locks = []; + private string $hashAlgorithm; + /** * When creating a Psr6Store you can configure a number options. * See the README for a list of all available options and their description. @@ -98,6 +100,7 @@ public function __construct(array $options = []) $this->options = $resolver->resolve($options); $this->cache = $this->options['cache']; $this->lockFactory = $this->options['lock_factory']; + $this->hashAlgorithm = version_compare(PHP_VERSION, '8.1.0', '>=') ? 'xxh128' : 'sha256'; } public function lookup(Request $request): ?Response @@ -323,7 +326,7 @@ public function getCacheKey(Request $request): string $uri = $request->getUri(); $uri = substr($uri, \strlen($request->getScheme().'://')); - return 'md'.hash('sha256', $uri); + return 'md'.hash($this->hashAlgorithm, $uri); } /** @@ -339,7 +342,7 @@ public function generateContentDigest(Response $response): ?string return null; } - return 'en'.hash('sha256', $response->getContent()); + return 'en'.hash($this->hashAlgorithm, $response->getContent()); } private function getVaryKey(array $vary, Request $request): string diff --git a/tests/Psr6StoreTest.php b/tests/Psr6StoreTest.php index 0dc3d31..994920b 100644 --- a/tests/Psr6StoreTest.php +++ b/tests/Psr6StoreTest.php @@ -333,7 +333,12 @@ public function testVaryResponseDropsNonVaryingOne(): void public function testRegularCacheKey(): void { $request = Request::create('https://foobar.com/'); - $expected = 'md'.hash('sha256', 'foobar.com/'); + $expected = 'md'.hash( + version_compare(PHP_VERSION, '8.1.0', '>=') + ? 'xxh128' + : 'sha256', + 'foobar.com/' + ); $this->assertSame($expected, $this->store->getCacheKey($request)); } @@ -375,7 +380,11 @@ public function testRegularLookup(): void $this->assertSame('hello world', $result->getContent()); $this->assertSame('whatever', $result->headers->get('Foobar')); - $this->assertSame('enb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9', $result->headers->get('X-Content-Digest')); + $this->assertSame( + version_compare(PHP_VERSION, '8.1.0', '>=') + ? 'endf8d09e93f874900a99b8775cc15b6c7' // xxh128 + : 'enb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9' // sha256 + , $result->headers->get('X-Content-Digest')); } public function testRegularLookupWithContentDigestsDisabled(): void @@ -851,10 +860,25 @@ public function testContentDigestExpiresCorrectly(array $responseHeaders, $expec ->expects($this->exactly(3)) ->method('getItem') ->withConsecutive( - ['enc3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2'], // content digest - ['md390aa862a7f27c16d72dd40967066969e7eb4b102c6215478a275766bf046665'], // meta + [ + // content digest + version_compare(PHP_VERSION, '8.1.0', '>=') + ? 'en3c9e102628997f44ac87b0b131c6992d' // xxh128 + : 'enc3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2' // sha256 + ], + [ + // meta + version_compare(PHP_VERSION, '8.1.0', '>=') + ? 'md0d10c3ce367c3309e789ed924fa6b183' // xxh128 + : 'md390aa862a7f27c16d72dd40967066969e7eb4b102c6215478a275766bf046665' // sha256 + ], [Psr6Store::COUNTER_KEY], // write counter - ['md390aa862a7f27c16d72dd40967066969e7eb4b102c6215478a275766bf046665'] // meta again + [ + // meta again + version_compare(PHP_VERSION, '8.1.0', '>=') + ? 'md0d10c3ce367c3309e789ed924fa6b183' // xxh128 + : 'md390aa862a7f27c16d72dd40967066969e7eb4b102c6215478a275766bf046665' // sha256 + ] ) ->willReturnOnConsecutiveCalls($contentDigestCacheItem, $cacheItem, $cacheItem, $cacheItem);