class-bytetransformer.php
2 weeks ago
class-checksumtransformer.php
2 weeks ago
class-deflatetransformer.php
2 weeks ago
class-inflatetransformer.php
2 weeks ago
class-checksumtransformer.php
44 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\ByteStream\ByteTransformer; |
| 4 | |
| 5 | /** |
| 6 | * A reader that computes a checksum of the bytes read. |
| 7 | */ |
| 8 | class ChecksumTransformer implements ByteTransformer { |
| 9 | |
| 10 | private $hash_context; |
| 11 | private $checksum; |
| 12 | private $flush_hash; |
| 13 | private $binary_output; |
| 14 | |
| 15 | public function __construct( string $encoding = 'sha1', $options = array() ) { |
| 16 | $this->hash_context = hash_init( $encoding ); |
| 17 | $this->flush_hash = $options['flush_hash'] ?? false; |
| 18 | $this->binary_output = $options['binary_output'] ?? false; |
| 19 | } |
| 20 | |
| 21 | public function filter_bytes( string $bytes ) { |
| 22 | hash_update( $this->hash_context, $bytes ); |
| 23 | |
| 24 | return $bytes; |
| 25 | } |
| 26 | |
| 27 | public function flush(): string { |
| 28 | if ( $this->flush_hash ) { |
| 29 | return $this->get_hash(); |
| 30 | } |
| 31 | |
| 32 | return ''; |
| 33 | } |
| 34 | |
| 35 | public function get_hash(): string { |
| 36 | if ( $this->hash_context ) { |
| 37 | $this->checksum = hash_final( $this->hash_context, $this->binary_output ); |
| 38 | $this->hash_context = null; |
| 39 | } |
| 40 | |
| 41 | return $this->checksum; |
| 42 | } |
| 43 | } |
| 44 |