Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\S3;
3
 
4
use AWS\CRT\CRT;
5
use Aws\Exception\CommonRuntimeException;
6
use GuzzleHttp\Psr7;
7
use InvalidArgumentException;
8
 
9
trait CalculatesChecksumTrait
10
{
1441 ariadna 11
    private static $supportedAlgorithms = [
12
        'crc32c' => true,
13
        'crc32' => true,
14
        'sha256' => true,
15
        'sha1' => true
16
    ];
17
 
1 efrain 18
    /**
19
     * @param string $requestedAlgorithm  the algorithm to encode with
20
     * @param string $value               the value to be encoded
21
     * @return string
22
     */
23
    public static function getEncodedValue($requestedAlgorithm, $value) {
24
        $requestedAlgorithm = strtolower($requestedAlgorithm);
25
        $useCrt = extension_loaded('awscrt');
1441 ariadna 26
 
27
        if (isset(self::$supportedAlgorithms[$requestedAlgorithm])) {
28
            if ($useCrt) {
29
                $crt = new Crt();
30
                switch ($requestedAlgorithm) {
31
                    case 'crc32c':
32
                        return base64_encode(pack('N*',($crt::crc32c($value))));
33
                    case 'crc32':
34
                        return base64_encode(pack('N*',($crt::crc32($value))));
35
                    default:
36
                        break;
37
                }
1 efrain 38
            }
1441 ariadna 39
 
40
            if ($requestedAlgorithm === 'crc32c') {
1 efrain 41
                throw new CommonRuntimeException("crc32c is not supported for checksums "
42
                    . "without use of the common runtime for php.  Please enable the CRT or choose "
43
                    . "a different algorithm."
44
                );
45
            }
1441 ariadna 46
 
47
            if ($requestedAlgorithm === "crc32") {
1 efrain 48
                $requestedAlgorithm = "crc32b";
49
            }
50
            return base64_encode(Psr7\Utils::hash($value, $requestedAlgorithm, true));
51
        }
1441 ariadna 52
 
53
        $validAlgorithms = implode(', ', array_keys(self::$supportedAlgorithms));
54
        throw new InvalidArgumentException(
55
            "Invalid checksum requested: {$requestedAlgorithm}."
56
            . "  Valid algorithms supported by the runtime are {$validAlgorithms}."
57
        );
1 efrain 58
    }
59
}