PluginProbe
Media Cloud Sync / 1.4.2
Media Cloud Sync v1.4.2
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / s3 / Aws / S3 / CalculatesChecksumTrait.php

CalculatesChecksumTrait.php in Media Cloud Sync 1.4.2, at includes/sdk/s3/Aws/S3/CalculatesChecksumTrait.php

45 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 private static $supportedAlgorithms = ['crc32c' => \true, 'crc32' => \true, 'sha256' => \true, 'sha1' => \true];
12 /**
13 * @param string $requestedAlgorithm the algorithm to encode with
14 * @param string $value the value to be encoded
15 * @return string
16 */
17 public static function getEncodedValue($requestedAlgorithm, $value)
18 {
19 $requestedAlgorithm = \strtolower($requestedAlgorithm);
20 $useCrt = \extension_loaded('awscrt');
21 if (isset(self::$supportedAlgorithms[$requestedAlgorithm])) {
22 if ($useCrt) {
23 $crt = new Crt();
24 switch ($requestedAlgorithm) {
25 case 'crc32c':
26 return \base64_encode(\pack('N*', $crt::crc32c($value)));
27 case 'crc32':
28 return \base64_encode(\pack('N*', $crt::crc32($value)));
29 default:
30 break;
31 }
32 }
33 if ($requestedAlgorithm === 'crc32c') {
34 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.");
35 }
36 if ($requestedAlgorithm === "crc32") {
37 $requestedAlgorithm = "crc32b";
38 }
39 return \base64_encode(Psr7\Utils::hash($value, $requestedAlgorithm, \true));
40 }
41 $validAlgorithms = \implode(', ', \array_keys(self::$supportedAlgorithms));
42 throw new InvalidArgumentException("Invalid checksum requested: {$requestedAlgorithm}." . " Valid algorithms supported by the runtime are {$validAlgorithms}.");
43 }
44 }
45