Skip to content

Commit

Permalink
Use md5 for cache hashing instead of weak crc32
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Aug 30, 2022
1 parent 2e94d03 commit 6116811
Showing 1 changed file with 8 additions and 5 deletions.
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));

return self::$cacheVersion;
}
Expand Down

0 comments on commit 6116811

Please sign in to comment.