Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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