forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenSslCipherMethodsProvider.php
More file actions
57 lines (47 loc) · 1.61 KB
/
OpenSslCipherMethodsProvider.php
File metadata and controls
57 lines (47 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PHPStan\DependencyInjection\AutowiredService;
use function array_filter;
use function array_map;
use function array_values;
use function function_exists;
use function in_array;
use function openssl_cipher_iv_length;
use function openssl_get_cipher_methods;
use function strtolower;
#[AutowiredService]
final class OpenSslCipherMethodsProvider
{
/** @var list<string>|null */
private ?array $supportedCipherMethods = null;
/**
* Returns supported cipher methods in lowercase.
*
* Filters out methods that openssl_get_cipher_methods() reports
* but are not actually usable due to https://github.com/php/php-src/issues/19994
*
* @return list<string>
*/
public function getSupportedCipherMethods(): array
{
if ($this->supportedCipherMethods !== null) {
return $this->supportedCipherMethods;
}
$methods = [];
if (function_exists('openssl_get_cipher_methods')) {
// openssl_get_cipher_methods() reports algorithms that are not actually
// supported on PHP 8.0-8.4 due to https://github.com/php/php-src/issues/19994
// Filter by actually testing each algorithm with openssl_cipher_iv_length().
$methods = array_values(array_filter(
openssl_get_cipher_methods(true),
static fn (string $algorithm): bool => @openssl_cipher_iv_length($algorithm) !== false,
));
}
$this->supportedCipherMethods = array_map('strtolower', $methods);
return $this->supportedCipherMethods;
}
public function isSupportedCipherMethod(string $method): bool
{
return in_array(strtolower($method), $this->getSupportedCipherMethods(), true);
}
}