diff options
Diffstat (limited to 'vendor/symfony/cache/Marshaller/SodiumMarshaller.php')
-rw-r--r-- | vendor/symfony/cache/Marshaller/SodiumMarshaller.php | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/vendor/symfony/cache/Marshaller/SodiumMarshaller.php b/vendor/symfony/cache/Marshaller/SodiumMarshaller.php new file mode 100644 index 0000000..77d16e8 --- /dev/null +++ b/vendor/symfony/cache/Marshaller/SodiumMarshaller.php | |||
@@ -0,0 +1,74 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
7 | * | ||
8 | * For the full copyright and license information, please view the LICENSE | ||
9 | * file that was distributed with this source code. | ||
10 | */ | ||
11 | |||
12 | namespace Symfony\Component\Cache\Marshaller; | ||
13 | |||
14 | use Symfony\Component\Cache\Exception\CacheException; | ||
15 | use Symfony\Component\Cache\Exception\InvalidArgumentException; | ||
16 | |||
17 | /** | ||
18 | * Encrypt/decrypt values using Libsodium. | ||
19 | * | ||
20 | * @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com> | ||
21 | */ | ||
22 | class SodiumMarshaller implements MarshallerInterface | ||
23 | { | ||
24 | private MarshallerInterface $marshaller; | ||
25 | |||
26 | /** | ||
27 | * @param string[] $decryptionKeys The key at index "0" is required and is used to decrypt and encrypt values; | ||
28 | * more rotating keys can be provided to decrypt values; | ||
29 | * each key must be generated using sodium_crypto_box_keypair() | ||
30 | */ | ||
31 | public function __construct( | ||
32 | private array $decryptionKeys, | ||
33 | ?MarshallerInterface $marshaller = null, | ||
34 | ) { | ||
35 | if (!self::isSupported()) { | ||
36 | throw new CacheException('The "sodium" PHP extension is not loaded.'); | ||
37 | } | ||
38 | |||
39 | if (!isset($decryptionKeys[0])) { | ||
40 | throw new InvalidArgumentException('At least one decryption key must be provided at index "0".'); | ||
41 | } | ||
42 | |||
43 | $this->marshaller = $marshaller ?? new DefaultMarshaller(); | ||
44 | } | ||
45 | |||
46 | public static function isSupported(): bool | ||
47 | { | ||
48 | return \function_exists('sodium_crypto_box_seal'); | ||
49 | } | ||
50 | |||
51 | public function marshall(array $values, ?array &$failed): array | ||
52 | { | ||
53 | $encryptionKey = sodium_crypto_box_publickey($this->decryptionKeys[0]); | ||
54 | |||
55 | $encryptedValues = []; | ||
56 | foreach ($this->marshaller->marshall($values, $failed) as $k => $v) { | ||
57 | $encryptedValues[$k] = sodium_crypto_box_seal($v, $encryptionKey); | ||
58 | } | ||
59 | |||
60 | return $encryptedValues; | ||
61 | } | ||
62 | |||
63 | public function unmarshall(string $value): mixed | ||
64 | { | ||
65 | foreach ($this->decryptionKeys as $k) { | ||
66 | if (false !== $decryptedValue = @sodium_crypto_box_seal_open($value, $k)) { | ||
67 | $value = $decryptedValue; | ||
68 | break; | ||
69 | } | ||
70 | } | ||
71 | |||
72 | return $this->marshaller->unmarshall($value); | ||
73 | } | ||
74 | } | ||