| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\S3; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\AWS\CRT\CRT; |
| 6 |
use Dudlewebs\WPMCS\s3\Aws\Exception\CommonRuntimeException; |
| 7 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7; |
| 8 |
use InvalidArgumentException; |
| 9 |
trait CalculatesChecksumTrait |
| 10 |
{ |
| 11 |
/** |
| 12 |
* @param string $requestedAlgorithm the algorithm to encode with |
| 13 |
* @param string $value the value to be encoded |
| 14 |
* @return string |
| 15 |
*/ |
| 16 |
public static function getEncodedValue($requestedAlgorithm, $value) |
| 17 |
{ |
| 18 |
$requestedAlgorithm = \strtolower($requestedAlgorithm); |
| 19 |
$useCrt = \extension_loaded('awscrt'); |
| 20 |
if ($useCrt) { |
| 21 |
switch ($requestedAlgorithm) { |
| 22 |
case 'crc32c': |
| 23 |
return CRT::crc32c($value); |
| 24 |
case 'crc32': |
| 25 |
return CRT::crc32($value); |
| 26 |
case 'sha256': |
| 27 |
case 'sha1': |
| 28 |
return \base64_encode(Psr7\Utils::hash($value, $requestedAlgorithm, \true)); |
| 29 |
default: |
| 30 |
break; |
| 31 |
throw new InvalidArgumentException("Invalid checksum requested: {$requestedAlgorithm}." . " Valid algorithms are CRC32C, CRC32, SHA256, and SHA1."); |
| 32 |
} |
| 33 |
} else { |
| 34 |
if ($requestedAlgorithm == 'crc32c') { |
| 35 |
throw new CommonRuntimeException("crc32c is not supported for checksums " . "without use of the common runtime for php. Please enable the CRT or choose " . "a different algorithm."); |
| 36 |
} |
| 37 |
if ($requestedAlgorithm == "crc32") { |
| 38 |
$requestedAlgorithm = "crc32b"; |
| 39 |
} |
| 40 |
return \base64_encode(Psr7\Utils::hash($value, $requestedAlgorithm, \true)); |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|