diff --git a/core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php b/core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php index f82b676..87d9d2a 100644 --- a/core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php +++ b/core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php @@ -47,6 +47,13 @@ class MTimeProtectedFastFileStorage extends FileStorage { protected $secret; /** + * The maximum number of characters of the hash to use for uniqueness. + * + * @var int + */ + protected $maxHashLength = 10; + + /** * Constructs this MTimeProtectedFastFileStorage object. * * @param array $configuration @@ -135,7 +142,8 @@ public function getFullPath($name, &$directory = NULL, &$directory_mtime = NULL) if (!isset($directory_mtime)) { $directory_mtime = file_exists($directory) ? filemtime($directory) : 0; } - return $directory . '/' . hash_hmac('sha256', $name, $this->secret . $directory_mtime) . '.php'; + $hashed_name = substr(hash_hmac('sha256', $name, $this->secret . $directory_mtime), 0, $this->maxHashLength) . '.php'; + return $directory . '/' . $hashed_name; } /** diff --git a/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php b/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php index d2cf918..4d07643 100644 --- a/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php +++ b/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php @@ -44,6 +44,13 @@ class TwigPhpStorageCache implements \Twig_CacheInterface { protected $templateCacheFilenamePrefix; /** + * The maximum number of characters of the hash to use for uniqueness. + * + * @var int + */ + protected $maxHashLength = 10; + + /** * Store cache backend and other information internally. * * @param \Drupal\Core\Cache\CacheBackendInterface $cache @@ -72,7 +79,7 @@ protected function storage() { * {@inheritdoc} */ public function generateKey($name, $className) { - $hash = hash('sha256', $className); + $hash = substr(hash('sha256', $className), 0, $this->maxHashLength); if (strpos($name, '{# inline_template_start #}') === 0) { // $name is an inline template, and can have characters that are not valid diff --git a/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php b/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php index 0bc52d0..8786d41 100644 --- a/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php +++ b/core/modules/system/src/Tests/Theme/TwigEnvironmentTest.php @@ -92,7 +92,7 @@ public function testInlineTemplate() { $cache = $environment->getCache(); $class = $environment->getTemplateClass($name); - $expected = $hash . '_inline-template' . '_' . hash('sha256', $class); + $expected = $hash . '_inline-template_' . substr(hash('sha256', $class), 0, 10); $this->assertEqual($expected, $cache->generateKey($name, $class)); } diff --git a/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageBase.php b/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageBase.php index 59944ff..9c02c15 100644 --- a/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageBase.php +++ b/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageBase.php @@ -82,7 +82,7 @@ public function testSecurity() { $expected_directory = $expected_root_directory . '/' . $name; } $directory_mtime = filemtime($expected_directory); - $expected_filename = $expected_directory . '/' . hash_hmac('sha256', $name, $this->secret . $directory_mtime) . '.php'; + $expected_filename = $expected_directory . '/' . substr(hash_hmac('sha256', $name, $this->secret . $directory_mtime), 0, 10) . '.php'; // Ensure the file exists and that it and the containing directory have // minimal permissions. fileperms() can return high bits unrelated to