Skip to content
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

Use md5 for cache hashing instead of weak crc32 #931

Merged
merged 1 commit into from
Aug 30, 2022
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
13 changes: 8 additions & 5 deletions src/StaticAnalysis/CachingFileAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
*/
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;

use function crc32;
use function file_get_contents;
use function file_put_contents;
use function implode;
use function is_file;
use function md5;
use function serialize;
use SebastianBergmann\CodeCoverage\Util\Filesystem;
use SebastianBergmann\FileIterator\Facade as FileIteratorFacade;
Expand Down Expand Up @@ -159,7 +160,8 @@ private function write(string $filename, $data): void

private function cacheFile(string $filename): string
{
return $this->directory . DIRECTORY_SEPARATOR . hash('sha256', $filename . crc32(file_get_contents($filename)) . self::cacheVersion());
return $this->directory . DIRECTORY_SEPARATOR
. md5($filename . "\0" . file_get_contents($filename) . "\0" . self::cacheVersion());
}

private static function cacheVersion(): string
Expand All @@ -168,13 +170,14 @@ private static function cacheVersion(): string
return self::$cacheVersion;
}

$buffer = '';
$buffer = [];

foreach ((new FileIteratorFacade)->getFilesAsArray(__DIR__, '.php') as $file) {
$buffer .= file_get_contents($file);
$buffer[] = $file;
$buffer[] = file_get_contents($file);
}

self::$cacheVersion = (string) crc32($buffer);
self::$cacheVersion = md5(implode("\0", $buffer));
mvorisek marked this conversation as resolved.
Show resolved Hide resolved

return self::$cacheVersion;
}
Expand Down