|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Patchlevel\Hydrator\Extension\Cryptography; |
| 6 | + |
| 7 | +use Patchlevel\Hydrator\Extension\Cryptography\Cipher\Cipher; |
| 8 | +use Patchlevel\Hydrator\Extension\Cryptography\Cipher\CipherKey; |
| 9 | +use Patchlevel\Hydrator\Extension\Cryptography\Cipher\CipherKeyFactory; |
| 10 | +use Patchlevel\Hydrator\Extension\Cryptography\Cipher\DecryptionFailed; |
| 11 | +use Patchlevel\Hydrator\Extension\Cryptography\Cipher\EncryptionFailed; |
| 12 | +use Patchlevel\Hydrator\Extension\Cryptography\Cipher\OpensslCipher; |
| 13 | +use Patchlevel\Hydrator\Extension\Cryptography\Cipher\OpensslCipherKeyFactory; |
| 14 | +use Patchlevel\Hydrator\Extension\Cryptography\Store\CipherKeyNotExists; |
| 15 | +use Patchlevel\Hydrator\Extension\Cryptography\Store\CipherKeyStore; |
| 16 | + |
| 17 | +use function array_key_exists; |
| 18 | +use function base64_decode; |
| 19 | +use function base64_encode; |
| 20 | +use function is_array; |
| 21 | + |
| 22 | +/** |
| 23 | + * @phpstan-type EncryptedDataV1 array{ |
| 24 | + * __enc: 'v1', |
| 25 | + * data: non-empty-string, |
| 26 | + * method?: non-empty-string, |
| 27 | + * iv?: non-empty-string, |
| 28 | + * } |
| 29 | + */ |
| 30 | +final class BaseCryptographer implements Cryptographer |
| 31 | +{ |
| 32 | + public function __construct( |
| 33 | + private readonly Cipher $cipher, |
| 34 | + private readonly CipherKeyStore $cipherKeyStore, |
| 35 | + private readonly CipherKeyFactory $cipherKeyFactory, |
| 36 | + ) { |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @return EncryptedDataV1 |
| 41 | + * |
| 42 | + * @throws EncryptionFailed |
| 43 | + */ |
| 44 | + public function encrypt(string $subjectId, mixed $value): array |
| 45 | + { |
| 46 | + try { |
| 47 | + $cipherKey = $this->cipherKeyStore->get($subjectId); |
| 48 | + } catch (CipherKeyNotExists) { |
| 49 | + $cipherKey = ($this->cipherKeyFactory)(); |
| 50 | + $this->cipherKeyStore->store($subjectId, $cipherKey); |
| 51 | + } |
| 52 | + |
| 53 | + return [ |
| 54 | + '__enc' => 'v1', |
| 55 | + 'data' => $this->cipher->encrypt($cipherKey, $value), |
| 56 | + 'method' => $cipherKey->method, |
| 57 | + 'iv' => base64_encode($cipherKey->iv), |
| 58 | + ]; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param EncryptedDataV1 $encryptedData |
| 63 | + * |
| 64 | + * @throws CipherKeyNotExists |
| 65 | + * @throws DecryptionFailed |
| 66 | + */ |
| 67 | + public function decrypt(string $subjectId, mixed $encryptedData): mixed |
| 68 | + { |
| 69 | + $cipherKey = $this->cipherKeyStore->get($subjectId); |
| 70 | + |
| 71 | + return $this->cipher->decrypt( |
| 72 | + new CipherKey( |
| 73 | + $cipherKey->key, |
| 74 | + $encryptedData['method'] ?? $cipherKey->method, |
| 75 | + isset($encryptedData['iv']) ? base64_decode($encryptedData['iv']) : $cipherKey->iv, |
| 76 | + ), |
| 77 | + $encryptedData['data'], |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + public function supports(mixed $value): bool |
| 82 | + { |
| 83 | + return is_array($value) && array_key_exists('__enc', $value) && $value['__enc'] === 'v1'; |
| 84 | + } |
| 85 | + |
| 86 | + /** @param non-empty-string $method */ |
| 87 | + public static function createWithOpenssl( |
| 88 | + CipherKeyStore $cryptoStore, |
| 89 | + string $method = OpensslCipherKeyFactory::DEFAULT_METHOD, |
| 90 | + ): static { |
| 91 | + return new self( |
| 92 | + new OpensslCipher(), |
| 93 | + $cryptoStore, |
| 94 | + new OpensslCipherKeyFactory($method), |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
0 commit comments