Skip to content

Commit c1608f5

Browse files
GrahamCampbellsagikazarmark
authored andcommitted
Use sha1 to reduce the risk of collisions (#12)
* Use sha1 to reduce the risk of collisions * Only cache if the max age is positive * Updated tests * Support setting a hash_algo * CS * Reverted accidental change
1 parent bbe6fe2 commit c1608f5

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

Diff for: spec/CachePluginSpec.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i
4242
$response->getHeader('Cache-Control')->willReturn(array());
4343
$response->getHeader('Expires')->willReturn(array());
4444

45-
$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
45+
$pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item);
4646
$item->isHit()->willReturn(false);
4747
$item->set(['response' => $response, 'body' => $httpBody])->willReturn($item)->shouldBeCalled();
4848
$item->expiresAfter(60)->willReturn($item)->shouldBeCalled();
@@ -63,7 +63,7 @@ function it_doesnt_store_failed_responses(CacheItemPoolInterface $pool, CacheIte
6363
$response->getHeader('Cache-Control')->willReturn(array());
6464
$response->getHeader('Expires')->willReturn(array());
6565

66-
$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
66+
$pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item);
6767
$item->isHit()->willReturn(false);
6868

6969
$next = function (RequestInterface $request) use ($response) {
@@ -101,7 +101,7 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
101101
$response->getHeader('Age')->willReturn(array('15'));
102102
$response->getHeader('Expires')->willReturn(array());
103103

104-
$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
104+
$pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item);
105105
$item->isHit()->willReturn(false);
106106

107107
// 40-15 should be 25

Diff for: src/CachePlugin.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ final class CachePlugin implements Plugin
3838
* @param array $config {
3939
*
4040
* @var bool $respect_cache_headers Whether to look at the cache directives or ignore them
41-
* @var int $default_ttl If we do not respect cache headers or can't calculate a good ttl, use this value.
41+
* @var int $default_ttl If we do not respect cache headers or can't calculate a good ttl, use this value
42+
* @var string $hash_algo The hashing algorithm to use when generating cache keys.
4243
* }
4344
*/
4445
public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = [])
@@ -150,7 +151,7 @@ private function getCacheControlDirective(ResponseInterface $response, $name)
150151
*/
151152
private function createCacheKey(RequestInterface $request)
152153
{
153-
return md5($request->getMethod().' '.$request->getUri());
154+
return hash($this->config['hash_algo'], $request->getMethod().' '.$request->getUri());
154155
}
155156

156157
/**
@@ -196,9 +197,11 @@ private function configureOptions(OptionsResolver $resolver)
196197
$resolver->setDefaults([
197198
'default_ttl' => null,
198199
'respect_cache_headers' => true,
200+
'hash_algo' => 'sha1',
199201
]);
200202

201203
$resolver->setAllowedTypes('default_ttl', ['int', 'null']);
202204
$resolver->setAllowedTypes('respect_cache_headers', 'bool');
205+
$resolver->setAllowedValues('hash_algo', hash_algos());
203206
}
204207
}

0 commit comments

Comments
 (0)