8000 [Cache] Hash using B64+MD5 in FilesystemAdapter by nicolas-grekas · Pull Request #18085 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Hash using B64+MD5 in FilesystemAdapter #18085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2016
Merged
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
[Cache] Hash using B64+MD5 in FilesystemAdapter
  • Loading branch information
nicolas-grekas committed Mar 10, 2016
commit e96bb10af4df8c44819c9edfa296d913aa2983d7
13 changes: 8 additions & 5 deletions src/Symfony/Component/Cache/Adapter/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct($directory, $defaultLifetime = null)
throw new InvalidArgumentException(sprintf('Cache directory is not writable (%s)', $directory));
}
// On Windows the whole path is limited to 258 chars
if ('\\' === DIRECTORY_SEPARATOR && strlen($dir) > 190) {
if ('\\' === DIRECTORY_SEPARATOR && strlen($dir) > 234) {
throw new InvalidArgumentException(sprintf('Cache directory too long (%s)', $directory));
}

Expand Down Expand Up @@ -62,10 +62,13 @@ protected function doFetch(array $ids)
@unlink($file);
}
} else {
$i = rawurldecode(rtrim(fgets($h)));
$value = stream_get_contents($h);
flock($h, LOCK_UN);
fclose($h);
$values[$id] = unserialize($value);
if ($i === $id) {
$values[$id] = unserialize($value);
}
}
}

Expand Down Expand Up @@ -125,7 +128,7 @@ protected function doSave(array $values, $lifetime)
if (!file_exists($dir)) {
@mkdir($dir, 0777, true);
}
$value = $expiresAt."\n".serialize($value);
$value = $expiresAt."\n".rawurlencode($id)."\n".serialize($value);
if (false !== @file_put_contents($file, $value, LOCK_EX)) {
@touch($file, $expiresAt);
} else {
Expand All @@ -138,8 +141,8 @@ protected function doSave(array $values, $lifetime)

private function getFile($id)
{
$hash = hash('sha256', $id);
$hash = str_replace('/', '-', base64_encode(md5($id, true)));

return $this->directory.$hash[0].DIRECTORY_SEPARATOR.$hash[1].DIRECTORY_SEPARATOR.$hash;
return $this->directory.$hash[0].DIRECTORY_SEPARATOR.$hash[1].DIRECTORY_SEPARATOR.substr($hash, 2, -2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why -2?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because these are padding = added by for b64

}
}
0