CalculateChecksumFromStream.php 793 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. declare(strict_types=1);
  3. namespace League\Flysystem;
  4. use function hash_final;
  5. use function hash_init;
  6. use function hash_update_stream;
  7. trait CalculateChecksumFromStream
  8. {
  9. private function calculateChecksumFromStream(string $path, Config $config): string
  10. {
  11. try {
  12. $stream = $this->readStream($path);
  13. $algo = (string) $config->get('checksum_algo', 'md5');
  14. $context = hash_init($algo);
  15. hash_update_stream($context, $stream);
  16. return hash_final($context);
  17. } catch (FilesystemException $exception) {
  18. throw new UnableToProvideChecksum($exception->getMessage(), $path, $exception);
  19. }
  20. }
  21. /**
  22. * @return resource
  23. */
  24. abstract public function readStream(string $path);
  25. }