Proyectos de Subversion Moodle

Rev

| 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
{
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
        $requestedAlgorithm = strtolower($requestedAlgorithm);
18
        $useCrt = extension_loaded('awscrt');
19
        if ($useCrt) {
20
            $crt = new Crt();
21
            switch ($requestedAlgorithm) {
22
                case 'crc32c':
23
                    return base64_encode(pack('N*',($crt->crc32c($value))));
24
                case 'crc32':
25
                    return base64_encode(pack('N*',($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(
32
                    "Invalid checksum requested: {$requestedAlgorithm}."
33
                    . "  Valid algorithms are CRC32C, CRC32, SHA256, and SHA1."
34
                );
35
            }
36
        }  else {
37
            if ($requestedAlgorithm == 'crc32c') {
38
                throw new CommonRuntimeException("crc32c is not supported for checksums "
39
                    . "without use of the common runtime for php.  Please enable the CRT or choose "
40
                    . "a different algorithm."
41
                );
42
            }
43
            if ($requestedAlgorithm == "crc32") {
44
                $requestedAlgorithm = "crc32b";
45
            }
46
            return base64_encode(Psr7\Utils::hash($value, $requestedAlgorithm, true));
47
        }
48
    }
49
 
50
}