From 611681111b10092836b4f2d138af84465335384f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 30 Aug 2022 08:41:55 +0200 Subject: [PATCH] Use md5 for cache hashing instead of weak crc32 --- src/StaticAnalysis/CachingFileAnalyser.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/StaticAnalysis/CachingFileAnalyser.php b/src/StaticAnalysis/CachingFileAnalyser.php index f16a10fc9..40283704b 100644 --- a/src/StaticAnalysis/CachingFileAnalyser.php +++ b/src/StaticAnalysis/CachingFileAnalyser.php @@ -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; @@ -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 @@ -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)); return self::$cacheVersion; }