Last active
November 13, 2026 16:14
-
-
Save donatj/e4e7ff7c8f4fba3ff568afed195877ad to your computer and use it in GitHub Desktop.
Test for Hashing GitHub Zip Contents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $zipFiles = [ | |
| 'phpstan.zip' => 'https://github.com/phpstan/phpstan/archive/refs/tags/2.3.0.zip', | |
| 'google-cloud.zip' => 'https://github.com/googleapis/google-cloud-php/archive/refs/tags/v0.352.0.zip', | |
| 'magento.zip' => 'https://github.com/magento/magento2/archive/refs/tags/2.4.9.zip', | |
| 'wordpress.zip' => 'https://github.com/WordPress/WordPress/archive/refs/tags/7.1.4.zip', | |
| 'google-api-client.zip' => 'https://github.com/googleapis/google-api-php-client-services/archive/refs/tags/v0.466.0.zip', | |
| 'microsoft-graph.zip' => 'https://github.com/microsoftgraph/msgraph-sdk-php/archive/refs/tags/v3.10.0.zip', | |
| 'mpdf.zip' => 'https://github.com/mpdf/mpdf/archive/refs/tags/v8.3.1.zip', | |
| 'symfony.zip' => 'https://github.com/symfony/symfony/archive/refs/tags/v8.1.11.zip', | |
| 'aws-sdk.zip' => 'https://github.com/aws/aws-sdk-php/archive/refs/tags/3.403.4.zip', | |
| 'drupal.zip' => 'https://github.com/drupal/core/archive/refs/tags/12.0.0-beta2.zip', | |
| 'laravel.zip' => 'https://github.com/laravel/framework/archive/refs/tags/v13.42.0.zip', | |
| 'phpoffice-phpword.zip' => 'https://github.com/PHPOffice/PHPWord/archive/refs/tags/1.4.0.zip', | |
| 'psr-http-message.zip' => 'https://github.com/php-fig/http-message/archive/refs/tags/2.0.zip', | |
| 'doctrine-lexer.zip' => 'https://github.com/doctrine/lexer/archive/refs/tags/3.0.3.zip', | |
| 'psr-log.zip' => 'https://github.com/php-fig/log/archive/refs/tags/3.0.2.zip', | |
| 'psr-http-factory.zip' => 'https://github.com/php-fig/http-factory/archive/refs/tags/1.1.0.zip', | |
| 'psr-container.zip' => 'https://github.com/php-fig/container/archive/refs/tags/2.0.2.zip', | |
| ]; | |
| foreach( $zipFiles as $zipFile => $url ) { | |
| if( file_exists($zipFile) ) { | |
| continue; | |
| } | |
| echo "Downloading $url...\n"; | |
| $success = @file_put_contents($zipFile, fopen($url, 'r')); | |
| if( $success === false ) { | |
| throw new RuntimeException("Failed to download $zipFile from $url"); | |
| } | |
| } | |
| $nameWidth = max(array_map('strlen', array_keys($zipFiles))); | |
| printf("%-{$nameWidth}s %8s %10s %17s %8s %s\n", 'Archive', 'Time (s)', 'Zip (MB)', 'Uncompressed (MB)', '', 'HASH'); | |
| foreach( $zipFiles as $zipFile => $url ) { | |
| $start = microtime(true); | |
| [ 'hash' => $hash, 'size' => $size ] = ziphash($zipFile, 'sha1'); | |
| $time = microtime(true) - $start; | |
| printf( | |
| "%-{$nameWidth}s %8.3F %10.2F %17.2F %8s %s\n", | |
| $zipFile, | |
| $time, | |
| filesize($zipFile) / 1000000, | |
| $size / 1000000, | |
| "SHA-1", | |
| $hash, | |
| ); | |
| } | |
| foreach( $zipFiles as $zipFile => $url ) { | |
| $start = microtime(true); | |
| [ 'hash' => $hash, 'size' => $size ] = ziphash($zipFile, 'sha256'); | |
| $time = microtime(true) - $start; | |
| printf( | |
| "%-{$nameWidth}s %8.3F %10.2F %17.2F %8s %s\n", | |
| $zipFile, | |
| $time, | |
| filesize($zipFile) / 1000000, | |
| $size / 1000000, | |
| "SHA-256", | |
| $hash, | |
| ); | |
| } | |
| /** | |
| * @return array{hash: string, size: int} SHA-1 of the file contents, and total uncompressed bytes hashed | |
| */ | |
| function ziphash( string $path, string $algo ): array { | |
| $zip = new ZipArchive(); | |
| $opened = $zip->open($path, ZipArchive::RDONLY); | |
| if( $opened !== true ) { | |
| throw new RuntimeException("Cannot open $path (ZipArchive error code $opened)"); | |
| } | |
| $files = []; | |
| for( $i = 0; $i < $zip->numFiles; $i++ ) { | |
| $name = $zip->getNameIndex($i); | |
| if( !str_ends_with($name, '/') ) { | |
| $files[] = [ 'index' => $i, 'name' => $name ]; | |
| } | |
| } | |
| // Breadth-first, alphabetical within each level. | |
| usort($files, static fn( $a, $b ) => (substr_count($a['name'], '/') <=> substr_count($b['name'], '/')) | |
| ?: strcmp($a['name'], $b['name']), | |
| ); | |
| $hash = hash_init($algo); | |
| $size = 0; | |
| foreach( $files as $file ) { | |
| $stream = $zip->getStreamIndex($file['index']); | |
| if( $stream === false ) { | |
| throw new RuntimeException("Cannot read {$file['name']}"); | |
| } | |
| $size += hash_update_stream($hash, $stream); | |
| fclose($stream); | |
| } | |
| $result = hash_final($hash); | |
| $zip->close(); | |
| return [ 'hash' => $result, 'size' => $size ]; | |
| } |
Author
Author
Timings from my personal M1 Max Mac Studio
Archive Time (s) Zip (MB) Uncompressed (MB) HASH
phpstan.zip 0.951 108.57 268.13 SHA-1 0b79723b004d3c198ee39d07ebf719901c6c8e93
google-cloud.zip 0.887 53.02 236.98 SHA-1 f19b11ec4839c72e835dfbbab98bb1591f9fc771
magento.zip 0.998 81.67 206.46 SHA-1 05ca3b4b0af2eda810451cfafd1bbf07c926866a
wordpress.zip 0.444 63.90 142.23 SHA-1 872a3dc42cf691186ee7978e668b9dcbd35f28dd
google-api-client.zip 0.698 51.35 139.25 SHA-1 51a6ce954e028c54aad146ac010429e0ec00f0dd
microsoft-graph.zip 0.705 49.98 122.37 SHA-1 ddded1b497971e6a6f9927e80ad53d1362291d6c
mpdf.zip 0.377 46.63 96.41 SHA-1 2931992b80b38dd8fc940b8c9e459242c290a3f1
symfony.zip 0.258 17.38 80.98 SHA-1 76cb7e1155ba8355c9453c8993f40dfac3b59882
aws-sdk.zip 0.169 8.02 63.27 SHA-1 3e09dcefba46b3d88f8da261c94eb9e178731360
drupal.zip 0.221 18.81 52.56 SHA-1 8d1e83fc2e134eb4ce2aee949bde1888a12c6a99
laravel.zip 0.039 2.48 8.68 SHA-1 d4f616616bd147d334d80d01b00f7f60ee36c22b
phpoffice-phpword.zip 0.007 0.53 1.80 SHA-1 943fd40a14b087eb7b15d4ca6e7661e86d6f11d5
psr-http-message.zip 0.000 0.02 0.07 SHA-1 0f007231003b79bf930c034fcf48279707887ec9
doctrine-lexer.zip 0.000 0.01 0.01 SHA-1 c79e1d2bcc63c2ecd54722d8fbf1d48141523cd5
psr-log.zip 0.000 0.01 0.01 SHA-1 a22e5b6931cec559d3d62915dbc058776c5f8011
psr-http-factory.zip 0.000 0.01 0.01 SHA-1 946705b1987363fcac83a1d309fff08bb80bac8a
psr-container.zip 0.000 0.00 0.00 SHA-1 6c8a1890de24016bf54be0dc4dd899ad157580f6
phpstan.zip 1.775 108.57 268.13 SHA-256 506131d1af7e7811c2baabd8ede5759f547e8ed4ac2d7055c01deef896928439
google-cloud.zip 1.664 53.02 236.98 SHA-256 5f54279c92dcbb75cdedb8b13e3b0536fb56b0947df14040cdb03b2e20b64890
magento.zip 1.593 81.67 206.46 SHA-256 2af73a95f692fd2d1e46b96cc868566ee204c5969fd40e4fb9a2f795b11cc07d
wordpress.zip 0.874 63.90 142.23 SHA-256 0cd6828bf7dd5d18b76f4542e275af401c62a9949665f477e008b5e43b99f516
google-api-client.zip 1.148 51.35 139.25 SHA-256 bcd8fb0e5b73acddc5048ab892e5f79ecb725aa2ba854f8611f77a55640cec36
microsoft-graph.zip 1.076 49.98 122.37 SHA-256 92a0f031553fe3ff91a28fc38d8b085d212d98db59b4fcb263bdad9dd20b3ac6
mpdf.zip 0.657 46.63 96.41 SHA-256 24b0d706a54f6c08f4bdce103c66be228a4251720c5708083b349073affced94
symfony.zip 0.508 17.38 80.98 SHA-256 5f83e37c70ef248e2c1ba9bd49cdff74b80f90f402ba1c2eaf9c533616778cf6
aws-sdk.zip 0.360 8.02 63.27 SHA-256 e2df0ea069e9fc1589146df2f9b3eb95f6ccdf0aa578eff531d86b403caeea17
drupal.zip 0.380 18.81 52.56 SHA-256 a437ef9f9daa5f3b5f492ad056e170308d94a972c0f10a88ce537441a5cf31f1
laravel.zip 0.067 2.48 8.68 SHA-256 1d15504040e4d0ab0f9c27669dfd91b0964c31b204b8c62579054492241792a7
phpoffice-phpword.zip 0.013 0.53 1.80 SHA-256 75b527bd633320949247efc7b456537d3ec2cd70455d076b9e71100a42bb763a
psr-http-message.zip 0.001 0.02 0.07 SHA-256 f15b20e1e2a9ae8243882d8d300c6b221d2b13ff01d2c79dba94efc13a9a5023
doctrine-lexer.zip 0.000 0.01 0.01 SHA-256 95750a75f347bfda1022c5375422c9be994ec60cba0cf343717d571c58d0c059
psr-log.zip 0.000 0.01 0.01 SHA-256 20a95eccd419a7852add28500db12bb24373f06b8371db06b438fcbaaf11da95
psr-http-factory.zip 0.000 0.01 0.01 SHA-256 02d3cfcfc6510f250bf61f0d76bbd6eefd54957b22541c29418b3a020c8a3906
psr-container.zip 0.000 0.00 0.00 SHA-256 5f0df121c35b41660932d2f7ded99e804fe9b6675b7edb5d96aa71913223eebf
Author
Timings on my dinky little $5 a month Digital Ocean Droplet
Archive Time (s) Zip (MB) Uncompressed (MB) HASH
phpstan.zip 2.578 108.57 268.13 SHA-1 0b79723b004d3c198ee39d07ebf719901c6c8e93
google-cloud.zip 2.532 53.02 236.98 SHA-1 f19b11ec4839c72e835dfbbab98bb1591f9fc771
magento.zip 2.596 81.67 206.46 SHA-1 05ca3b4b0af2eda810451cfafd1bbf07c926866a
wordpress.zip 1.155 63.90 142.23 SHA-1 872a3dc42cf691186ee7978e668b9dcbd35f28dd
google-api-client.zip 2.055 51.35 139.25 SHA-1 51a6ce954e028c54aad146ac010429e0ec00f0dd
microsoft-graph.zip 2.099 49.98 122.37 SHA-1 ddded1b497971e6a6f9927e80ad53d1362291d6c
mpdf.zip 0.931 46.63 96.41 SHA-1 2931992b80b38dd8fc940b8c9e459242c290a3f1
symfony.zip 0.660 17.38 80.98 SHA-1 76cb7e1155ba8355c9453c8993f40dfac3b59882
aws-sdk.zip 0.433 8.02 63.27 SHA-1 3e09dcefba46b3d88f8da261c94eb9e178731360
drupal.zip 0.538 18.81 52.56 SHA-1 8d1e83fc2e134eb4ce2aee949bde1888a12c6a99
laravel.zip 0.087 2.48 8.68 SHA-1 d4f616616bd147d334d80d01b00f7f60ee36c22b
phpoffice-phpword.zip 0.018 0.53 1.80 SHA-1 943fd40a14b087eb7b15d4ca6e7661e86d6f11d5
psr-http-message.zip 0.001 0.02 0.07 SHA-1 0f007231003b79bf930c034fcf48279707887ec9
doctrine-lexer.zip 0.000 0.01 0.01 SHA-1 c79e1d2bcc63c2ecd54722d8fbf1d48141523cd5
psr-log.zip 0.000 0.01 0.01 SHA-1 a22e5b6931cec559d3d62915dbc058776c5f8011
psr-http-factory.zip 0.000 0.01 0.01 SHA-1 946705b1987363fcac83a1d309fff08bb80bac8a
psr-container.zip 0.000 0.00 0.00 SHA-1 6c8a1890de24016bf54be0dc4dd899ad157580f6
phpstan.zip 4.093 108.57 268.13 SHA-256 506131d1af7e7811c2baabd8ede5759f547e8ed4ac2d7055c01deef896928439
google-cloud.zip 3.950 53.02 236.98 SHA-256 5f54279c92dcbb75cdedb8b13e3b0536fb56b0947df14040cdb03b2e20b64890
magento.zip 3.452 81.67 206.46 SHA-256 2af73a95f692fd2d1e46b96cc868566ee204c5969fd40e4fb9a2f795b11cc07d
wordpress.zip 1.931 63.90 142.23 SHA-256 0cd6828bf7dd5d18b76f4542e275af401c62a9949665f477e008b5e43b99f516
google-api-client.zip 2.821 51.35 139.25 SHA-256 bcd8fb0e5b73acddc5048ab892e5f79ecb725aa2ba854f8611f77a55640cec36
microsoft-graph.zip 2.773 49.98 122.37 SHA-256 92a0f031553fe3ff91a28fc38d8b085d212d98db59b4fcb263bdad9dd20b3ac6
mpdf.zip 1.443 46.63 96.41 SHA-256 24b0d706a54f6c08f4bdce103c66be228a4251720c5708083b349073affced94
symfony.zip 1.131 17.38 80.98 SHA-256 5f83e37c70ef248e2c1ba9bd49cdff74b80f90f402ba1c2eaf9c533616778cf6
aws-sdk.zip 0.744 8.02 63.27 SHA-256 e2df0ea069e9fc1589146df2f9b3eb95f6ccdf0aa578eff531d86b403caeea17
drupal.zip 0.835 18.81 52.56 SHA-256 a437ef9f9daa5f3b5f492ad056e170308d94a972c0f10a88ce537441a5cf31f1
laravel.zip 0.131 2.48 8.68 SHA-256 1d15504040e4d0ab0f9c27669dfd91b0964c31b204b8c62579054492241792a7
phpoffice-phpword.zip 0.025 0.53 1.80 SHA-256 75b527bd633320949247efc7b456537d3ec2cd70455d076b9e71100a42bb763a
psr-http-message.zip 0.001 0.02 0.07 SHA-256 f15b20e1e2a9ae8243882d8d300c6b221d2b13ff01d2c79dba94efc13a9a5023
doctrine-lexer.zip 0.000 0.01 0.01 SHA-256 95750a75f347bfda1022c5375422c9be994ec60cba0cf343717d571c58d0c059
psr-log.zip 0.000 0.01 0.01 SHA-256 20a95eccd419a7852add28500db12bb24373f06b8371db06b438fcbaaf11da95
psr-http-factory.zip 0.000 0.01 0.01 SHA-256 02d3cfcfc6510f250bf61f0d76bbd6eefd54957b22541c29418b3a020c8a3906
psr-container.zip 0.000 0.00 0.00 SHA-256 5f0df121c35b41660932d2f7ded99e804fe9b6675b7edb5d96aa71913223eebf
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Timings from my M1 Macbook bloated with Corporate Antivirus